Why is Claude Code billing my API key instead of my subscription?
Because an API key is in the environment the agent runs in. Claude Code's documentation says that when ANTHROPIC_API_KEY is set, it is used instead of your Claude Pro, Max, Ultra, Team or Enterprise subscription even if you are logged in; in interactive mode you are asked once, and in non-interactive mode (claude -p) the key is always used when present. Scheduled agents (cron, launchd, scripts that load a .env, CI) usually run non-interactively, so they bill the key per token without asking.
Where the key usually comes from
- Your shell start-up file (
~/.zshrc,~/.bashrc) exportsANTHROPIC_API_KEY, so everyclaude -pstarted from that shell or its scripts inherits it. - A crontab line or a variable set at the top of the crontab, a launchd
EnvironmentVariablesentry, or a systemdEnvironment=line. - A script that loads a project
.env(for exampleset -a; source .env) which contains the key. - A GitHub Actions workflow that passes
anthropic_api_keytoanthropics/claude-code-action;claude_code_oauth_tokenuses a subscription token instead.
How to switch a run back to your subscription
unset ANTHROPIC_API_KEYin the environment the job runs in, then check/statusin Claude Code, which marks the credential that is not in use.- Remove the key from the job definition or the
.envit loads, or give the job a subscription token instead. - For headless runs that should bill an API key, cap them with
--max-budget-usdand a spend limit on the API workspace.
A worked example
What skillhawk agents prints for scheduled agents (example data):
! cron claude */30 * * * * cd ~/repo && claude -p "sweep" (crontab)
bills: API key, pay per token (ANTHROPIC_API_KEY)
launchd claude com.me.nightly.plist (~/Library/LaunchAgents/com.me.nightly.plist)
bills: subscription token (CLAUDE_CODE_OAUTH_TOKEN)
API keys your shell exports (1)
! ~/.zshrc: ANTHROPIC_API_KEY. Agents started from this shell, including `claude -p` in scripts, bill this key per token, even when you are signed in to a subscription.
The Skillhawk command that helps
Read-only; it prints variable names, never their values:
npx skillhawk agents
It reads your crontab, launchd or systemd user jobs, the scripts and .env files they load, this repository's scheduled GitHub workflows and your shell start-up files.
Questions
Does claude -p use my Max subscription?
Only if no API key is present. Claude Code's documentation says that in non-interactive mode (-p) an ANTHROPIC_API_KEY in the environment is always used when present, instead of your subscription.
How do I know which account a run is using?
Run /status in Claude Code, which marks the credential not in use. For scheduled jobs, check the environment they run in; npx skillhawk agents lists each scheduled agent with the credential it will use.
Sources
- Claude Code docs: environment variables (ANTHROPIC_API_KEY)
"this key is used instead of your Claude Pro, Max, Ultra, Team, or Enterprise subscription" - Claude Code docs: authentication and credentials
- GitHub issue #37686: scheduled claude -p runs inherited an API key ($1,800+ in two days)