Claude Code Environment Variables
Environment variables are an important way to control Claude Code's behavior, allowing flexible adjustment of settings without editing config files. This chapter details all environment variables supported by Claude Code, their purposes, configuration methods, and common use cases.
Environment variable overview
Claude Code uses environment variables to control behavior; these can be set in the following ways:
- Directly in the shell
export - in
~/.claude/settings.json'senvconfigured in the field - In project-level
.claude/settings.jsonconfigure - Configure via the IDE plugin's settings interface
Environment variable priority from high to low: command line > project-level settings.json > user-level settings.json > shell environment variables. Variables configured in settings.json are automatically passed to the Claude process.
Authentication-related variables
| Variable Name | Description | Value |
|---|---|---|
ANTHROPIC_API_KEY |
Claude API key (obtained from claude.ai) | API key string |
ANTHROPIC_BASE_URL |
Target address for API requests (for proxies or custom endpoints) | URL address |
ANTHROPIC_AUTH_TOKEN |
Authentication token (for scenarios such as the VS Code plugin) | Token string |
Configuration example
Example
Set in the shell
export
ANTHROPIC_API_KEY
=
"sk-ant-xxxxx"
export
ANTHROPIC_BASE_URL
=
"https://api.anthropic.com"
Example
// Configure in settings.json
{
"env"
:
{
"ANTHROPIC_API_KEY"
:
"sk-ant-xxxxx"
,
"ANTHROPIC_BASE_URL"
:
"https://custom-proxy.com/v1"
}
}
Model configuration variables
| Variable Name | Description | Value |
|---|---|---|
ANTHROPIC_MODEL |
The model used by default | claude-opus-4-5、claude-sonnet-4-5、claude-haiku-3-5 etc. |
ANTHROPIC_SMALL_FAST_MODEL |
Model used in fast response mode (for simple tasks) | Model name |
CLAUDE_CODE_SUBAGENT_MODEL |
Uniformly set the model used by all subagents | Model name |
CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS |
Disable experimental features when using the Anthropic Messages format with Bedrock or Vertex | 1 |
Use Cases
Subagents inherit the main conversation's model by default. Through CLAUDE_CODE_SUBAGENT_MODEL you can set it uniformly, assigning simple tasks to Haiku and complex analysis to Sonnet to optimize cost:
Example
Main conversation uses Opus for complex reasoning, subagents uniformly use Sonnet
export
ANTHROPIC_MODEL
=
"claude-opus-4-5"
export
CLAUDE_CODE_SUBAGENT_MODEL
=
"claude-sonnet-4-5"
Tool and command variables
| Variable Name | Description | Value |
|---|---|---|
CLAUDE_CODE_DISABLE_SLASH_COMMANDS |
Disable all slash commands | 1 |
CLAUDE_CODE_DISABLE_GIT_INSTRUCTIONS |
Disable built-in Git-related system prompts (higher priority than in settings.json includeGitInstructions) |
1 |
CLAUDE_CODE_USE_POWERSHELL_TOOL |
Enable the PowerShell tool on Windows (requires use with defaultShell: "powershell" setting) |
1 |
CLAUDE_CODE_IDE_SKIP_AUTO_INSTALL |
Skip automatic installation of IDE extensions (replaces autoInstallIdeExtension setting) |
1 |
CLAUDE_CODE_DISABLE_BACKGROUND_TASKS |
Disable background task functionality | 1 |
Permission and security variables
| Variable Name | Description | Value |
|---|---|---|
CLAUDE_CODE_PERMISSION_MODE |
Set the default permission mode (see the subagent chapter for details) | default、acceptEdits、dontAsk、bypassPermissions、plan |
CLAUDE_CODE_ALLOWED_TOOLS |
Whitelist of tools Claude may use (comma-separated) | Tool list |
CLAUDE_CODE_DISALLOWED_TOOLS |
Blacklist of tools Claude is forbidden to use | Tool list |
Permission modes explained
default: normal permission prompts, asking before each operationacceptEdits: automatically accept file edits without confirmationdontAsk: automatically reject unauthorized operations without interrupting executionbypassPermissions: skip all permission checks (only in fully trusted environments)plan: read-only planning mode, no write operations
Example
Use read-only mode in CI environments
export
CLAUDE_CODE_PERMISSION_MODE
=
"plan"
export
CLAUDE_CODE_ALLOWED_TOOLS
=
"Read,Grep,Glob,Bash(gh *)"
Logging and debugging variables
| Variable Name | Description | Value |
|---|---|---|
CLAUDE_CODE_DEBUG |
Enable debug output | 1 |
CLAUDE_CODE_ENABLE_TELEMETRY |
Enable telemetry data collection | 1 |
OTEL_METRICS_EXPORTER |
OpenTelemetry metrics exporter | otlp etc. |
Session and history variables
| Variable Name | Description | Value |
|---|---|---|
CLAUDE_CODE_DISABLE_HISTORY |
Disable conversation history saving | 1 |
CLAUDE_CODE_SESSION_TIMEOUT |
Session timeout (seconds) | Digit |
CLAUDE_CODE_MAX_SESSIONS |
Maximum number of sessions saved | Digit |
MCP-related variables
When Claude Code executes an MCP tool, it sets the following environment variables:
| Variable Name | Description | Value |
|---|---|---|
CLAUDE_CODE_MCP_SERVER_NAME |
MCP server name | String |
CLAUDE_CODE_MCP_TOOL_NAME |
Name of the MCP tool being invoked | String |
CLAUDE_CODE_MCP_TOOL_ARGS |
Arguments passed to the MCP tool | JSON string |
Workflow-related variables
| Variable Name | Description | Value |
|---|---|---|
CLAUDE_CODE_WORKTREE_CLEANUP_PERIOD_DAYS |
Automatic cleanup period for orphaned worktrees | Number of days |
CLAUDE_CODE_DISABLE_WORKTREE_AUTO_CLEANUP |
Disable automatic worktree cleanup | 1 |
GitHub Actions-specific variables
| Variable Name | Description | Value |
|---|---|---|
ANTHROPIC_VERTEX_PROJECT_ID |
Vertex AI project ID (set by the authentication step when using Vertex) | GCP project ID |
CLOUD_ML_REGION |
Vertex AI region | Region code (e.g. us-east5) |
settings.json configuration method
All environment variables can be configured in settings.json , which is more declarative and portable:
Example
{
"env"
:
{
"ANTHROPIC_MODEL"
:
"claude-sonnet-4-5"
,
"CLAUDE_CODE_SUBAGENT_MODEL"
:
"claude-haiku-3-5"
,
"CLAUDE_CODE_PERMISSION_MODE"
:
"plan"
,
"CLAUDE_CODE_ALLOWED_TOOLS"
:
"Read,Grep,Glob,Bash"
,
"CLAUDE_CODE_ENABLE_TELEMETRY"
:
"1"
,
"OTEL_METRICS_EXPORTER"
:
"otlp"
}
}
VS Code plugin configuration
When using the Claude Code plugin in VS Code, you can configure through environmentVariables settings:
Example
{
"claudeCode.environmentVariables"
:
[
{
"name"
:
"ANTHROPIC_BASE_URL"
,
"value"
:
"https://custom-proxy.com/api"
}
,
{
"name"
:
"ANTHROPIC_AUTH_TOKEN"
,
"value"
:
"your-token-here"
}
,
{
"name"
:
"ANTHROPIC_MODEL"
,
"value"
:
"claude-sonnet-4-5"
}
]
}
Common usage scenarios
Scenario 1: Using a proxy or custom API endpoint
Example
Access the Claude API through an enterprise internal proxy
export
ANTHROPIC_BASE_URL
=
"https://proxy.company.com/anthropic/v1"
export
ANTHROPIC_API_KEY
=
"your-api-key"
Scenario 2: Cost optimization configuration
Example
Main conversation uses Sonnet, subagents uniformly use Haiku
export
ANTHROPIC_MODEL
=
"claude-sonnet-4-5"
export
CLAUDE_CODE_SUBAGENT_MODEL
=
"claude-haiku-3-5"
Scenario 3: CI/CD environment security configuration
Example
Use read-only mode in CI environments
export
CLAUDE_CODE_PERMISSION_MODE
=
"plan"
export
CLAUDE_CODE_DISABLE_HISTORY
=
"1"
export
CLAUDE_CODE_ALLOWED_TOOLS
=
"Read,Grep,Glob,Bash(gh *)"
Scenario 4: Windows PowerShell Environment
Example
Enabling PowerShell on Windows
$env
:CLAUDE_CODE_USE_POWERSHELL_TOOL =
"1"
Setting defaultShell in settings.json
Precedence and Overrides
Environment variable precedence (from highest to lowest):
- Command line
exportSettings - Project-level
.claude/settings.json - User level
~/.claude/settings.json - Claude Code default values
in
settings.jsonVariables configured in it override shell environment variables with the same name. This is because settings.json is explicitly read when the Claude Code process starts, giving it higher precedence.
Viewing the Current Configuration
Use /config The command can show the current complete configuration:
/config
This displays all currently effective settings, including environment variables and configurations in settings.json.
Best Practices
1. Handling Sensitive Information
- Do not hardcode sensitive information such as API keys in settings.json
- Use environment variables or shell configuration files (
~/.bashrc、~/.zshrc) - Ensure files containing sensitive information are not under git version control
2. Configure by Environment
- Local development: use a more permissive permission mode
- CI/CD: use read-only mode
plan - Production debugging: enable
CLAUDE_CODE_DEBUG
3. Centralized Management
- Put common configurations in
~/.claude/settings.json - Put project-specific configurations in the project directory's
.claude/settings.json - Provide sensitive configurations via shell environment variables or CI Secrets
Common questions
Q: Which takes precedence, environment variables or settings.json?
Variables configured in settings.json have higher precedence and override shell environment variables with the same name.
Q: How do I make Claude Code use a different API endpoint?
Settings ANTHROPIC_BASE_URL Point the environment variable at your proxy or custom endpoint.
Q: How do subagents control which model is used?
can be used via CLAUDE_CODE_SUBAGENT_MODEL Set the model for all subagents uniformly, or specify it individually in each subagent's frontmatter.
Q: How do I enable PowerShell on Windows?
Set environment variables CLAUDE_CODE_USE_POWERSHELL_TOOL=1, and also set in settings.json defaultShell: "powershell"。
Q: How do I disable all slash commands?
Settings CLAUDE_CODE_DISABLE_SLASH_COMMANDS=1 environment variable.