Aident AI

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:
Stop unattended Claude Code jobs so they cannot create more API usage.
Run
/statusinside Claude Code and record the active authentication method.Close Claude Code.
Remove
ANTHROPIC_API_KEYfrom the current process environment without printing its value.Remove the unintended persistent source, such as a shell profile, IDE launcher,
settings.json, service, or CI job.Start a new Claude Code process and run
/statusagain.
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
/statuswithout 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:
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:
To list related variable names while removing their values from the output:
On Windows PowerShell:
Check whether Windows also stores a persistent user or machine value without displaying it:
In Command Prompt:
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:
On Windows PowerShell:
On Command Prompt:
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:
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
EnvironmentandEnvironmentFileentries;macOS launch agents and launch daemons;
Windows Task Scheduler actions and wrapper scripts;
Docker Compose
environmentandenv_fileentries;CI workflow
env, repository secrets, and organization secrets; andIDE 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:
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:
Stop the process or automation creating requests.
Record the UTC date range, affected workspace, key name or ID, and usage totals without copying the secret.
Review Claude Console usage and billing data for the same window.
If the key may be exposed or used by an unknown process, delete it in Claude Console and rotate the application deliberately.
Add a lower workspace spend limit or notification where your plan supports it.
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:
Copy one exact read-only Action name, inspect its schema, and preflight a narrow input before execution:
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 |
|---|---|---|
|
| 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 |
Interactive use is fixed but | The scheduler, service, container, or CI environment still contains the key | Inspect that exact job boundary and test one bounded invocation |
Removing | 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 |
| A stale key or custom credential helper still overrides login | Check |
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
Manage API key environment variables in Claude Code, Anthropic, updated May 5, 2026
Environment variables, Anthropic, accessed August 3, 2026
Authentication, Anthropic, accessed August 3, 2026
Error reference, Anthropic, accessed August 3, 2026
Use Claude Code with your Pro or Max plan, Anthropic, accessed August 3, 2026
Workspaces, Anthropic, accessed August 3, 2026
What should I do if I suspect my API key has been compromised?, Anthropic, accessed August 3, 2026
Silent
ANTHROPIC_API_KEYprecedence shadows Max subscription auth, GitHub issue, April 29, 2026Environment API key routed sessions to API billing despite active Max subscription, GitHub issue, July 17, 2026
Claude Code subprocess uses API key auth instead of subscription, GitHub issue, July 27, 2026
Warning for those that haven't experienced this yet, Reddit, August 2, 2026
Albatross provider-authentication boundary, X, August 3, 2026


