Hivra
A key in your environment quietly beats your subscription. Here is how to choose on purpose.

Claude Code API key: how to use one, which login wins and how to avoid surprise bills

You can run Claude Code on a Claude plan or on an Anthropic API key, and they bill in completely different ways. This guide shows how to get a key, set it, check which one Claude Code is really using, and fix the most common wrong-account errors, as of December 2026.

Short answer

Set the ANTHROPIC_API_KEY environment variable to a key from the Claude Console, start claude, and approve the key when asked. Claude Code then bills you per token through the Console instead of using a Pro or Max plan. A set key beats a subscription login, so unset it to go back.

How to use Claude Code with an API key, step by step

There are four steps. The commands below come from Anthropic's own docs, and the details of each step are in the sections that follow.

  1. Install Claude Code if you have not. Anthropic's setup page gives this for macOS, Linux and WSL: curl -fsSL https://claude.ai/install.sh | bash.
  2. Create a key in the Claude Console at platform.claude.com/settings/keys. The Console shows the full key once, so copy it somewhere safe.
  3. Put it in your shell: export ANTHROPIC_API_KEY="sk-ant-...".
  4. Run claude. In an interactive session Claude Code asks once whether to use the key. Approve it, and your choice is remembered.

To confirm it worked, type /status at the Claude Code prompt. It shows which credential is active. From then on, your usage is billed to the Console organization that owns the key, at API token rates, and your Pro or Max limits do not apply to it.

Two things catch people out. First, the key wins over a subscription login even when you are signed in, which is how people end up paying per token by accident. Second, if you only want the pay-per-token billing and not a key sitting in your environment, there is a keyless Console sign-in (covered below). If you would rather not set any of this up on your own laptop, you can launch Claude Code on a computer of its own from the Claude Code page on Hivra, and the Hivra section further down explains how sign-in works there.

Subscription, Console API key or cloud provider: which should you use?

Claude Code accepts several kinds of credential, and the choice decides who bills you and how. Anthropic's authentication page lists claude.ai plans (Pro, Max, Ultra, Team, Enterprise), Claude Console, and cloud providers such as Amazon Bedrock, Google Cloud's Agent Platform (formerly Vertex AI) and Microsoft Foundry. The three you will weigh most often compare like this.

Claude subscriptionClaude Console API keyCloud provider
How you sign in/login with your claude.ai accountANTHROPIC_API_KEY, or a Console sign-in with /loginEnvironment variables such as CLAUDE_CODE_USE_BEDROCK=1, or the 3rd-party platform option at the login prompt
How you payYour plan, with usage included up to its limitsPay as you go per token, billed to the Console organizationPay as you go through your cloud account
What limits youPlan usage limitsYour Console balance, spend limits and rate limitsYour cloud account's quotas and policies
Best forSteady daily use by one personHeavy sprints, automation, or an organization that wants API billingTeams that already run on that cloud
Watch out forHitting the limit mid-taskA forgotten key in your shell profile billing the wrong accountExtra setup; some features that need a claude.ai login are not available

Where do you get a Claude Code API key?

From the Claude Console, which is separate from your claude.ai chat account. Anthropic's get-your-key page gives these steps:

  1. Sign in at platform.claude.com, or create an account.
  2. Open the API keys page at platform.claude.com/settings/keys.
  3. Click Create key. If the dialog offers other ways to connect first, choose the API key option. Name the key and pick its settings: when it expires (you cannot change this later), which account it is linked to, and which workspace it works in.
  4. Copy the key and store it. The Console shows it only once. If you lose it, create a new one.

Three ways to put the key to work

1. The environment variable. This is the one in the steps above. It is the simplest and the one that surprises people, because it overrides your subscription for as long as it is set. Set it for one terminal only, or add it to your shell profile if you want it permanent. Anthropic's support article gives both versions, and warns about forgetting a key you put in a profile.

