Claude Code API key: use Claude Code with your own Anthropic key

Updated · 6 min read

Claude Code, Anthropic's coding agent, runs on one of two kinds of account. You can sign in with a Claude subscription (Pro, Max, Ultra, Team or Enterprise), and your usage counts against the plan's limits. Or you can use a Claude Console account with an API key and pay for every token. To switch Claude Code to an API key, set ANTHROPIC_API_KEY before you run claude and approve the key when it asks, or run /login and choose the Console account. /status shows which one is active. The free Claude plan doesn't include Claude Code.

Subscription or API key

Claude subscription Console account (API key)
How you sign in claude.ai account through /login ANTHROPIC_API_KEY, /login with a Console account, or an apiKeyHelper script
What you pay Pro $20/month ($17 billed annually), Max $100, Ultra $200 Per token, from your Console credit balance
Limits Usage windows every 5 hours and every week (Ultra has no 5-hour limit for verified accounts); paid usage credits let you go past them Your organization's rate limits and spend limits
Features only a subscription gets Cloud sessions, Remote Control, Chrome extension, /schedule, voice dictation None of these

Prices are from Anthropic's pricing page on 2026-11-18. Max gives 5x Pro usage and Ultra 10x Pro usage per week.

Step by step: Claude Code with an API key

  1. Create a key in the Claude Console at platform.claude.com and add credit on the Billing page. The Claude API key guide covers both.
  2. Install Claude Code: curl -fsSL https://claude.ai/install.sh | bash on macOS, Linux and WSL, irm https://claude.ai/install.ps1 | iex in Windows PowerShell, or brew install --cask claude-code / winget install Anthropic.ClaudeCode.
  3. Export the key: export ANTHROPIC_API_KEY=sk-ant-.... Add the line to ~/.zshrc or ~/.bashrc to keep it.
  4. Run claude. It asks once whether to use the key and remembers your answer; the "Use custom API key" toggle in /config changes it later. In non-interactive mode (claude -p) the key is always used.
  5. Check with /status. From the shell, claude auth status prints an authMethod field: api_key, claude.ai and so on.

You can also skip the environment variable: run /login (or claude auth login --console) and choose the Anthropic Console account. Claude Code then offers Sign in with your Console account, which stores a login and creates no key, or Create an API key (marked legacy). Both bill your Console organization.

The first Console sign-in creates a workspace called Claude Code in your organization. It collects Claude Code spend, you can't create keys in it by hand, and it is the only workspace with per-user monthly spend limits.

For keys from a password vault or short-lived tokens, set "apiKeyHelper": "/path/to/get-key.sh" in ~/.claude/settings.json. Claude Code runs the script, caches the output for five minutes, and runs it again after a 401 or 403 error.

Which credential wins

When several are present, Claude Code picks the first one in this order:

  1. A cloud provider, when CLAUDE_CODE_USE_BEDROCK, CLAUDE_CODE_USE_VERTEX or CLAUDE_CODE_USE_FOUNDRY is set
  2. ANTHROPIC_AUTH_TOKEN (a bearer token, for gateways)
  3. ANTHROPIC_API_KEY
  4. apiKeyHelper
  5. CLAUDE_CODE_OAUTH_TOKEN (a subscription token from claude setup-token)
  6. Anthropic profiles and federation credentials
  7. Your subscription login from /login

So a Pro or Max user with ANTHROPIC_API_KEY in their shell is billed per token once they approve the key. Run unset ANTHROPIC_API_KEY to fall back to the subscription. When both are configured, /status marks the credential that isn't in use.

The variables and apiKeyHelper apply to the CLI, the VS Code extension, the Agent SDK and GitHub Actions. The desktop app and cloud sessions don't read them.

API prices per million tokens

Claude Code's default model is Opus 5.5 on every subscription plan and on the Anthropic API. Switch with /model sonnet, /model haiku or /model fable. Anthropic's prices on 2026-11-18:

