Claude Code Using API Billing Instead of Max? Fix ANTHROPIC_API_KEY

Claude Code Using API Billing Instead of Max? Fix ANTHROPIC_API_KEY

Aident AI

An amber credential capsule is lifted from an exposed metered route into a sealed teal vault beside a stable blue subscription path.

Claude Code Using API Billing Instead of Max? Fix ANTHROPIC_API_KEY

If Claude Code is charging API credits while you have a Pro, Max, Team, or Enterprise subscription, check whether ANTHROPIC_API_KEY is present in the environment that launched Claude Code. Anthropic documents that this variable takes precedence over subscription login after approval. In non-interactive claude -p runs, the key is always used when present.

The fastest safe fix is:

  1. Stop unattended Claude Code jobs so they cannot create more API usage.

  2. Run /status inside Claude Code and record the active authentication method.

  3. Close Claude Code.

  4. Remove ANTHROPIC_API_KEY from the current process environment without printing its value.

  5. Remove the unintended persistent source, such as a shell profile, IDE launcher, settings.json, service, or CI job.

  6. Start a new Claude Code process and run /status again.

Expected result: /status identifies your Claude subscription rather than an API key or Console billing source. Logging out and back in is not enough if the higher-priority environment variable is still present.

Match the Exact Problem

This guide applies when one or more of these are true:

  • Claude Console shows API usage that you expected your subscription to cover.

  • Claude Code shows an API key or Console billing source in /status.

  • You are logged in to Max or Pro, but receive Credit balance is too low, Invalid API key, or an organization-disabled error.

  • A headless claude -p, cron, systemd, CI, or Task Scheduler job consumes API credits.

  • The issue started after testing an Anthropic SDK, proxy, gateway, local model, or third-party provider.

Do not assume that a .env file merely existing on disk changes Claude Code authentication. The key must reach the Claude Code process through a loader, shell, IDE, settings file, wrapper, service, or automation environment. That distinction matters because deleting an unrelated project file may break your app without fixing the process that launched Claude Code.

Use a different diagnosis when /status already shows the expected subscription and the charge is extra usage you explicitly enabled. If the problem is unusually fast plan consumption rather than API billing, use the separate Claude Code usage-limit diagnostic.

Prerequisites

Before changing credentials:

  • stop scheduled and background Claude Code runs;

  • note the UTC time when unexpected API usage began;

  • capture /status without exposing a token;

  • identify which application still needs the API key; and

  • make sure you can sign in to the intended Claude subscription again.

Never paste the key into a support ticket, chat, screenshot, issue, or shell-history example. The commands below report whether a variable exists, not its value.

Step 1: Confirm the Active Billing Route

Inside the affected Claude Code session, run:

/status

Record the authentication and account information shown. Then exit that session. A running process keeps the environment it inherited at launch, so editing a profile or system setting does not repair an already-running Claude Code process.

If an automated run is involved, stop the scheduler, service, or CI job before continuing. A correct interactive terminal does not prove that a separate background environment is correct.

Expected result: you know whether the affected process used subscription OAuth, an API key, or another provider route.

Step 2: Check the Current Environment Without Printing the Secret

On macOS, Linux, or WSL, run this outside Claude Code:

if printenv ANTHROPIC_API_KEY >/dev/null; then
  printf 'ANTHROPIC_API_KEY is set\n'
else
  printf 'ANTHROPIC_API_KEY is unset\n'
fi

To list related variable names while removing their values from the output:

env | sed 's/=.*//' | grep -E '^ANTHROPIC_(API_KEY|AUTH_TOKEN|BASE_URL)$'

On Windows PowerShell:

if (Test-Path Env:ANTHROPIC_API_KEY) {
  'ANTHROPIC_API_KEY is set'
} else {
  'ANTHROPIC_API_KEY is unset'
}

Check whether Windows also stores a persistent user or machine value without displaying it:

