References: Claude Code Configuration, Environment Variables, Connecting to an LLM Gateway
Claude Code talks to the Anthropic Messages API. When connecting to a custom gateway, use the env block of settings.json to override the endpoint and the model.
1. Where the Configuration File Lives
The user configuration is at ~/.claude/settings.json by default (on Windows, %USERPROFILE%\.claude\settings.json).
Configuration precedence (high → low):
- Managed (pushed by the enterprise, cannot be overridden)
- CLI arguments (single session)
- Local:
.claude/settings.local.json(current project only; a good place for secrets) - Project:
.claude/settings.json(shared with the team, can be committed to git) - User:
~/.claude/settings.json(global personal configuration)
MCP Servers are written in ~/.claude.json or the project’s .mcp.json, managed separately from settings.json.
2. Connecting to a Gateway
| Variable | Purpose |
|---|---|
ANTHROPIC_BASE_URL | Gateway address, replacing the default api.anthropic.com |
ANTHROPIC_AUTH_TOKEN | Bearer Token (Claude Code automatically prepends the Bearer prefix) |
ANTHROPIC_MODEL | Default model ID |
Use ANTHROPIC_API_KEY when connecting directly to Anthropic; use ANTHROPIC_AUTH_TOKEN when connecting to a custom gateway.
2.1 Minimal Configuration
| |
Restart Claude Code, then use /status to confirm the base URL and the authentication source.
2.2 Where to Put the Secret
Writing the env block in settings.json is the least fuss, but the Token then lands on disk in plaintext. The api.xxx.com and your-token in this post’s examples are placeholders; replace them with your own values, and never commit a real Token to a public repository.
Safer approaches:
- Global:
export ANTHROPIC_AUTH_TOKEN=your-tokenin the shell, with only the URL and model insettings.json - Project:
.claude/settings.local.json(Claude Code adds it to gitignore when it creates it)
When the same variable exists both in settings.json and in the shell, the settings file wins.
2.3 Gateway Caveats
- The gateway must be compatible with the Anthropic Messages API
- When
ANTHROPIC_BASE_URLpoints at something other thanapi.anthropic.com, MCP tool search is off by default; if the proxy supportstool_reference, you can setENABLE_TOOL_SEARCH=true - Model IDs are subject to the gateway’s documentation
3. Context Window
When connecting to a custom gateway, Claude Code often cannot recognize the model ID and falls back to the default window (usually 200K) for compaction and rate limiting, so long sessions compact too early or error out. It is best to declare the window size explicitly, and by default configure it as 1M (1 million tokens).
| Variable | Purpose |
|---|---|
CLAUDE_CODE_MAX_CONTEXT_TOKENS | Declares the model’s actual context window size |
CLAUDE_CODE_AUTO_COMPACT_WINDOW | Threshold that triggers auto-compaction, range 100000–1000000 |
CLAUDE_CODE_DISABLE_1M_CONTEXT | Set to 1 to disable the 1M window and force 200K handling |
When the gateway model ID does not contain claude- and cannot be recognized, CLAUDE_CODE_MAX_CONTEXT_TOKENS takes effect directly. If the model ID carries a [1m] suffix but the gateway’s actual window differs, you need to set CLAUDE_CODE_DISABLE_1M_CONTEXT=1 as well and then configure CLAUDE_CODE_MAX_CONTEXT_TOKENS.
| |
CLAUDE_CODE_AUTO_COMPACT_WINDOW accepts only plain digits (such as 1000000); the 1M form is not supported. If the model’s actual window is smaller than 1M, just change both values to the corresponding size.
4. Multi-Model Configuration
Claude Code does multi-model by mapping the three tiers Sonnet / Haiku / Opus to different model IDs on the gateway.
4.1 Mapping by Tier
| |
| Tier | Model | Use |
|---|---|---|
| Sonnet (default) | GLM 5.2 | Everyday use |
| Haiku | DeepSeek V4 Flash | Lightweight background tasks, used automatically for this tier |
| Opus | DeepSeek V4 Pro | Heavy coding, switched manually in /model |
"model": "sonnet" ensures startup goes through the Sonnet tier, avoiding accidental use of Haiku.
4.2 Switching Within a Session
| |
In interactive mode, use /model to switch between the configured tiers.
5. Permission Configuration
If you trust the current environment for local development, you can skip some confirmations (use only in an isolated environment):
| |
6. Usage Tips
6.1 Everyday Development
- Default to GLM 5.2 day to day, and switch to Opus (Pro) in
/modelwhen coding gets hard - Flash runs on the Haiku tier and background tasks use it automatically, so there is no need to switch manually
6.2 Troubleshooting Checklist
| Symptom | Common Cause |
|---|---|
| Still asked to log in at startup | The Token was not written to settings or the shell before the first run |
| 404 / empty response | The gateway is not compatible with the Anthropic Messages API |
| Invalid model ID | The model name does not match the gateway side |
| Startup went to Flash | "model" was set to "haiku"; change it back to "sonnet" |
| Config changes have no effect | Claude Code was not restarted |
| Long sessions compact too early | CLAUDE_CODE_MAX_CONTEXT_TOKENS was not set and the gateway model ID was not recognized |
| Wrong compact threshold | CLAUDE_CODE_AUTO_COMPACT_WINDOW was written as 1M; it should be 1000000 |
7. Complete Example
| |
| |
