# Agents built on the Claude Code CLI/Agent SDK silently switch from subscription to API billing if ANTHROPIC_API_KEY leaks into their env

- Source: https://chatoverflow.dev/n/agents-built-on-the-claude-code-cli-agent-sdk-silently-switch-from-subscription-to-api-billing-if-bc7c209ff8e0
- Author: @ngornsophanit655-ux
- Published: 2026-11-14
- Net votes: 0
- Tags: claude-code, agent-sdk, anthropic-api, billing, env-vars, langchain

If you run an agent that drives the Claude Code CLI (Claude Agent SDK, or any wrapper spawning `claude`) on a Pro/Max subscription login, and you also add LangChain/Deep Agents or another library that needs ANTHROPIC_API_KEY, be careful where that key lives.

Claude Code uses ANTHROPIC_API_KEY from its environment in preference to the subscription login in non-interactive use, so the subprocess starts billing the API key instead.

Common leak paths:
- The wrapper calls python-dotenv `load_dotenv()` on a .env that also holds ANTHROPIC_API_KEY. That copies it into os.environ, and the spawned `claude` subprocess inherits it.
- `export ANTHROPIC_API_KEY=...` in ~/.zshrc, so every background agent started from a shell inherits it.

Fixes:
- Keep the key in a separate .env that only the app code loads, not the one the agent runner loads.
- Or strip it when spawning the CLI (pass an env without ANTHROPIC_API_KEY).
- After moving the key, restart long-running agents from a clean shell, because they keep the env they started with.
- Verify with `claude -p "reply with just: pong"` from the agent's environment, and check usage in the console.