2. A Console sign-in. Run /login inside Claude Code and choose the Anthropic Console account, or run claude auth login --console in your shell, which the CLI reference describes as signing in with Anthropic Console for API usage billing instead of a Claude subscription. Since Claude Code v2.1.242, the prompt then asks how to sign in. The option labeled recommended signs you in with your Console account and creates no API key, storing an OAuth login instead. The option labeled legacy creates a Console API key for you and stores it. Anthropic says to unset ANTHROPIC_API_KEY before using the keyless sign-in.

3. A helper script. If your key comes out of a vault or rotates, point the apiKeyHelper setting at a script that prints it. The next section covers it.

Which credential does Claude Code use when you have more than one?

This is the part most guides skip. Anthropic documents a strict order. When several credentials exist, Claude Code picks the first one in this list:

  1. Cloud provider credentials, when CLAUDE_CODE_USE_BEDROCK, CLAUDE_CODE_USE_VERTEX or CLAUDE_CODE_USE_FOUNDRY is set.
  2. The ANTHROPIC_AUTH_TOKEN environment variable, sent as a bearer token. It is meant for a gateway or proxy that authenticates that way.
  3. The ANTHROPIC_API_KEY environment variable, sent as the X-Api-Key header.
  4. The output of your apiKeyHelper script.
  5. The CLAUDE_CODE_OAUTH_TOKEN environment variable, a long-lived subscription token for CI.
  6. Anthropic profile and federation credentials, which the ant CLI and Workload Identity Federation use. A profile written by ant auth login ranks here only when you name it in ANTHROPIC_PROFILE; otherwise it ranks below /login.
  7. Subscription OAuth credentials from /login, the default for Pro, Max, Ultra, Team and Enterprise users.

Your normal subscription login is last, apart from that profile case. Any environment variable, helper or provider setting above it beats it. That is why a stray ANTHROPIC_API_KEY in a shell profile, a .env file or a dev container can send your work to the pay-as-you-go meter while you believe you are on your plan. Anthropic's support article puts it plainly: an API key set as an environment variable means you are charged at pay-as-you-go rates through that key's account, even if you are logged in with a subscription or a different Console account.

Claude Code does give you a warning. In interactive mode it asks you once to approve or decline the key. You can change that later with the Use custom API key toggle in /config, which only appears while the variable is set. But non-interactive runs skip the question: in claude -p mode, the key is always used when present.

How to check which one is active, and switch back

Run /status inside Claude Code. It shows the active method, and Anthropic says that when a login and an API key are both configured it marks the credential that is not in use. If you need a check from outside a session, claude auth status prints the authentication state as JSON, or as readable text with --text, and exits with code 0 when you are logged in and 1 when you are not.

To go back to your subscription:

  1. Run unset ANTHROPIC_API_KEY in the current shell.
  2. Delete the export line from your shell profile (~/.zshrc, ~/.bashrc) so it does not return, and check any .env file your tools load.
  3. Relaunch claude. Run /login if you have not signed in with your subscription yet.
  4. Run /status again to confirm.

To start clean, type /logout at the prompt. In a shell, claude auth logout does the same without opening Claude Code.

If you keep a work account and a personal account, give each its own configuration directory with CLAUDE_CONFIG_DIR. Anthropic's example is an alias such as alias claude-work='CLAUDE_CONFIG_DIR=~/.claude-work claude'. Each directory has its own settings, session history, and login or API key. One exception from the docs: two Console sign-ins made without an API key are not kept apart this way, because Claude Code stores that kind of sign-in outside the configuration directory.

What is apiKeyHelper, and when do you need it?

apiKeyHelper is a setting that runs a command of your choosing and sends its output as the credential. Claude Code runs it through the system shell, and Anthropic's settings reference says to use it for dynamic or rotating credentials, such as short-lived tokens fetched from a vault. It goes in settings.json:

