AAnvio
Security

Runtime OAuth

How Anvio authenticates vendor CLI runtimes (Claude Code, Cursor, Codex, Antigravity) via their own official OAuth — never Anvio-hosted login.

7 min read

Runtime OAuth is Anvio's mechanism for running agents on a vendor subscription (Claude Pro/Max, ChatGPT, Google Sign-In) instead of a pay-per-token API key. It is documented in full in ADR 0009 and is a completely different system from the model-provider API keys covered in Authentication.

Don't conflate these two systems:

  • Model provider API key (ANTHROPIC_API_KEY, etc.) → powers the local runtime's direct API calls, billed per token.
  • Runtime OAuth (anvio setup-token) → powers vendor-native runtimes (claude-code, cursor, codex, antigravity), billed against your existing subscription quota.

Setting both for the same agent is a footgun — see the warning at the end of this page.

Why this exists

Anvio supports runtime providers beyond the built-in local tool loop: claude-code, cursor, codex, and antigravity. Anthropic's third-party policy prohibits products from hosting their own claude.ai login UI or reselling subscription rate limits. So instead of building an OAuth screen, Anvio delegates login to the official vendor CLI and only stores the resulting token locally, encrypted, via the connection broker (see Secrets & Encryption).

The two-layer model (from ADR 0009)

LayerAuth mechanismWhen used
Runtime providerOAuth token / app session via official vendor toolingagent.spec.runtime.provider ∈ {claude-code, cursor, codex, antigravity}
Model providerAPI key / credential poollocal runtime (default) and explicit model routing

Onboarding: anvio setup-token

anvio setup-token --claude       # wraps official `claude setup-token`
anvio setup-token --cursor       # wraps official `agent login`
anvio setup-token --codex        # wraps official `codex login`
anvio setup-token --antigravity  # wraps official `agy auth login` (auto-installs CLI if missing)
anvio setup-token --list         # show supported vendors

Legacy alias: anvio connect login claude-code → anvio setup-token --claude.

Token flow

Rendering diagram…

Per-vendor mechanics

Claude Code (implemented)

  1. ClaudeCodeRuntimeProvider uses @anthropic-ai/claude-agent-sdk.
  2. Auth resolution order:
    • Encrypted connection broker payload (service: claude-code)
    • CLAUDE_CODE_OAUTH_TOKEN environment variable
    • Interactive CLI cache (SDK fallback, only if the user already ran claude login themselves)
  3. ANTHROPIC_API_KEY is never passed into the Agent SDK subprocess env when OAuth mode is active — an API key would shadow OAuth and bill API credits instead of subscription quota.
spec:
  runtime:
    provider: claude-code
    fallback: local
  model:
    provider: anthropic   # used only when fallback: local
    model: claude-sonnet-4-20250514

Cursor (implemented)

  • Auth via Cursor agent CLI (agent login) or ACP (anvio acp serve).
  • anvio setup-token --cursor stores the CLI session in the connection broker.
  • CursorRuntimeProvider uses ACP when ANVIO_ACP_ENDPOINT is set; otherwise shells out to agent -p.

Codex (implemented)

  • Auth via OpenAI Codex CLI (codex login) → broker stores a snapshot of ~/.codex/auth.json.
  • CodexRuntimeProvider runs codex exec with an isolated auth home and strips OPENAI_API_KEY from the subprocess env.

Antigravity (implemented)

  • Official Antigravity CLI (agy) — Google Sign-In via the system keyring.
  • anvio setup-token --antigravity wraps agy auth login, auto-installing the CLI if missing.
  • AntigravityRuntimeProvider runs agy -p; strips GEMINI_API_KEY; supports an optional ANTIGRAVITY_TOKEN for CI.

Fallback chains (cross-vendor)

RuntimeRoutingAgentRuntime selects the configured external runtime, or falls back to DefaultAgentRuntime (model provider + tool loop) when unconfigured. Fallback walks an ordered chain:

# Chain: claude-code → cursor → codex → local
spec:
  runtime:
    provider: claude-code
    fallbacks: [cursor, codex, local]
  model:
    provider: anthropic   # used only when fallback reaches `local`
    model: claude-sonnet-4-20250514

Shorthand for a single hop still works: fallback: local expands to [provider, local].

TriggerBehavior
Primary not configured (isConfigured() false)Skip to next in chain
Primary auth failure mid-run (expired token, 401, login required)Retry next in chain
Non-auth errorFail immediately — no failover

Per-agent and per-user runtime

