Direct answer
From client configuration to protocol pass-through, this guide explains what must be verified when Claude Code connects to an LLM gateway.
What this guide helps you solve
Help users and gateway operators configure model routing correctly and distinguish "a request succeeds" from "fully compatible."
Prefer model selectors recognized by the current CLI
Claude Code can select a model interactively with /model or through a launch option. Official aliases may point to newer models as the product evolves, while fixed model IDs suit controlled environments that require reproducible results.
A gateway may use its own model IDs or aliases. Do not infer upstream support from a name displayed on a page; treat the gateway's model list and a real request as the source of truth.
claude --model sonnet/modelConfigure base URL with correct credential variables
An Anthropic Messages-compatible gateway uses ANTHROPIC_BASE_URL. Put a Bearer token in ANTHROPIC_AUTH_TOKEN and an x-api-key credential in ANTHROPIC_API_KEY. The wrong variable sends credentials in a header the server does not read.
For the first connection, export variables only in the current shell. Confirm the URL, authentication, and model before moving them to secure user-level configuration.
export ANTHROPIC_BASE_URL='https://gateway.example.com'
export ANTHROPIC_AUTH_TOKEN='<gateway-token>'
claudeFirst verify the gateway with a minimal Messages request
Calling /v1/messages directly before launching Claude Code separates gateway URL and authentication issues from CLI configuration. Use a model ID explicitly provided by the service rather than copying another vendor's example.
A 401 points to credentials or request headers; a 404 commonly indicates an incorrect base URL path; an unknown-model response usually means networking and authentication reached the gateway, so check model routing next.
export CLAUDE_MODEL='<gateway-model-id>'
curl -X POST "$ANTHROPIC_BASE_URL/v1/messages" \
-H "Authorization: Bearer $ANTHROPIC_AUTH_TOKEN" \
-H 'anthropic-version: 2023-06-01' \
-H 'content-type: application/json' \
-d "{\"model\":\"$CLAUDE_MODEL\",\"max_tokens\":1,\"messages\":[{\"role\":\"user\",\"content\":\".\"}]}"Full compatibility requires more than a working /v1/messages endpoint
Claude Code consumes server-sent events, so the gateway must forward the streaming response as it arrives rather than buffer the complete response. anthropic-version, anthropic-beta, and their corresponding request body fields must pass through together according to the protocol.
A gateway should not forward only a fixed allowlist of fields observed today. Claude Code updates add capabilities; stripping headers, rewriting request bodies, or wrapping upstream errors can break retries and capability fallback.
- Forward SSE data as soon as it arrives so the client does not appear stuck.
- Keep the beta header and accompanying body fields.
- Preserve upstream error statuses and response bodies whenever possible.
- When the optional token-counting endpoint is missing, the client uses local estimates instead.
Enable gateway model discovery on demand
Gateways that support the Anthropic Messages format can implement /v1/models. After model discovery is explicitly enabled, Claude Code adds eligible models to the /model selector. This capability is off by default and requires support from both the current CLI and gateway.
Discovery failures should not affect core inference; the client may use a cache or built-in list. Before claiming automatic discovery support, test the endpoint, authentication headers, three-second response requirement, and model ID filtering.
export CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1
claude --debug-file /tmp/claude-gateway-debug.logUnderstand which product surfaces support custom gateways
The CLI and VS Code can read gateway variables from their respective configurations; Desktop uses separate third-party inference settings. Claude Code on the web and in Slack are Anthropic-hosted surfaces and do not use a custom gateway merely because ANTHROPIC_BASE_URL is set locally.
Some features that depend on claude.ai identity or direct service access are unavailable while gateway credentials are active. Documentation should clearly distinguish "CLI inference works" from "every Claude Code product surface works."
Official sources and verification scope
This guide is based on public official documentation. Commands and configuration may change between client versions, so verify the linked sources before use.
Read the verification methodology