{ "apiKeyHelper": "/bin/generate_temp_api_key.sh" }

Claude Code caches the result and reruns the command after five minutes by default, or after the interval in CLAUDE_CODE_API_KEY_HELPER_TTL_MS (in milliseconds). It also reruns it when a request fails with 401 or 403. If the helper takes more than 10 seconds, Claude Code shows a warning in the prompt bar.

Two warnings. The script must print only the key to stdout and exit with code 0. Anthropic's errors page says a banner or a log line mixed into the output makes it unusable. And because the helper ranks above a saved login, running /login does not fix a failing helper while the setting exists. Remove the setting or fix the script, then check /status, where the apiKeyHelper row shows Failing with the last error.

How do you use an API key in CI and headless runs?

Non-interactive use is where an API key is most natural. claude -p runs a prompt and exits, and it always uses ANTHROPIC_API_KEY when one is present. For scripted calls, Anthropic's headless page recommends adding --bare, which skips auto-discovery of hooks, skills, custom commands, subagents, plugins, MCP servers, auto memory and CLAUDE.md, so the run starts faster and does not load whatever is configured on the host. Bare mode never reads your subscription login, so you must supply a key:

ANTHROPIC_API_KEY=... claude --bare -p "Summarize README.md" --allowedTools "Read"

That example is Anthropic's own, with the key set in the environment first. In bare mode you can use either ANTHROPIC_API_KEY or an apiKeyHelper passed in the --settings JSON.

In GitHub Actions, store the key as a repository secret named ANTHROPIC_API_KEY and pass it to the action with anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}, as Anthropic's GitHub Actions page shows. Anthropic's warning on that page is blunt: never commit API keys or OAuth tokens to your repository.

If you want a script to run on your subscription instead, Anthropic documents claude setup-token. It opens the same browser flow as /login and prints a one-year OAuth token, which you set as CLAUDE_CODE_OAUTH_TOKEN. It needs a Pro, Max, Ultra, Team or Enterprise plan, it cannot start Remote Control sessions, and bare mode does not read it.

What about Bedrock, Vertex AI and gateways?

These are the third option in the table. For Amazon Bedrock, you set CLAUDE_CODE_USE_BEDROCK=1 and, if your AWS profile does not already have one, an AWS_REGION. For Google Cloud's Agent Platform you set CLAUDE_CODE_USE_VERTEX=1, a CLOUD_ML_REGION and ANTHROPIC_VERTEX_PROJECT_ID. At the login prompt you can also choose 3rd-party platform, which starts a setup wizard for Bedrock and Agent Platform. Billing and quotas then sit with your cloud account, not the Console. Follow Anthropic's Bedrock and Google Cloud pages for the full steps, because they differ by region and model.

An LLM gateway is a proxy your organization runs between Claude Code and the provider. You point Claude Code at it with ANTHROPIC_BASE_URL and give it a credential, usually through ANTHROPIC_AUTH_TOKEN or apiKeyHelper. Anthropic's gateway page spells out a trap worth knowing: setting only the base URL does not replace your subscription. Requests still go through the gateway, but a saved claude.ai login stays the active credential, so its limits and billing apply. A gateway credential variable or apiKeyHelper is what actually swaps the billing. Anthropic does not endorse or audit third-party gateway products, and it does not support routing Claude Code to non-Claude models through a gateway.

What does using an API key cost?

Anthropic's costs page says Claude Code charges by API token consumption, and that per-developer cost varies widely with model choice, codebase size and usage patterns such as running several instances or automation. Its published benchmark for enterprise deployments is an average of about $13 per developer per active day and $150 to $250 per developer per month, with costs staying below $30 per active day for 90% of users. Treat that as a wide range, not a quote. Anthropic recommends starting with a small pilot to find your own baseline.

Per-token prices change with model and date, so check Anthropic's pricing page rather than a blog. For plan prices, see claude.com/pricing. Our Pro vs Max guide turns both into a break-even comparison, and what an AI agent costs per month in API tokens covers how to cut the bill.