ScopeMechanismExample
Per agentagent.spec.runtime in agent YAML/MDarchitect → claude-code, coder → codex
Per userConnection broker key channel:userId:serviceAlice OAuth'd to Claude, Bob OAuth'd to Codex
# Multi-user OAuth on one workspace
export ANVIO_CONNECTION_ENCRYPTION_KEY=your-secret
anvio setup-token --claude --user alice
anvio setup-token --codex  --user bob

Agent-to-agent collaboration goes through orchestration (delegate_task, supervisor pattern, mixture_of_agents, workflows) — not a shared chat thread by default, so each agent's runtime OAuth stays scoped to its own identity.

Docker, server, and headless OAuth

Vendor OAuth flows expect a browser. Headless servers and containers should not run interactive login on first boot. ADR 0009 documents five patterns:

Run login on a machine with a browser, then ship the encrypted broker state to the server.

# On laptop / bastion (has a browser)
export ANVIO_CONNECTION_ENCRYPTION_KEY="${ANVIO_CONNECTION_ENCRYPTION_KEY}"
anvio setup-token --claude --user alice
anvio setup-token --codex  --user bob

# Copy to server
rsync -av ./workspace/connections/ user@server:/opt/anvio/workspace/connections/
services:
  worker:
    environment:
      ANVIO_WORKSPACE: /workspace
      ANVIO_CONNECTION_ENCRYPTION_KEY: ${ANVIO_CONNECTION_ENCRYPTION_KEY}
      # Optional global fallback when broker miss:
      # CLAUDE_CODE_OAUTH_TOKEN: ${CLAUDE_CODE_OAUTH_TOKEN}
    volumes:
      - ./workspace:/workspace
    # Do NOT bake tokens into the image layer

Enable the broker in workspace/harness/defaults.yaml:

connectBroker:
  enabled: true
  encryptionKeyEnv: ANVIO_CONNECTION_ENCRYPTION_KEY

Inject tokens via a secrets manager or .env (never commit):

environment:
  CLAUDE_CODE_OAUTH_TOKEN: ${CLAUDE_CODE_OAUTH_TOKEN}
  CODEX_ACCESS_TOKEN: ${CODEX_ACCESS_TOKEN}   # optional Codex path

The platform resolves Claude OAuth from env when the broker is disabled or empty.

VendorHeadless commandNotes
Codexcodex login --device-authOpen the device URL on another machine
CursorNO_OPEN_BROWSER=1 agent loginPaste the URL manually
Claude Codeclaude setup-token on a bastionCopy token → Pattern 1 or 2

After Codex device login, copy ~/.codex/auth.json or run anvio setup-token --codex --user bob on the bastion.

environment:
  ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY}

Agents with runtime.fallback: local work without any vendor OAuth. Use this when subscription OAuth isn't required in the container.

volumes:
  - ${HOME}/.codex:/root/.codex:ro

Weak isolation — acceptable for single-developer staging, not recommended for multi-tenant production.

Multi-user in Docker

Usersetup-tokenAgent example
alice--claude --user alicearchitect (claude-code)
bob--codex --user bobcoder (codex)

Sessions must use matching userId (via the auth plugin or a defaultUserId override per channel). Each user gets isolated OAuth from the broker; the same agent definition can serve both if the runtime resolves per-user tokens.

Security checklist

  • Never commit ANVIO_CONNECTION_ENCRYPTION_KEY or any OAuth token to git.
  • Rotate the encryption key only with a migration plan — existing encrypted blobs become permanently unreadable.
  • Prefer a secrets manager (Vault, K8s Secret, Docker Secret) over a plain .env in production.
  • Do not run anvio setup-token in CI without a dedicated service-account token path.

Key facts

  • Claude Code agents can run on Pro/Max subscription quota without ever setting ANTHROPIC_API_KEY.
  • The local fallback still requires API keys — unchanged Level 1 behavior.
  • The connection broker stores runtime OAuth tokens separately from MCP/channel tokens, discriminated by a service field (claude-code, codex, cursor, antigravity vs. arbitrary MCP service names).
  • Compliance posture: login UX always stays on the vendor's own CLI/app; Anvio only stores the resulting token locally.

Alternatives considered and rejected (ADR 0009)

AlternativeWhy rejected
Custom claude.ai OAuth UI inside AnvioViolates Anthropic's third-party policy
Single API-key path for all runtimesDouble-billing risk; doesn't use subscription quota
Hard-require an API key on every agentBlocks the vendor-native runtime goal entirely
Edit on GitHub

Last updated on

On this page