| name | direnv |
| description | Set up direnv/.envrc for per-project Claude Code environments — switch API keys, providers, models, and feature flags automatically when entering a directory. Use when the user wants to manage multiple Claude accounts, switch between Anthropic/Bedrock/Vertex per project, or configure per-directory environment variables. |
| allowed-tools | Bash, Read, Write, Edit, Glob |
direnv — Per-Project Claude Code Environments
Set up .envrc files so Claude Code automatically loads the right API keys, provider, model, and feature flags when you cd into a project directory.
Prerequisites
Check that direnv is installed and hooked into the shell:
which direnv && direnv version
Check the shell hook is active:
grep -q 'direnv' ~/.zshrc ~/.bashrc 2>/dev/null && echo "hook found" || echo "hook missing"
If the hook is missing, add it:
- zsh:
echo 'eval "$(direnv hook zsh)"' >> ~/.zshrc
- bash:
echo 'eval "$(direnv hook bash)"' >> ~/.bashrc
Creating a .envrc
Ask the user which scenario they need, then generate the .envrc:
Direct API Key (personal/hobby)
export ANTHROPIC_API_KEY="sk-ant-..."
AWS Bedrock
export CLAUDE_CODE_USE_BEDROCK=1
export AWS_PROFILE=my-bedrock-profile
export AWS_REGION=us-east-1
Google Vertex AI
export CLAUDE_CODE_USE_VERTEX=1
export CLOUD_ML_REGION=us-east5
export ANTHROPIC_VERTEX_PROJECT_ID=my-project-id
Microsoft Azure Foundry
export CLAUDE_CODE_USE_FOUNDRY=1
export ANTHROPIC_FOUNDRY_BASE_URL="https://my-resource.services.ai.azure.com/api"
export ANTHROPIC_FOUNDRY_API_KEY="..."
Feature Flags / Tuning
export ANTHROPIC_MODEL="claude-opus-4-6"
export CLAUDE_CODE_MAX_OUTPUT_TOKENS=64000
export CLAUDE_CODE_EFFORT_LEVEL=high
export CLAUDE_CODE_AUTOCOMPACT_PCT_OVERRIDE=99
Security Checklist
After creating the .envrc:
- Allow it:
direnv allow (required after every edit)
- Gitignore it: Verify
.envrc is in .gitignore — it may contain secrets
grep -q '.envrc' .gitignore 2>/dev/null || echo '.envrc' >> .gitignore
- Never commit API keys — if the project is shared, use
direnv allow locally and keep .envrc out of version control.
Verifying
After setup, verify the environment loads:
cd /path/to/project
direnv allow
env | grep -E 'ANTHROPIC|CLAUDE_CODE|AWS_PROFILE|CLOUD_ML'
Then start claude and confirm the right provider/model is active.
Common Patterns
Layered configs with .envrc.local
export CLAUDE_CODE_EFFORT_LEVEL=high
export CLAUDE_CODE_MAX_OUTPUT_TOKENS=64000
source_env_if_exists .envrc.local
export ANTHROPIC_API_KEY="sk-ant-..."
Switching between projects
No action needed — direnv automatically loads/unloads when you cd. Just set up each project's .envrc once.
Reference: Key Claude Code Env Vars
| Variable | Purpose |
|---|
ANTHROPIC_API_KEY | Direct API authentication |
ANTHROPIC_MODEL | Model selection |
CLAUDE_CODE_USE_BEDROCK | Enable AWS Bedrock provider |
CLAUDE_CODE_USE_VERTEX | Enable Google Vertex provider |
CLAUDE_CODE_USE_FOUNDRY | Enable Azure Foundry provider |
CLAUDE_CODE_EFFORT_LEVEL | low / medium / high |
CLAUDE_CODE_MAX_OUTPUT_TOKENS | Max output tokens (up to 64000) |
CLAUDE_CODE_AUTOCOMPACT_PCT_OVERRIDE | Auto-compaction threshold (1-100) |
CLAUDE_CODE_SUBAGENT_MODEL | Model for subagents |
CLAUDE_CODE_SHELL | Override shell detection |
CLAUDE_CONFIG_DIR | Custom config directory |