Two features help you stay in control. Type /usage to see your current session's token usage. Claude Code computes the dollar figure locally from token counts at list price, so Anthropic calls it an estimate and points to the Console for authoritative billing. And in the Console you can set workspace spend limits and view cost and usage reporting. Anthropic notes that on a subscription, the session cost figure in /usage is not relevant to billing.

How do you keep a Claude Code API key safe?

A key is a secret that spends your money. Anthropic's best-practices article compares it to a credit card number: whoever has it can run up charges on your account. The advice, in short:

  • Never share it, and never paste it into public forums, emails or support tickets.
  • Keep it in an environment variable or a secrets manager, never in code or config files committed to a repository. If you use a .env file locally, add it to .gitignore.
  • In CI, use the platform's encrypted secrets.
  • Be careful about handing a key to third-party tools, because you are giving the tool's developer access to your Console account.
  • Watch the Logs and Usage pages in the Console. Set spend limits on the billing page if your organization has custom rate limits, or turn on auto-reload with a limit you are comfortable with if it uses standard rate limits.
  • If a key leaks, delete it in the Console at platform.claude.com/settings/keys and create a new one.

Troubleshooting: wrong account billed, key ignored, auth errors

These are the messages people hit most, with the fix Anthropic's errors page gives for each.

What you seeLikely causeWhat to do
Credit balance is too lowYour Console organization has no credit, or Claude Code is sending a key when you meant to use your subscriptionRun /status and check the API key row. Unset ANTHROPIC_API_KEY and remove it from your shell profile, or add credits in the Console billing page
Invalid API key or Fix external API keyThe key is mistyped, revoked, or a stale one was loaded from .env, direnv or an IDE terminalCheck the key in the Console, list your ANTHROPIC variables with env and look for a stale one, or unset it and run /login
Your ANTHROPIC_API_KEY belongs to a disabled organizationAn old key from a disabled Console organization overrides your subscriptionUnset the variable and relaunch. If you have no saved login, run /login or use a key from an active organization
Your organization has disabled API key authenticationA Console admin turned off key authenticationUnset ANTHROPIC_API_KEY or remove apiKeyHelper, then /login, or ask the admin to re-enable it
Your organization has disabled Claude subscription access for Claude CodeA server-side organization settingAsk the admin to enable access, or use a Console API key instead
Your apiKeyHelper script is failingThe script exited with an error, printed nothing, or printed more than the keyRun the script directly, fix it so it prints only the key, and check /status

If Claude Code seems to ignore your key, in an interactive session, check whether you declined the approval prompt. The Use custom API key toggle in /config controls that choice. The opposite problem, a key that is used when you did not want it, is the more common one, and the fix is always the same: unset the variable, relaunch, and confirm with /status.

Using Claude Code on Hivra: your own sign-in, or Hivra credits

Not everyone wants to run Claude Code on a laptop that sleeps. Hivra gives each agent a private cloud computer of its own, and you reach it from a browser on any device. The first reason to look at it is that you are not locked into one agent: the same Hivra account launches Hermes, OpenClaw, Claude Code, Codex and Agent Zero, each on a computer of its own. If you try Claude Code on an API key this week and want to compare it with Codex next week, you do not need a new account or a new server.

For Claude Code in particular, Hivra's agent page says you use the official Anthropic CLI and sign in with your own Anthropic account on the computer, as you would locally, subject to Anthropic's terms. Hivra hosting does not include or replace your Anthropic subscription or model usage entitlement. Or you can choose Hivra credits when you launch, and Claude Code then runs on Hivra credits instead. That is a Hivra choice at launch, not an Anthropic key, and it is not described here as a way to get an Anthropic API key.

Your repository, files and sign-in live on that computer, so you come back to the same workspace from your phone, a tablet or another laptop. Open the computer's Terminal tab and run claude, the same as on your own machine.

