How to use Claude Code with an API key instead of a subscription
Yes, Claude Code works with an Anthropic API key from the Claude Console. Set the ANTHROPIC_API_KEY environment variable and run claude, or choose the Console account at the /login prompt. With a key you pay for API tokens used. With a Pro, Max, Ultra, Team or Enterprise login your usage is included in the plan's limits. If both exist, an approved ANTHROPIC_API_KEY overrides your subscription until you unset it.
Can you use Claude Code with an API key?
Yes. Claude Code authenticates in several ways, and two matter for most builders: a claude.ai subscription login and an Anthropic API key from the Claude Console. They bill differently. The facts below are from the authentication and costs pages of the Claude Code docs, checked in October 2026.
| Subscription login | API key (Console) | |
|---|---|---|
| How you sign in | /login with your claude.ai account on Pro, Max, Ultra, Team or Enterprise | ANTHROPIC_API_KEY, or the Console account at /login |
| How you pay | Usage is included in your plan, within its limits | Per API token consumed |
| What happens at the limit | You can add usage credits to keep working (/usage-credits) | Billed per token with no plan allowance to run out of; rate limits apply at the organization level, and /usage-credits is not available with API key authentication |
| Where to watch spend | /usage shows plan usage bars and a breakdown | Session cost in /usage is an estimate; the Console Usage page is authoritative |
| Prompt cache lifetime for the main conversation | About one hour within plan usage | Five minutes |
| Team controls | Admin console for Team and Enterprise | Console workspaces with spend limits |
For plan names and what they cost today, see the Claude Code pricing guide and the usage limits guide. This page sticks to credentials.
How do you set up Claude Code with an API key?
Create a key in the Claude Console (platform.claude.com), then give it to Claude Code through an environment variable. Install Claude Code first if you have not; the install guide has the steps.
# 1. Put your Console key in the environment for this shell export ANTHROPIC_API_KEY=your-key-here # 2. Start Claude Code in your project claude # 3. Confirm which credential is active /status
In an interactive session Claude Code asks once whether to approve the key, and remembers your answer. To change it later, use the “Use custom API key” toggle in /config, which appears only while ANTHROPIC_API_KEY is set. In non-interactive mode with -p, the key is always used when present. To make the variable permanent, add the export line to your shell profile. Never paste a real key into a file you commit.
The same variable works in scripts and CI. Bare mode, which skips local configuration for faster scripted starts, never reads your subscription login. Set ANTHROPIC_API_KEY or supply an apiKeyHelper in the --settings JSON.
Can you sign in with a Console account instead of pasting a key?
Yes. At the /login prompt choose the Anthropic Console account. Your Console admin must have invited you first. From Claude Code v2.1.242, Claude Code then asks how you want to sign in:
- Sign in with your Console account, labeled recommended. Claude Code keeps the OAuth token from that sign-in as an Anthropic profile and creates no API key. It refreshes the login automatically, and if refresh fails requests error until you sign in again.
- Create an API key, labeled legacy. Claude Code creates a Console key for you and stores it with your other credentials.
Some setups skip the question and create a key without asking, for example when you run against a cloud provider such as Amazon Bedrock, or when a settings file sets forceLoginOrgUUID. Unset ANTHROPIC_API_KEY before you sign in without a key. Running /logout removes and revokes the credential that sign-in wrote.
When you first authenticate with a Console account, a workspace called Claude Code is created automatically. It gives your organization central cost tracking for Claude Code. You cannot create API keys for that workspace, and its traffic counts toward your organization's overall rate limits. You can set a workspace spend limit and a workspace rate limit from the Console.
Which credential does Claude Code use when you have several?
When more than one is present, Claude Code chooses in this order, as listed in the docs:
- Cloud provider credentials, when CLAUDE_CODE_USE_BEDROCK, CLAUDE_CODE_USE_VERTEX or CLAUDE_CODE_USE_FOUNDRY is set.
- The ANTHROPIC_AUTH_TOKEN variable, sent as a bearer token. Meant for LLM gateways and proxies.
- The ANTHROPIC_API_KEY variable, sent as the X-Api-Key header.
- The output of an apiKeyHelper script, for dynamic or rotating credentials such as short-lived tokens from a vault.
- The CLAUDE_CODE_OAUTH_TOKEN variable, a long-lived subscription token for CI and scripts, generated with claude setup-token.
- Anthropic profile and federation credentials.
- Subscription OAuth credentials from /login, the default for Pro, Max, Ultra, Team and Enterprise users.
A signed-in Claude apps gateway session sits outside this list and outranks the providers.
Which environment variables matter for API key use?
| Variable | What it does |
|---|---|
| ANTHROPIC_API_KEY | API key sent as X-Api-Key. When set, it is used instead of your subscription even if you are logged in, after you approve it in interactive mode |
| ANTHROPIC_AUTH_TOKEN | A custom value for the Authorization header, prefixed with Bearer. Use it with a gateway that authenticates with bearer tokens |
| CLAUDE_CODE_OAUTH_TOKEN | A long-lived subscription token from claude setup-token. For CI where browser login is not possible. Bare mode does not read it |
| ANTHROPIC_BASE_URL | Changes where requests go, such as a proxy or gateway. It does not change which model answers |
| ANTHROPIC_MODEL | The model setting to use for the session; see the models guide |
| CLAUDE_CONFIG_DIR | Gives an account its own settings, history and login or key |
To keep two accounts apart, such as work and personal, give each its own configuration directory with an alias like alias claude-work='CLAUDE_CONFIG_DIR=~/.claude-work claude'. Each directory has its own settings, session history, and claude.ai login or API key.
{
"apiKeyHelper": "/bin/generate_temp_api_key.sh"
}Claude Code runs that command through the system shell and sends its output as both the X-Api-Key and Authorization: Bearer headers. If the script takes longer than 10 seconds, Claude Code shows a slow-helper notice, and if it fails, requests error within three attempts. Credentials from /login are stored in the macOS Keychain, or in a file in your Claude folder with restricted permissions on Linux.
How do billing and spend tracking differ with a key?
Claude Code charges by API token consumption when you use a key. The cost depends on the model, codebase size, how many sessions run at once and any automation. The Session block at the top of /usage shows token usage for the current session, and the docs say it is meant for API users. Claude Code computes the dollar figure locally from token counts at list price, so treat it as an estimate. The authoritative number is the Usage page in the Claude Console. These totals reset when /clear starts a new session.
On a subscription the same session figure is not relevant for billing, because usage is included in the plan. Subscribers see plan usage bars and a breakdown of what counts against their limits instead.
- Pick the model deliberately. A smaller model costs less on routine work. See which model for which job.
- Clear between tasks. Every request sends the whole conversation, and the cache is only five minutes on a key. See the context window guide.
- Set a spend limit. Console workspaces let you cap Claude Code spend. Admins can also set a workspace rate limit to protect other workloads.
- Cap automated runs. In scripts and the GitHub Action use turn limits and timeouts; see the GitHub Actions guide.
Should you use a subscription or an API key?
| Situation | Fits better | Why |
|---|---|---|
| Daily building on your own laptop | Subscription login | Usage is included in the plan, and the main conversation gets the longer cache |
| CI, scripts and the GitHub Action for a team | API key from the Console | Not tied to one person's account; the GitHub Actions docs recommend a key for secrets shared across repositories |
| An organization that wants spend caps per project | Console workspaces with limits | Spend limits and per-user reporting live in the Console |
| Short-lived or rotating credentials | apiKeyHelper | The script can fetch a fresh token from a vault |
| Your company runs on Amazon Bedrock, Google Cloud or Microsoft Foundry | Cloud provider credentials | They rank first in the credential order |
If you have a long-lived subscription token and want to run Claude in CI, claude setup-token prints a one-year OAuth token after browser approval. It does not save it; you copy it into CLAUDE_CODE_OAUTH_TOKEN. It requires a Pro, Max, Ultra, Team or Enterprise plan and can only make model requests.
Once you are signed in, a good first thing to do with Claude Code is build something small. The board has ideas with ready build prompts, or you can post one you will not build yourself.
How do you keep your API key safe?
- Never commit it. In GitHub Actions use repository secrets and reference them as secrets in the workflow.
- Keep it out of shared settings. A project's .claude/settings.json is meant to be committed. Use your shell profile, a secret manager or apiKeyHelper instead.
- Deny Claude access to env files. Add Read(./.env) to permissions.deny. See the permissions guide.
- Delete retired keys in the Console. Deleting a GitHub secret does not revoke the credential it held.
- Run /logout on shared machines. It removes and revokes the credential a Console sign-in wrote.
Frequently asked questions
Does Claude Code need an API key?
No. You can sign in with a Claude Pro, Max, Ultra, Team or Enterprise account instead, and your usage is then included in the plan. An API key from the Claude Console is the alternative, and it bills per token. Organizations can also use Amazon Bedrock, Google Cloud's Agent Platform or Microsoft Foundry credentials.
How do I use my Anthropic API key with Claude Code?
Set the ANTHROPIC_API_KEY environment variable, for example export ANTHROPIC_API_KEY=your-key, then run claude. Approve the key when asked. You can also choose the Anthropic Console account at the /login prompt. Run /status to confirm which credential is active, and avoid committing the key to any repository.
Where do I get an API key for Claude Code?
Create one in the Claude Console at platform.claude.com. If you sign in to Claude Code with a Console account, you can choose to have it create a key for you, though that option is labeled legacy; the recommended route signs in without creating a key. Admins can invite members with the Claude Code role, which can only create Claude Code keys.
Why is Claude Code charging my API key instead of my subscription?
Because ANTHROPIC_API_KEY is set in your environment and you approved it. An approved key overrides your subscription login. Run unset ANTHROPIC_API_KEY, restart Claude Code and check /status to confirm the subscription is active. To keep the key for scripts only, set it for those commands instead of in your shell profile.
Can I use a subscription and an API key at the same time?
Not in one session. Claude Code picks one credential by a fixed order, and an approved ANTHROPIC_API_KEY ranks above subscription OAuth. You can keep both configured and use /status to see which is active. To run two accounts side by side, give each a separate CLAUDE_CONFIG_DIR.
How do I check how much I'm spending with an API key?
Run /usage and read the Session block for an estimate of token usage in the current session. It is computed locally at list price, so for the authoritative number open the Usage page in the Claude Console. Organizations can set workspace spend limits and view per-member spend there.
Created with AI assistance and edited for this site. Examples are illustrative and do not represent customer research. About the publisher.