[pscustomobject]@{
  User = [bool][Environment]::GetEnvironmentVariable('ANTHROPIC_API_KEY', 'User')
  Machine = [bool][Environment]::GetEnvironmentVariable('ANTHROPIC_API_KEY', 'Machine')
}

In Command Prompt:

if defined ANTHROPIC_API_KEY (echo ANTHROPIC_API_KEY is set) else (echo ANTHROPIC_API_KEY is unset)

Avoid echo $ANTHROPIC_API_KEY, Get-ChildItem Env:ANTHROPIC_API_KEY, or diagnostic dumps that include environment values. They can place the full key in terminal history, logs, recordings, or agent context.

Expected result: you know whether the current shell can pass the API key to a child process, without revealing the credential.

Step 3: Test a Clean One-Process Launch

On macOS, Linux, or WSL, launch one process with the API key removed:

env -u ANTHROPIC_API_KEY claude

On Windows PowerShell:

Remove-Item Env:ANTHROPIC_API_KEY -ErrorAction SilentlyContinue
claude

On Command Prompt:

set "ANTHROPIC_API_KEY="
claude

Run /status in the new session. If necessary, use /login and choose the Claude subscription account, then run /status again.

Expected result: the new process uses the subscription. If it still shows API billing, another configuration layer is supplying the key after launch.

Step 4: Remove the Persistent Source

Change only the source that is not supposed to authenticate Claude Code. Your application can still use an API key through an isolated process or secret manager.

Shell profiles

Find profile files that name the variable without printing the matching line:

grep -l 'ANTHROPIC_API_KEY' \
  ~/.zshrc ~/.zprofile ~/.bashrc ~/.bash_profile ~/.profile 2>/dev/null

Open each reported file in your editor, remove the unintended export, save it, and start a new terminal. Do not use a broad search-and-delete command against your home directory.

Claude settings and project launchers

Claude Code settings can define variables under an env key. Check reviewed user, project, and local settings files for the variable name. Also inspect aliases, wrapper scripts, IDE run configurations, direnv, dotenv shell plugins, and terminal profiles.

If your application needs the Anthropic SDK, prefer an application-specific name such as MY_APP_ANTHROPIC_API_KEY, then map it to the SDK only inside that application's process. Do not export a generic provider key across every terminal just for convenience.

Windows user or machine variables

Open System Properties > Advanced > Environment Variables. Remove ANTHROPIC_API_KEY from the unintended User or System scope, then fully restart the terminal and IDE. A process that was already open can retain the old value.

Services, containers, and CI

Inspect the exact environment used by the failing job:

  • systemd unit Environment and EnvironmentFile entries;

  • macOS launch agents and launch daemons;

  • Windows Task Scheduler actions and wrapper scripts;

  • Docker Compose environment and env_file entries;

  • CI workflow env, repository secrets, and organization secrets; and

  • IDE tasks that launch claude -p.

Do not remove a production secret merely because Claude Code can see it. First stop the job, decide which process owns the key, and narrow the key's scope. For CI that should use subscription authentication, follow Anthropic's current CLAUDE_CODE_OAUTH_TOKEN guidance and verify that it is permitted for that workflow. For API-backed automation, keep the API key and treat pay-as-you-go billing as intentional.

Expected result: new Claude Code processes no longer inherit an unintended API key, while the application that owns the key still receives it through its own boundary.

Step 5: Verify the Fix Before Resuming Automation

Start Claude Code from the same terminal, IDE, service, or scheduler path that previously produced charges. In an interactive session, run:

/status

Then perform one small, non-sensitive test. Check the Claude Console usage page after its normal reporting delay. Resume broader or unattended work only after both checks agree with the intended billing route.

For headless work, test one bounded invocation before restoring a schedule. A clean login in your everyday terminal does not validate cron, CI, a container, or Task Scheduler.