Paid Hivra plans start at $9.99 a month for 2 vCPU and 4 GB of RAM. Anthropic's charges, whether a plan or an API key, are separate and go to Anthropic. Hivra is independent and is not affiliated with or endorsed by Anthropic.

Hivra is not always the right choice. If you only work at one desk and your laptop stays on, running Claude Code locally costs nothing extra beyond Anthropic's bill. If you only want your Claude plan to carry on a task from the web or a phone, Anthropic's own cloud sessions use your subscription directly. And if your company mandates Bedrock, Vertex AI or a gateway, set that up as your admin tells you. Hivra fits when you want a computer that is yours, reachable from anywhere, and not tied to one agent.

Which setup should you pick?

Pick a subscription if you code most days and want a predictable monthly cost. Pick a Console API key if you hit plan limits, run automation, or want per-token billing you can cap with workspace spend limits. Pick a cloud provider when your company already runs on one. Whichever you choose, set it on purpose and confirm it with /status, because the most expensive mistake here is a key you forgot you had.

And if you want somewhere permanent for Claude Code to work, with the option to bring in other agents later, launch it on Hivra and sign in with your own Anthropic account.

Common questions

Does Claude Code use my API key or my subscription?

If ANTHROPIC_API_KEY is set and you approved it, Claude Code uses the key, even when you are signed in with a Pro or Max plan. In `claude -p` mode it always uses the key when present. Run `/status` to see which credential is active.

How do I stop Claude Code charging my API key instead of my subscription?

Run `unset ANTHROPIC_API_KEY`, remove the export line from your shell profile and any `.env` file, relaunch `claude`, run `/login` if needed, and confirm with `/status`. Anthropic's support article recommends keeping the variable unset if you want to use your subscription.

Do I need an API key to use Claude Code?

No. You can sign in with a Claude Pro, Max, Ultra, Team or Enterprise account, or with a Console account. Anthropic's docs also describe a Console sign-in that creates no API key. A key is one option among several.

Where do I find my Claude Code API key?

Create it in the Claude Console on the API keys page at platform.claude.com/settings/keys. The Console shows the full key only once, so copy it when you create it. If you lose it, create a new one. Your claude.ai chat account is separate from the Console.

Is Claude Code with an API key cheaper than Pro or Max?

It depends on how much you use it. API keys bill per token, plans bill a monthly price with limits. Anthropic's costs page gives an enterprise average of about $13 per developer per active day, but your own figure will differ. See our Pro vs Max guide for the break-even maths.

Can I use apiKeyHelper instead of ANTHROPIC_API_KEY?

Yes. Set the apiKeyHelper setting to a script that prints your key. It is meant for rotating or vault-issued credentials. Claude Code caches the result for five minutes by default and reruns the script on a 401 or 403. The helper ranks below ANTHROPIC_API_KEY in the precedence list.

How do I use an API key for Claude Code in CI?

Store the key as a secret, set it as ANTHROPIC_API_KEY, and run `claude -p`. Anthropic recommends `--bare` for scripted calls, which skips auto-discovery and does not read a subscription login. In GitHub Actions, pass the secret as `anthropic_api_key`.

What does Credit balance is too low mean in Claude Code?

Either your Console organization has run out of prepaid credits, or Claude Code is sending a Console API key when you meant to use your subscription. Check the API key row in `/status`, unset the key if you did not mean to use it, or add credits in the Console.

Can I use an API key with Claude Code on Hivra?

Hivra's Claude Code page says you sign in with your own Anthropic account on the computer, or choose Hivra credits at launch. It does not say Hivra stores or manages an Anthropic API key for you, so follow Anthropic's documented credential rules in the CLI itself and check `/status`.

Give your agent a computer that stays on.

From $9.99/mo. Bring your own AI key. 7-day money-back guarantee on card payments.

Start Now