Model (alias) Input Output Cache read Cache write (5 min)
Opus 5.5 (opus, default) $4 $20 $0.20 $5
Sonnet 5.5 (sonnet) $2 $10 $0.10 $2.50
Haiku 5.5 (haiku), prompts up to 100K tokens $0.10 $0.50 $0.01 $0.125
Fable 5.2 (fable) $8 $40 $0.20 $10

Cache prices matter because Claude Code re-sends the full context on every turn and most of it is read from the cache. In the sample /usage output in Anthropic's docs, a session read 940K tokens from the cache and sent only 1.2K uncached input tokens. Fast mode for Opus 5.5 and Fable 5.2 costs twice the standard price.

Is an API key cheaper than a subscription?

For daily use, an API key usually costs more. Anthropic's cost page says enterprise deployments average about $13 per developer per active day and $150-250 per developer per month, with 90% of users below $30 per active day. At that average, two active days cost more than a month of Pro, and a month of regular use costs more than Max.

An API key makes sense when:

  • you use Claude Code a few times a month;
  • you run it in scripts or CI (claude -p); bare mode (--bare) reads only an API key or apiKeyHelper;
  • your company pays through a Console organization or a cloud account;
  • you don't want the 5-hour and weekly windows.

If you are a subscriber who runs into limits now and then, /usage-credits opens the settings where you turn on paid usage credits, so you can keep your plan instead of switching.

Amazon Bedrock, Google Cloud and Microsoft Foundry

Claude Code also runs on Claude models in your cloud account, billed by the cloud provider:

  • Amazon Bedrock: submit the use case form for an Anthropic model in the Bedrock model catalog. Then run claude, choose 3rd-party platform → Amazon Bedrock and follow the wizard (/setup-bedrock reopens it). Manual setup is CLAUDE_CODE_USE_BEDROCK=1 plus AWS_REGION; a Bedrock API key goes in AWS_BEARER_TOKEN_BEDROCK.
  • Google Cloud's Agent Platform (formerly Vertex AI): enable the API, request the Claude models in Model Garden, then pick Google Vertex AI at the login prompt, or set CLAUDE_CODE_USE_VERTEX=1, CLOUD_ML_REGION=global and ANTHROPIC_VERTEX_PROJECT_ID.
  • Microsoft Foundry: create Claude deployments, then set CLAUDE_CODE_USE_FOUNDRY=1, ANTHROPIC_FOUNDRY_RESOURCE and ANTHROPIC_FOUNDRY_API_KEY (or sign in with az login instead of a key).

On these platforms the aliases can point at older models (sonnet is Sonnet 4.5 on Bedrock and Agent Platform), so Anthropic's docs recommend pinning model versions. Some features are missing; web search, for example, doesn't work on Bedrock. Spend limits live in your cloud's budget tools.

Teams and gateways

Anthropic recommends Claude for Teams or Enterprise for most teams. Team seats cost $20 per month (Standard, billed annually) or $100 (Premium). Teams that prefer API billing invite members to a Console organization. The Claude Code role can create only Claude Code keys.

If your company runs an LLM gateway, set ANTHROPIC_BASE_URL to its address and put the credential in ANTHROPIC_AUTH_TOKEN (sent as a bearer token) or ANTHROPIC_API_KEY (sent as x-api-key). Anthropic doesn't support routing Claude Code to non-Claude models through any gateway. A base URL other than api.anthropic.com turns off Remote Control.

Control what you spend

  • /usage (alias /cost) shows the session's cost. Claude Code computes it locally at list price, so treat it as an estimate.
  • Console spend limits: at platform.claude.com/settings/spend-limits, pick the Claude Code workspace, set a monthly limit and add an email notification.
  • --max-budget-usd stops a scripted run once estimated spend reaches the cap: claude -p --max-budget-usd 5.00 "...". It works only in print mode, and spend can pass the cap, so leave some headroom.
  • A cheaper model helps: Sonnet 5.5 costs half as much per token as Opus 5.5.

Keep the key safe

  • Never put the key in a project's .claude/settings.json: that file is committed and shared with everyone who clones the repository. Use your shell profile, the env block of ~/.claude/settings.json, or an apiKeyHelper that reads a vault.
  • Give each machine or CI job its own key, so you can revoke one in the Console without breaking the others.

