Claude Code

Route Claude Code through Merge Gateway by overriding the Anthropic base URL

Claude Code is Anthropic’s terminal coding agent. Override its base URL and auth token to send every request through Merge Gateway, with routing policies, cost governance, and full request observability in the Gateway dashboard.

Before you start

  • Install Claude Code: curl -fsSL https://claude.ai/install.sh | bash or npm install -g @anthropic-ai/claude-code
  • Grab an API key from gateway.merge.dev/settings/api-keys
  • Pick the Claude models you want available. Gateway model names take the form provider/model, for example anthropic/claude-opus-4-7 and anthropic/claude-sonnet-4-6. Browse the full catalog with GET /v1/models or in the dashboard.

Configure Claude Code

1

Export environment variables in your project shell

In a terminal scoped to your project, set:

$export MERGE_GATEWAY_API_KEY="mg_your_key"
$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-4-7"
$export ANTHROPIC_DEFAULT_SONNET_MODEL="anthropic/claude-sonnet-4-6"
$export ANTHROPIC_DEFAULT_HAIKU_MODEL="anthropic/claude-haiku-4-5-20251001"

ANTHROPIC_API_KEY must be explicitly empty, or Claude Code prefers it over ANTHROPIC_AUTH_TOKEN and bypasses Gateway. The ANTHROPIC_DEFAULT_*_MODEL variables map Claude Code’s opus, sonnet, and haiku tiers to the Gateway provider/model slugs you want to use.

If you previously signed in to Claude Code with Anthropic credentials, run /logout and relaunch so the env vars take effect.

2

Launch Claude Code

Run claude in your project. The new session uses Gateway via the env vars you exported above.

3

Verify with /status

Run /status. Confirm the output shows:

  • Auth token: ANTHROPIC_AUTH_TOKEN
  • Anthropic base URL: https://api-gateway.merge.dev/v1/anthropic

Then send a quick prompt to confirm streaming works, and ask Claude Code to make a small code change to verify tool calls and file edits flow through Gateway. The request appears in your Gateway dashboard within a few seconds.

Caveats

Claude Code reads both ANTHROPIC_API_KEY and ANTHROPIC_AUTH_TOKEN. If ANTHROPIC_API_KEY is set to anything non-empty, it wins and Claude Code bypasses your Gateway base URL. The explicit export ANTHROPIC_API_KEY="" in the recipe above clears any inherited value for the session.

The ANTHROPIC_DEFAULT_OPUS_MODEL, ANTHROPIC_DEFAULT_SONNET_MODEL, and ANTHROPIC_DEFAULT_HAIKU_MODEL env vars override the model identifier Claude Code sends in each request. Set them to Gateway provider/model slugs (anthropic/claude-opus-4-7, anthropic/claude-sonnet-4-6, anthropic/claude-haiku-4-5-20251001) so Gateway can resolve them. Without these overrides Claude Code sends the bare Anthropic alias (for example claude-sonnet-4-6), which Gateway accepts but does not let you steer per request.

Instead of hard-picking a model, point at a routing policy and let Gateway select the vendor and model per request based on your rules (cost, performance, or prompt complexity). Useful when you want cheap models for simple edits and stronger models for hard problems without switching by hand.

The recipe above lives in your shell, so it applies only to that terminal. To persist it, add the same export lines to your shell profile (~/.zshrc, ~/.bashrc, or ~/.config/fish/config.fish), or use a project-scoped .claude/settings.local.json with an env block (Claude Code does not read .env files).

Claude Code relies on tool calls to read and edit files. Tool calling works through Gateway for any model whose capabilities.supports_tool_calling is true on the chosen vendor route. See Tool calling.

Next steps