Expected result: /status shows subscription authentication and no new API usage is attributable to the test. If Console usage still rises, stop and inspect the exact launcher again.

If Unexpected Charges Already Happened

Contain first, then preserve evidence:

  1. Stop the process or automation creating requests.

  2. Record the UTC date range, affected workspace, key name or ID, and usage totals without copying the secret.

  3. Review Claude Console usage and billing data for the same window.

  4. If the key may be exposed or used by an unknown process, delete it in Claude Console and rotate the application deliberately.

  5. Add a lower workspace spend limit or notification where your plan supports it.

  6. Contact Anthropic Support with the account, timestamps, and billing evidence.

Do not promise yourself that removing the variable will reverse prior charges, and do not initiate a chargeback before giving the provider a precise opportunity to investigate. Authentication cleanup prevents new misrouting; billing resolution is a separate process.

Keep API Access Without Recreating the Problem

The goal is not to eliminate API keys from every workload. It is to stop a general-purpose coding-agent process from inheriting credentials it does not need.

Use these boundaries:

  • scope API keys to a dedicated Claude Console workspace;

  • set spend limits and notifications appropriate to that workload;

  • inject the key only into the application process that needs it;

  • use short-lived or rotated credentials where supported;

  • keep API-backed and subscription-backed launchers visibly separate; and

  • verify the active route before unattended execution.

If you also switch between work and personal Claude accounts, follow the separate Claude Code profile guide. Account profiles isolate Claude state, but they do not override a provider key inherited from the parent environment.

For supported integrations, Aident Loadout keeps provider credentials in Vault and exposes typed, task-scoped Actions instead of requiring a raw provider token in a project shell. Verify the broker without printing any secret:

aident account auth status
aident vault vault --action status
aident capabilities search \
  --query "list GitHub repository issues" \
  --limit 5

Copy one exact read-only Action name, inspect its schema, and preflight a narrow input before execution:

aident capabilities get --name '<canonical-action-name>'
aident capabilities preflight \
  --name '<canonical-action-name>' \
  --input '<narrow-read-only-input>'

Expected result: the integration is available through a typed capability while the provider token stays out of the Claude Code environment. For the broader pattern, see How to Give AI Agents API Access Without Exposing Keys.

Common Failure Modes

What you see

Likely cause

Safer response

/status still shows API billing after /login

ANTHROPIC_API_KEY still wins in the process environment or settings

Exit, remove the override at its source, start a new process, and recheck

The terminal is fixed but VS Code is not

The IDE inherited an older environment or has its own setting

Fully quit the IDE, inspect its launcher, restart it, and run /status there

Interactive use is fixed but claude -p still charges API credits

The scheduler, service, container, or CI environment still contains the key

Inspect that exact job boundary and test one bounded invocation

Removing .env changes nothing

The key came from a shell profile, settings file, service, or parent process

Check the effective process environment without printing values

The app breaks after removing the key globally

The application legitimately depended on the same generic variable

Restore it only for that app process under a scoped launcher or secret manager

Invalid API key or organization-disabled errors persist

A stale key or custom credential helper still overrides login

Check /status, settings, helper configuration, and related variable names

Console usage rises after the clean test

Another process or another credential is producing traffic

Stop automation, correlate UTC timestamps, and rotate a suspect key

Why This Fix Works

Claude Code resolves several authentication methods by precedence. Anthropic's current documentation places an approved ANTHROPIC_API_KEY environment variable above subscription OAuth, and non-interactive mode always uses the key when it is present. /login can create valid subscription credentials without changing that higher-priority input.

Removing the unintended variable from the process that launches Claude Code lets authentication fall through to the subscription credential. Removing its persistent source prevents the override from returning in a new terminal, IDE, service, or scheduled run. Keeping application credentials in a narrower process or broker preserves API access without making it ambient authority for every coding-agent session.

Ready to test that boundary? Set up Aident Loadout, inspect one read-only Action schema, and confirm that the provider token is absent from the Claude Code environment.

