This page looks best with JavaScript enabled

Claude Code Custom Model Configuration and Usage Tips

 ·  ☕ 5 min read

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):

  1. Managed (pushed by the enterprise, cannot be overridden)
  2. CLI arguments (single session)
  3. Local: .claude/settings.local.json (current project only; a good place for secrets)
  4. Project: .claude/settings.json (shared with the team, can be committed to git)
  5. 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

VariablePurpose
ANTHROPIC_BASE_URLGateway address, replacing the default api.anthropic.com
ANTHROPIC_AUTH_TOKENBearer Token (Claude Code automatically prepends the Bearer prefix)
ANTHROPIC_MODELDefault 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

1
2
3
4
5
6
7
8
9
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.xxx.com/v1",
    "ANTHROPIC_AUTH_TOKEN": "your-token",
    "ANTHROPIC_MODEL": "zhipu/glm5.2",
    "CLAUDE_CODE_MAX_CONTEXT_TOKENS": "1000000",
    "CLAUDE_CODE_AUTO_COMPACT_WINDOW": "1000000"
  }
}

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-token in the shell, with only the URL and model in settings.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_URL points at something other than api.anthropic.com, MCP tool search is off by default; if the proxy supports tool_reference, you can set ENABLE_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).

VariablePurpose
CLAUDE_CODE_MAX_CONTEXT_TOKENSDeclares the model’s actual context window size
CLAUDE_CODE_AUTO_COMPACT_WINDOWThreshold that triggers auto-compaction, range 100000–1000000
CLAUDE_CODE_DISABLE_1M_CONTEXTSet 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.

1
2
3
4
5
6
7
8
9
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.xxx.com/v1",
    "ANTHROPIC_AUTH_TOKEN": "your-token",
    "ANTHROPIC_MODEL": "zhipu/glm5.2",
    "CLAUDE_CODE_MAX_CONTEXT_TOKENS": "1000000",
    "CLAUDE_CODE_AUTO_COMPACT_WINDOW": "1000000"
  }
}

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.xxx.com/v1",
    "ANTHROPIC_AUTH_TOKEN": "your-token",
    "ANTHROPIC_MODEL": "zhipu/glm5.2",
    "ANTHROPIC_DEFAULT_SONNET_MODEL": "zhipu/glm5.2",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL": "deepseek/deepseek-v4-flash",
    "ANTHROPIC_DEFAULT_OPUS_MODEL": "deepseek/deepseek-v4-pro",
    "ANTHROPIC_DEFAULT_SONNET_MODEL_NAME": "GLM 5.2",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL_NAME": "DeepSeek V4 Flash",
    "ANTHROPIC_DEFAULT_OPUS_MODEL_NAME": "DeepSeek V4 Pro",
    "CLAUDE_CODE_MAX_CONTEXT_TOKENS": "1000000",
    "CLAUDE_CODE_AUTO_COMPACT_WINDOW": "1000000"
  },
  "model": "sonnet"
}
TierModelUse
Sonnet (default)GLM 5.2Everyday use
HaikuDeepSeek V4 FlashLightweight background tasks, used automatically for this tier
OpusDeepSeek V4 ProHeavy 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

1
2
claude --model deepseek/deepseek-v4-pro
claude -p "review this diff"

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):

1
2
3
4
{
  "skipAutoPermissionPrompt": true,
  "skipDangerousModePermissionPrompt": true
}

6. Usage Tips

6.1 Everyday Development

  • Default to GLM 5.2 day to day, and switch to Opus (Pro) in /model when 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

SymptomCommon Cause
Still asked to log in at startupThe Token was not written to settings or the shell before the first run
404 / empty responseThe gateway is not compatible with the Anthropic Messages API
Invalid model IDThe 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 effectClaude Code was not restarted
Long sessions compact too earlyCLAUDE_CODE_MAX_CONTEXT_TOKENS was not set and the gateway model ID was not recognized
Wrong compact thresholdCLAUDE_CODE_AUTO_COMPACT_WINDOW was written as 1M; it should be 1000000

7. Complete Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.xxx.com/v1",
    "ANTHROPIC_AUTH_TOKEN": "your-token",
    "ANTHROPIC_MODEL": "zhipu/glm5.2",
    "ANTHROPIC_DEFAULT_SONNET_MODEL": "zhipu/glm5.2",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL": "deepseek/deepseek-v4-flash",
    "ANTHROPIC_DEFAULT_OPUS_MODEL": "deepseek/deepseek-v4-pro",
    "ANTHROPIC_DEFAULT_SONNET_MODEL_NAME": "GLM 5.2",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL_NAME": "DeepSeek V4 Flash",
    "ANTHROPIC_DEFAULT_OPUS_MODEL_NAME": "DeepSeek V4 Pro",
    "CLAUDE_CODE_MAX_CONTEXT_TOKENS": "1000000",
    "CLAUDE_CODE_AUTO_COMPACT_WINDOW": "1000000"
  },
  "model": "sonnet",
  "skipAutoPermissionPrompt": true,
  "skipDangerousModePermissionPrompt": true
}
1
2
3
claude            # default GLM 5.2
claude /model     # switch between GLM / Flash / Pro
claude /status    # confirm the gateway

微信公众号
WRITTEN BY
微信公众号