ANTHROPIC_API_KEY Not Set in Subprocess (2026)

The Error

Error: ANTHROPIC_API_KEY environment variable is not set
  Subprocess spawned by Claude Code cannot authenticate
  Parent process has the key, but child process does not inherit it
  at ApiClient.validateKey (api-client.js:12)

This appears when a script or tool spawned by Claude Code tries to use the Anthropic API but cannot access the API key because it was not exported to child process environments.

The Fix

export ANTHROPIC_API_KEY="sk-ant-..."
  1. Ensure the API key is exported (not just set) so child processes inherit it.
  2. Add the export to your ~/.zshrc or ~/.bashrc for persistence.
  3. Verify it propagates: env | grep ANTHROPIC_API_KEY.

Why This Happens

In Unix shells, variables set with VAR=value are local to the current shell. Only export VAR=value makes them available to child processes. If you set ANTHROPIC_API_KEY without export in a shell config file, or if you set it in a parent shell but the child process (like a Python script calling the Anthropic SDK) starts in a new environment, the key is not inherited. Docker containers, CI runners, and subshells each have their own environment.

If That Doesn’t Work

Check if the key is actually exported:

env | grep ANTHROPIC
# vs
set | grep ANTHROPIC  # shows local vars too

Pass the key explicitly to a subprocess:

ANTHROPIC_API_KEY="$ANTHROPIC_API_KEY" python script.py

For Docker containers, pass it as a build arg or runtime env:

docker run -e ANTHROPIC_API_KEY="$ANTHROPIC_API_KEY" my-image

Check for shadowed variable names:

grep -r "ANTHROPIC_API_KEY" ~/.zshrc ~/.bashrc ~/.profile

Prevention

# CLAUDE.md rule
Ensure ANTHROPIC_API_KEY is exported (not just set) in your shell profile. Verify with 'env | grep ANTHROPIC_API_KEY'. For Docker and CI, pass the key explicitly via -e flag or secrets.

See Also

This fix also applies if you see variations of this error:

  • Connection or process errors with similar root causes in the same subsystem
  • Timeout variants where the operation starts but does not complete
  • Permission variants where access is denied to the same resource
  • Configuration variants where the same setting is missing or malformed

If your specific error message differs slightly from the one shown above, the fix is likely the same. The key indicator is the operation that failed (shown in the stack trace) rather than the exact wording of the message.

Frequently Asked Questions

Does this error affect all operating systems?

This error can occur on macOS, Linux, and Windows (WSL). The exact error message may differ slightly between platforms, but the root cause and fix are the same. macOS users may see additional Gatekeeper or notarization prompts. Linux users should check that the relevant system packages are installed. Windows users should ensure they are running inside WSL2, not native Windows.

Will this error come back after updating Claude Code?

Updates can occasionally reintroduce this error if the update changes default configurations or dependency requirements. After updating Claude Code, verify your project still builds and runs correctly. If the error returns, reapply the fix and check the changelog for breaking changes.

Can this error cause data loss?

No, this error occurs before or during an operation and does not corrupt existing files. Claude Code’s edit operations are atomic — they either complete fully or not at all. However, if the error occurs during a multi-step operation, you may have partial changes that need to be reviewed with git diff before continuing.

How do I report this error to Anthropic if the fix does not work?

Open an issue at github.com/anthropics/claude-code with: (1) the full error message including stack trace, (2) your Node.js version (node --version), (3) your Claude Code version (claude --version), (4) your operating system and version, and (5) the command or operation that triggered the error.

Try it: Paste your error into our Error Diagnostic for an instant fix.