Sources

Home

Home

Home

Integrations

Integrations

Integrations

Vault

Vault

Vault

Audit

Audit

Audit

Arana Grande

Arana Grande

Arana Grande

Free

Free

Free

30-day audit summary

30-day audit summary

30-day audit summary

Daily action-call volume and the latest receipts from the Loadout audit trail.

Daily action-call volume and the latest receipts from the Loadout audit trail.

Daily action-call volume and the latest receipts from the Loadout audit trail.

View Audit

View Audit

View Audit

Loadout usage

Loadout usage

Loadout usage

617 action calls in the last 30 days

617 action calls in the last 30 days

617 action calls in the last 30 days

May 19 - Jun 17

May 19 - Jun 17

May 19 - Jun 17

10 active days

10 active days

10 active days

Less

Less

Less

More

More

More

Recent activity

Recent activity

Recent activity

Latest action-call receipts from connected agents

Latest action-call receipts from connected agents

Latest action-call receipts from connected agents

Apr 23, 09:23 AM

Apr 23, 09:23 AM

Apr 23, 09:23 AM

Shopify

Shopify

Shopify

Creates Or Updates An Asset For A Theme

Creates Or Updates An Asset For A Theme

Creates Or Updates An Asset For A Theme

Success

Success

Success

Apr 23, 09:21 AM

Apr 23, 09:21 AM

Apr 23, 09:21 AM

Shopify

Shopify

Shopify

Update Products Param Product Id

Update Products Param Product Id

Update Products Param Product Id

Success

Success

Success

Apr 23, 08:53 AM

Apr 23, 08:53 AM

Apr 23, 08:53 AM

Shopify

Shopify

Shopify

Update Products Param Product Id

Update Products Param Product Id

Update Products Param Product Id

Failed

Failed

Failed

Apr 22, 22:13 PM

Apr 22, 22:13 PM

Apr 22, 22:13 PM

Shopify

Shopify

Shopify

Create Product Image

Create Product Image

Create Product Image

Success

Success

Success

Apr 22, 22:12 PM

Apr 22, 22:12 PM

Apr 22, 22:12 PM

Shopify

Shopify

Shopify

Create Product Image

Create Product Image

Create Product Image

Success

Success

Success

Connected integration coverage

Connected integration coverage

Connected integration coverage

162

162

162

of 753 accessible connected

of 753 accessible connected

of 753 accessible connected

Callable actions

Callable actions

Callable actions

1,126

1,126

1,126

Vault credentials

Vault credentials

Vault credentials

8

8

8

Explore what's possible

Explore what's possible

Explore what's possible

See all Integrations

See all Integrations

See all Integrations

Google Ads

Google Ads

Google Ads

All available Goolge Ads tools via...

All available Goolge Ads tools via...

All available Goolge Ads tools via...

X (twitter)

X (twitter)

X (twitter)

All available X tools via...

All available X tools via...

All available X tools via...

Github

Github

Github

All available Github tools via...

All available Github tools via...

All available Github tools via...

Notion

Notion

Notion

All available Notion tools via...

All available Notion tools via...

All available Notion tools via...

Slack

Slack

Slack

All available Slack tools via...

All available Slack tools via...

All available Slack tools via...

Firecrawl

Firecrawl

Firecrawl

All available Firecrawl tools via...

All available Firecrawl tools via...

All available Firecrawl tools via...

753 integrations are available for loadouts.

753 integrations are available for loadouts.

753 integrations are available for loadouts.

Plug your entire stack into your AI agents.

Plug your entire stack into your AI agents.

Plug your entire stack into your AI agents.

Skip the integration headache. Plug 750+ tools into Claude Code, Codex, and OpenClaw in one go, and let your agents execute today.

Skip the integration headache. Plug 750+ tools into Claude Code, Codex, and OpenClaw in one go, and let your agents execute today.