Other coding agents that take your Claude API key

  • Cline

    Free, open source · VS Code, JetBrains, CLI · Open source

    Visit

    Open-source AI coding agent for VS Code, JetBrains and the terminal. Bring your own key, endpoint or local model.

    Works with:AnthropicOpenAIGoogle GeminiOllama+1 more

  • Kilo Code

    Free to start, no markup on model usage · VS Code, JetBrains, CLI · Open source

    Visit

    Open-source AI coding agent for VS Code, JetBrains and the CLI. Use 500+ models at zero markup or bring your own keys.

    Works with:AnthropicOpenAIGoogle GeminiOpenRouter+8 more

  • Aider

    Free, open source · CLI · Open source

    Visit

    AI pair programming in your terminal. Works with your own Anthropic, OpenAI, DeepSeek and other model keys.

    Works with:AnthropicOpenAIDeepSeek

  • OpenCode

    Free, open source · CLI, Web · Open source

    Visit

    Open-source AI coding agent for the terminal, desktop and web that works with any model provider or local models.

    Works with:OpenAIAnthropicxAI

  • Zed

    Free editor; paid plans for Zed-hosted AI · macOS, Windows, Linux · Open source

    Visit

    Fast open-source code editor with an AI agent that works with your own Anthropic, OpenAI, Gemini, OpenRouter or local model keys.

    Works with:AnthropicOpenAIGoogle GeminiMistral+5 more

  • goose

    Free, open source · macOS, Windows, Linux · Open source

    Visit

    Open-source AI agent for desktop and CLI that automates engineering tasks with any LLM and MCP extensions.

    Works with:AnthropicOpenAIGoogle GeminiOpenRouter+3 more

  • OpenHands

    Free, open source; hosted and enterprise plans · Web, CLI, Self-hosted · Open source

    Visit

    Open-source platform for AI coding agents with a GUI, CLI and SDK. Use any LLM with your own API key or local models.

    Works with:OpenAIAnthropicGoogle GeminiOpenRouter+6 more

  • Refact

    Free, open source (Refact Cloud discontinued) · VS Code, JetBrains · Open source

    Visit

    Open-source AI coding agent for VS Code and JetBrains that runs a local engine with your own provider keys or local models.

    Works with:OpenAIAnthropicGoogle GeminiDeepSeek+5 more

See all 89 apps that work with Anthropic

Other agents

Anthropic supports Claude Code only with Claude models. If you want one agent for several providers, the open-source agents above take an Anthropic key alongside others. Codex is OpenAI's equivalent with its own API key option, and Cursor accepts an Anthropic key for its chat models.

Questions

Does Claude Code need an API key?

No. You can sign in with a Claude Pro, Max, Ultra, Team or Enterprise plan instead. An API key from the Claude Console is the pay-per-token alternative. The Free plan does not include Claude Code.

Can I use my Claude Pro or Max subscription as an API key?

No. A subscription works only through sign-in. For scripts and CI, claude setup-token creates a one-year token (CLAUDE_CODE_OAUTH_TOKEN) that uses your subscription, but it works only in Claude Code and only for model requests.

How do I switch Claude Code from my API key back to my subscription?

Run unset ANTHROPIC_API_KEY, remove the variable from your shell profile and settings files, then run /login with your claude.ai account. /status shows which credential is active.

How much does Claude Code cost with an API key?

You pay per token from your Console balance: Opus 5.5, the default model, is $4 per million input tokens and $20 per million output tokens; Sonnet 5.5 is $2 and $10. Anthropic puts the enterprise average at about $13 per developer per active day.

Can Claude Code use GPT, Gemini or other non-Claude models?

Anthropic does not support routing Claude Code to non-Claude models through any gateway. For other models, use OpenAI's Codex or an open-source agent such as Cline, Aider or OpenCode.

Where does Claude Code store my login?

In the macOS Keychain, or in ~/.claude/.credentials.json (file mode 0600) on Linux and %USERPROFILE%\.claude\.credentials.json on Windows.

Sources

Checked against the providers' own documentation on 2026-11-18. Prices and limits change; the links below have the current figures.

Related guides