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 thelocalruntime'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)
| Layer | Auth mechanism | When used |
|---|---|---|
| Runtime provider | OAuth token / app session via official vendor tooling | agent.spec.runtime.provider ∈ {claude-code, cursor, codex, antigravity} |
| Model provider | API key / credential pool | local 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 vendorsLegacy alias: anvio connect login claude-code → anvio setup-token --claude.
Token flow
Per-vendor mechanics
Claude Code (implemented)
ClaudeCodeRuntimeProvideruses@anthropic-ai/claude-agent-sdk.- Auth resolution order:
- Encrypted connection broker payload (
service: claude-code) CLAUDE_CODE_OAUTH_TOKENenvironment variable- Interactive CLI cache (SDK fallback, only if the user already ran
claude loginthemselves)
- Encrypted connection broker payload (
ANTHROPIC_API_KEYis 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-20250514Cursor (implemented)
- Auth via Cursor agent CLI (
agent login) or ACP (anvio acp serve). anvio setup-token --cursorstores the CLI session in the connection broker.CursorRuntimeProvideruses ACP whenANVIO_ACP_ENDPOINTis set; otherwise shells out toagent -p.
Codex (implemented)
- Auth via OpenAI Codex CLI (
codex login) → broker stores a snapshot of~/.codex/auth.json. CodexRuntimeProviderrunscodex execwith an isolated auth home and stripsOPENAI_API_KEYfrom the subprocess env.
Antigravity (implemented)
- Official Antigravity CLI (
agy) — Google Sign-In via the system keyring. anvio setup-token --antigravitywrapsagy auth login, auto-installing the CLI if missing.AntigravityRuntimeProviderrunsagy -p; stripsGEMINI_API_KEY; supports an optionalANTIGRAVITY_TOKENfor 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-20250514Shorthand for a single hop still works: fallback: local expands to [provider, local].
| Trigger | Behavior |
|---|---|
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 error | Fail immediately — no failover |
Per-agent and per-user runtime
| Scope | Mechanism | Example |
|---|---|---|
| Per agent | agent.spec.runtime in agent YAML/MD | architect → claude-code, coder → codex |
| Per user | Connection broker key channel:userId:service | Alice 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 bobAgent-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 layerEnable the broker in workspace/harness/defaults.yaml:
connectBroker:
enabled: true
encryptionKeyEnv: ANVIO_CONNECTION_ENCRYPTION_KEYInject 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 pathThe platform resolves Claude OAuth from env when the broker is disabled or empty.
| Vendor | Headless command | Notes |
|---|---|---|
| Codex | codex login --device-auth | Open the device URL on another machine |
| Cursor | NO_OPEN_BROWSER=1 agent login | Paste the URL manually |
| Claude Code | claude setup-token on a bastion | Copy 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:roWeak isolation — acceptable for single-developer staging, not recommended for multi-tenant production.
Multi-user in Docker
| User | setup-token | Agent example |
|---|---|---|
alice | --claude --user alice | architect (claude-code) |
bob | --codex --user bob | coder (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_KEYor 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
.envin production. - Do not run
anvio setup-tokenin 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
localfallback still requires API keys — unchanged Level 1 behavior. - The connection broker stores runtime OAuth tokens separately from MCP/channel tokens, discriminated by a
servicefield (claude-code,codex,cursor,antigravityvs. 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)
| Alternative | Why rejected |
|---|---|
| Custom claude.ai OAuth UI inside Anvio | Violates Anthropic's third-party policy |
| Single API-key path for all runtimes | Double-billing risk; doesn't use subscription quota |
| Hard-require an API key on every agent | Blocks the vendor-native runtime goal entirely |
Last updated on