Environment Variables
Complete reference for Claude Code environment variables and configuration options
title: Environment Variables description: Complete reference for Claude Code environment variables and configuration options
Claude Code uses environment variables to configure API connections, behavior settings, and integrations. This guide covers all available environment variables and best practices for managing them.
Required Variables
ANTHROPIC_API_KEY
The primary API key for authenticating with Claude.
export ANTHROPIC_API_KEY="sk-ant-api03-..."
Notes:
- Required for all Claude Code operations
- Obtain from console.anthropic.com
- Never commit to version control
- Use project-specific keys when possible
Optional Variables
Model Selection
ANTHROPIC_MODEL
Override the default model used by Claude Code.
export ANTHROPIC_MODEL="claude-sonnet-4-20250514"
Available models:
claude-opus-4-20250514- Most capable, best for complex tasksclaude-sonnet-4-20250514- Balanced speed and capability (default)claude-3-5-haiku-20241022- Fastest, best for simple tasks
API Configuration
ANTHROPIC_BASE_URL
Custom API endpoint for proxies or alternative backends.
export ANTHROPIC_BASE_URL="https://your-proxy.example.com/v1"
Use cases:
- Corporate proxies
- Self-hosted Claude instances
- API gateways with custom authentication
ANTHROPIC_TIMEOUT
Request timeout in milliseconds.
export ANTHROPIC_TIMEOUT="120000" # 2 minutes
Default: 60000 (1 minute)
Behavior Settings
CLAUDE_CODE_MAX_TOKENS
Maximum tokens for Claude responses.
export CLAUDE_CODE_MAX_TOKENS="8192"
Higher values allow longer responses but increase costs.
CLAUDE_CODE_AUTO_APPROVE
Comma-separated list of tools to auto-approve.
export CLAUDE_CODE_AUTO_APPROVE="Read,Glob,Grep"
Reduces confirmation prompts for read-only operations.
Security
CLAUDE_CODE_DISABLE_TELEMETRY
Disable anonymous usage telemetry.
export CLAUDE_CODE_DISABLE_TELEMETRY="1"
Git Integration
CLAUDE_CODE_GIT_AUTHOR
Override the git author for commits made by Claude.
export CLAUDE_CODE_GIT_AUTHOR="Claude Code <claude@example.com>"
Setting Environment Variables
Shell Configuration
Add to your shell profile for persistence:
Bash (~/.bashrc or ~/.bash_profile):
export ANTHROPIC_API_KEY="your-key-here"
Zsh (~/.zshrc):
export ANTHROPIC_API_KEY="your-key-here"
Fish (~/.config/fish/config.fish):
set -x ANTHROPIC_API_KEY "your-key-here"
Project-Specific Variables
Create a .env file in your project root (never commit):
# .env
ANTHROPIC_API_KEY=sk-ant-api03-...
ANTHROPIC_MODEL=claude-sonnet-4-20250514
Add to .gitignore:
.env
.env.local
.env.*.local
Using direnv
Automatically load environment variables when entering a directory:
- Install direnv:
brew install direnv # macOS
- Add to shell:
eval "$(direnv hook bash)" # or zsh, fish
- Create
.envrc:
export ANTHROPIC_API_KEY="your-key-here"
- Allow the directory:
direnv allow
Environment Variable Precedence
Claude Code loads variables in this order (later overrides earlier):
- System environment
- Shell profile (~/.bashrc, ~/.zshrc)
- Project
.envfile .env.localfile- Command-line exports
Validating Configuration
Check if your API key is set:
echo $ANTHROPIC_API_KEY | head -c 20
# Should show: sk-ant-api03-...
Test the connection:
claude "Hello, are you working?"
Troubleshooting
API Key Not Found
Symptom: "ANTHROPIC_API_KEY not set" error
Solutions:
-
Verify the variable is exported (not just set):
Bashexport ANTHROPIC_API_KEY="..." # Correct ANTHROPIC_API_KEY="..." # Wrong - not exported -
Source your profile after changes:
Bashsource ~/.zshrc -
Check for typos in the variable name
Invalid API Key
Symptom: 401 Unauthorized errors
Solutions:
- Verify the key at console.anthropic.com
- Check for extra whitespace or quotes
- Ensure the key starts with
sk-ant-api03-
Model Not Available
Symptom: "Model not found" error
Solutions:
- Check model name spelling
- Verify your API plan includes the model
- Use the default model (unset ANTHROPIC_MODEL)
Security Best Practices
-
Never commit API keys - Use
.envfiles and.gitignore -
Use project-specific keys - Create separate keys for different projects in the Anthropic console
-
Rotate keys regularly - Especially if you suspect exposure
-
Use environment managers - Tools like direnv, dotenv, or 1Password CLI
-
Audit access - Monitor API usage in the Anthropic console
-
Limit permissions - Use the minimum required API access level