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
- 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.
- Install Claude Code:
curl -fsSL https://claude.ai/install.sh | bashon macOS, Linux and WSL,irm https://claude.ai/install.ps1 | iexin Windows PowerShell, orbrew install --cask claude-code/winget install Anthropic.ClaudeCode. - Export the key:
export ANTHROPIC_API_KEY=sk-ant-.... Add the line to~/.zshrcor~/.bashrcto keep it. - Run
claude. It asks once whether to use the key and remembers your answer; the "Use custom API key" toggle in/configchanges it later. In non-interactive mode (claude -p) the key is always used. - Check with
/status. From the shell,claude auth statusprints anauthMethodfield:api_key,claude.aiand 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:
- A cloud provider, when
CLAUDE_CODE_USE_BEDROCK,CLAUDE_CODE_USE_VERTEXorCLAUDE_CODE_USE_FOUNDRYis set ANTHROPIC_AUTH_TOKEN(a bearer token, for gateways)ANTHROPIC_API_KEYapiKeyHelperCLAUDE_CODE_OAUTH_TOKEN(a subscription token fromclaude setup-token)- Anthropic profiles and federation credentials
- 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 orapiKeyHelper; - 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-bedrockreopens it). Manual setup isCLAUDE_CODE_USE_BEDROCK=1plusAWS_REGION; a Bedrock API key goes inAWS_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=globalandANTHROPIC_VERTEX_PROJECT_ID. - Microsoft Foundry: create Claude deployments, then set
CLAUDE_CODE_USE_FOUNDRY=1,ANTHROPIC_FOUNDRY_RESOURCEandANTHROPIC_FOUNDRY_API_KEY(or sign in withaz logininstead 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-usdstops 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, theenvblock of~/.claude/settings.json, or anapiKeyHelperthat 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
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.
- code.claude.com/docs/en/authentication
- code.claude.com/docs/en/costs
- code.claude.com/docs/en/feature-availability
- code.claude.com/docs/en/model-config
- code.claude.com/docs/en/settings-reference
- code.claude.com/docs/en/env-vars
- code.claude.com/docs/en/cli-reference
- code.claude.com/docs/en/commands
- code.claude.com/docs/en/setup
- code.claude.com/docs/en/amazon-bedrock
- code.claude.com/docs/en/google-vertex-ai
- code.claude.com/docs/en/microsoft-foundry
- code.claude.com/docs/en/llm-gateway
- code.claude.com/docs/en/llm-gateway-connect
- claude.com/pricing
- platform.claude.com/docs/en/about-claude/pricing
- platform.claude.com/docs/en/build-with-claude/workspaces