Skip to main content
Environment variables can control Claude Code behavior such as model selection, authentication, request routing, and feature toggles. Many of the same behaviors can also be configured through a settings file field, a CLI flag, or an in-session command like /model. This page covers how to:

Set environment variables

A variable you set in your shell lasts for that terminal session, while a variable in a settings file applies every time claude runs.

In your shell

Set the variable before launching claude:
To set it for every session, add the export line to ~/.bashrc, ~/.zshrc, or your shell’s profile file.
The assignment line prints nothing on success, so confirm the variable is set by printing it in the same shell before you run claude:

In settings files

Add variables under the env key in a settings.json file, creating the file if it doesn’t exist. Claude Code reads them directly from the file, so they take effect no matter how claude was launched. A running session applies new and changed values to its environment when you save the file, but a feature that reads its variables once at startup, such as OpenTelemetry monitoring, keeps its startup values until you relaunch. Removing a variable from the file doesn’t unset it in a running session; the removal takes effect the next time you launch claude.
~/.claude/settings.json
The file you choose controls who the variables apply to: See Settings files for where each file lives and Settings precedence for how they combine when more than one sets the same variable.

Precedence

Where the same behavior has both an environment variable and a settings field, the environment variable takes precedence. For example, ANTHROPIC_MODEL overrides the model setting, and CLAUDE_CODE_AUTO_CONNECT_IDE overrides autoConnectIde. The settings field applies when the environment variable is not set. When the same variable is set in both your shell and a settings file env block, the settings file value applies. Claude Code writes each env entry into the process environment at startup and again when the file changes, replacing the value inherited from the shell. A few variables are special-cased; the env setting lists the exceptions. Between settings files, env values follow settings precedence, so a managed settings entry overrides the same variable in user or project settings. How an environment variable interacts with CLI flags and in-session commands varies per feature: --model and /model override ANTHROPIC_MODEL, while CLAUDE_CODE_EFFORT_LEVEL overrides /effort. When a variable interacts with another configuration source, its row in the Variables list states the precedence or links to the page that documents it. Claude Code reads shell environment variables at startup, so changes to them take effect the next time you launch claude. Variables set under the env key in settings files are reapplied to a running session when the file changes, with the startup-only exception described in In settings files.

Variables

Numeric variables such as timeouts, token budgets, and retry counts accept scientific notation and digit-separator spellings in addition to plain digits, except where a variable’s row notes it takes plain digits only. For example, Claude Code reads 2e3 as 2000 and 64_000 as 64000. Before v2.1.211, these spellings could silently set a much smaller value, such as 1e6 setting a timeout to 1. Standard OpenTelemetry exporter variables (OTEL_METRICS_EXPORTER, OTEL_LOGS_EXPORTER, OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_PROTOCOL, OTEL_EXPORTER_OTLP_HEADERS, OTEL_METRIC_EXPORT_INTERVAL, OTEL_RESOURCE_ATTRIBUTES, and signal-specific variants) are also supported. See Monitoring for configuration details.

See also