Skip to navigation

Claude Code

Route Claude Code through Gateway by overriding the Anthropic base URL

Claude Code reads its base URL and auth token from environment variables, which you point at Gateway’s Anthropic-compatible endpoint. Shared setup: Building with coding agents.

Configure Claude Code

Set these in the shell where you run claude. If you signed in with Anthropic credentials before, run /logout and relaunch.

export MERGE_GATEWAY_API_KEY="mg_..."
export ANTHROPIC_BASE_URL="https://api-gateway.merge.dev/v1/anthropic"
export ANTHROPIC_AUTH_TOKEN="$MERGE_GATEWAY_API_KEY"
export ANTHROPIC_API_KEY=""
export ANTHROPIC_DEFAULT_OPUS_MODEL="anthropic/claude-opus-5"
export ANTHROPIC_DEFAULT_SONNET_MODEL="anthropic/claude-sonnet-5"
export ANTHROPIC_DEFAULT_HAIKU_MODEL="anthropic/claude-haiku-4-5-20251001"

ANTHROPIC_API_KEY must be empty: any value takes precedence over ANTHROPIC_AUTH_TOKEN and bypasses Gateway. Confirm both with /status, then ask for a small code change.

ANTHROPIC_DEFAULT_*_MODEL maps the opus, sonnet, and haiku tiers to Gateway models. Unset, Claude Code sends bare Anthropic IDs such as claude-sonnet-4-6, which Gateway also accepts.

Enable routing

Set a tier to a model alias so its policy picks the model per request:

export ANTHROPIC_DEFAULT_OPUS_MODEL="@alias/coding-heavy"
export ANTHROPIC_DEFAULT_SONNET_MODEL="@alias/coding-heavy"

Keep ANTHROPIC_DEFAULT_HAIKU_MODEL pinned: Claude Code sends background tasks to the haiku tier, and a fixed model keeps them cheap and predictable. default_routing also works here, using the key’s project policy or the organization default.

Gotchas

  • The /model picker lists the full Gateway catalog, your organization’s aliases, and default_routing. Gateway prefixes non-Anthropic IDs with claude-code/ so the picker keeps them, then strips it. Models with a 1M-token context window carry a [1m] suffix.
  • The Anthropic endpoint has no project_id body field. Scope an organization key with ANTHROPIC_CUSTOM_HEADERS="X-Project-Id: <project-uuid>", or use a project API key.
  • Claude Code doesn’t read .env files. Persist settings per project in the env block of .claude/settings.local.json, or in your shell profile.
  • Each session is one thread in Logs; to change grouping, see tracing from coding agents

The Claude Desktop app, including its built-in Claude Code surface, ignores these variables

Next steps