2> Fetch the complete documentation index at: https://code.claude.com/docs/llms.txt2> Fetch the complete documentation index at: https://code.claude.com/docs/llms.txt
3> Use this file to discover all available pages before exploring further.3> Use this file to discover all available pages before exploring further.
4 4
5# LLM gateway configuration5# LLM gateways
6 6
7> Learn how to configure Claude Code to work with LLM gateway solutions. Covers gateway requirements, authentication configuration, model selection, and provider-specific endpoint setup.7> Route Claude Code through an LLM gateway for centralized authentication, usage tracking, and cost controls. Covers connecting Claude Code to a gateway, rolling one out for your organization, what Claude Code sends to a gateway, and how gateways interact with claude.ai subscriptions.
8 8
9LLM gateways provide a centralized proxy layer between Claude Code and model providers, often providing:9An LLM gateway is a proxy your organization runs between Claude Code and a model provider. Claude Code sends API traffic to the gateway, and the gateway forwards it to the provider using a credential your organization controls.
10 10
11* **Centralized authentication** - Single point for API key management11This page covers:
12* **Usage tracking** - Monitor usage across teams and projects
13* **Cost controls** - Implement budgets and rate limits
14* **Audit logging** - Track all model interactions for compliance
15* **Model routing** - Switch between providers without code changes
16 12
17This page covers gateway requirements and configuration for the Claude Code CLI. Enterprise Desktop deployments can configure gateway providers via [managed settings](https://support.claude.com/en/articles/12622667-enterprise-configuration). The Claude Desktop app can also run against a self-hosted gateway through the [Cowork on 3P research preview](https://claude.com/docs/cowork/3p/gateway), which uses its own configuration keys.13* [What a gateway provides](#what-a-gateway-provides)
18 14* [How routing and credentials work](#how-a-gateway-works)
19## Gateway requirements15* [The steps to roll one out](#roll-out-a-gateway)
20 16* [How gateways interact with claude.ai subscriptions](#subscriptions-and-gateways)
21For an LLM gateway to work with Claude Code, it must meet the following requirements:17* [What's configured separately from the gateway](#configure-separately-from-the-gateway)
22
23**API format**
24
25The gateway must expose to clients at least one of the following API formats:
26
271. **Anthropic Messages**: `/v1/messages`, `/v1/messages/count_tokens`
28 * Must forward request headers: `anthropic-beta`, `anthropic-version`
29
302. **Bedrock InvokeModel**: `/invoke`, `/invoke-with-response-stream`
31 * Must preserve request body fields: `anthropic_beta`, `anthropic_version`
32
333. **Vertex rawPredict**: `:rawPredict`, `:streamRawPredict`, `/count-tokens:rawPredict`
34 * Must forward request headers: `anthropic-beta`, `anthropic-version`
35
36Failure to forward headers or preserve body fields may result in reduced functionality or inability to use Claude Code features.
37 18
38<Note>19<Note>
39 Claude Code determines which features to enable based on the API format. When using the Anthropic Messages format with Bedrock or Vertex, you may need to set environment variable `CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1`.20 - If you're a developer connecting to an existing gateway: [connect Claude Code to your gateway](/en/llm-gateway-connect)
21 - If you're an admin rolling out a gateway for your organization: [deploy and distribute a gateway](/en/llm-gateway-rollout)
22 - If you're configuring a gateway product: the [gateway protocol reference](/en/llm-gateway-protocol)
40</Note>23</Note>
41 24
42**Request headers**25## What a gateway provides
43
44Claude Code includes the following headers on API requests:
45
46| Header | Description |
47| :------------------------------ | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
48| `X-Claude-Code-Session-Id` | A unique identifier for the current Claude Code session. Proxies can use this to aggregate all API requests from a single session without parsing the request body. |
49| `X-Claude-Code-Agent-Id` | Identifier of the subagent or teammate that issued the request. Your proxy can use this to attribute API cost to individual parallel subagents within a session, without parsing the request body. Present only for requests made by an in-process subagent or teammate. |
50| `X-Claude-Code-Parent-Agent-Id` | Identifier of the agent that spawned the agent making the request. Use this with `X-Claude-Code-Agent-Id` to attribute API costs across nested agents in your proxy. Present only when the requesting agent was itself spawned by another agent. |
51
52Both agent ID headers are ephemeral per-spawn identifiers, not persistent user or device IDs.
53
54Claude Code also prepends a short attribution block to the system prompt containing the client version and a fingerprint derived from the conversation. The Anthropic API strips this block before processing, so it does not affect first-party prompt caching. If your gateway implements its own prompt cache keyed on the full request body, set [`CLAUDE_CODE_ATTRIBUTION_HEADER=0`](/en/env-vars) to omit it.
55
56## Configuration
57
58### Model selection
59
60By default, Claude Code uses standard model names for the selected API format.
61
62When `ANTHROPIC_BASE_URL` points at a gateway that exposes the Anthropic Messages format, Claude Code can query the gateway's `/v1/models` endpoint at startup and add the returned models to the `/model` picker. Set `CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1` to enable this. Discovery is off by default so that gateways backed by a shared API key do not surface every model the key can access to every user. Each discovered entry is labeled "From gateway" and uses the `display_name` field from the response when one is provided. This requires Claude Code v2.1.129 or later.
63
64Discovery applies only to the Anthropic Messages format. It does not run for Bedrock or Vertex pass-through endpoints, and it does not run when `ANTHROPIC_BASE_URL` is unset or points at `api.anthropic.com`.
65
66The discovery request authenticates the same way as inference requests: it sends `ANTHROPIC_AUTH_TOKEN` as a bearer token, or `ANTHROPIC_API_KEY` as the `x-api-key` header when no auth token is set, along with any headers from `ANTHROPIC_CUSTOM_HEADERS`. Only models whose ID begins with `claude` or `anthropic` are added to the picker. Results are cached to `~/.claude/cache/gateway-models.json` and refreshed on each startup. If the request fails or the gateway does not implement `/v1/models`, the picker falls back to the cached list from the previous startup or to the built-in model list.
67
68If your gateway uses model names that do not match the discovery filter, use the environment variables documented in [Model configuration](/en/model-config) to add them manually.
69
70## LiteLLM configuration
71
72<Warning>
73 LiteLLM PyPI versions 1.82.7 and 1.82.8 were compromised with credential-stealing malware. Do not install these versions. If you have already installed them:
74
75 * Remove the package
76 * Rotate all credentials on affected systems
77 * Follow the remediation steps in [BerriAI/litellm#24518](https://github.com/BerriAI/litellm/issues/24518)
78
79 LiteLLM is a third-party proxy service. Anthropic doesn't endorse, maintain, or audit LiteLLM's security or functionality. This guide is provided for informational purposes and may become outdated. Use at your own discretion.
80</Warning>
81
82### Prerequisites
83
84* Claude Code updated to the latest version
85* LiteLLM Proxy Server deployed and accessible
86* Access to Claude models through your chosen provider
87
88### Basic LiteLLM setup
89
90**Configure Claude Code**:
91
92#### Authentication methods
93
94##### Static API key
95
96Simplest method using a fixed API key:
97
98```bash theme={null}
99# Set in environment
100export ANTHROPIC_AUTH_TOKEN=sk-litellm-static-key
101
102# Or in Claude Code settings
103{
104 "env": {
105 "ANTHROPIC_AUTH_TOKEN": "sk-litellm-static-key"
106 }
107}
108```
109
110This value will be sent as the `Authorization` header.
111
112##### Dynamic API key with helper
113
114For rotating keys or per-user authentication:
115
1161. Create an API key helper script:
117
118```bash theme={null}
119#!/bin/bash
120# ~/bin/get-litellm-key.sh
121
122# Example: Fetch key from vault
123vault kv get -field=api_key secret/litellm/claude-code
124
125# Example: Generate JWT token
126jwt encode \
127 --secret="${JWT_SECRET}" \
128 --exp="+1h" \
129 '{"user":"'${USER}'","team":"engineering"}'
130```
131
1322. Configure Claude Code settings to use the helper:
133 26
134```json theme={null}27A gateway gives your organization one place to manage:
135{
136 "apiKeyHelper": "~/bin/get-litellm-key.sh"
137}
138```
139 28
1403. Set token refresh interval:29* **Credentials**: the provider key stays server-side; developers hold gateway credentials instead
30* **Usage tracking**: attribute usage by developer or team, regardless of which provider serves the request
31* **Cost controls**: enforce budgets and rate limits in one place
32* **Audit logging**: log every model request for compliance
33* **Provider switching**: change the provider in gateway configuration, without touching developer machines
141 34
142```bash theme={null}35All of these except provider switching apply whether the upstream is Anthropic's API or a [cloud provider](/en/third-party-integrations).
143# Refresh every hour (3600000 ms)
144export CLAUDE_CODE_API_KEY_HELPER_TTL_MS=3600000
145```
146 36
147This value will be sent as `Authorization` and `X-Api-Key` headers. The `apiKeyHelper` has lower precedence than `ANTHROPIC_AUTH_TOKEN` or `ANTHROPIC_API_KEY`.37The tradeoff is that the gateway becomes infrastructure your organization operates. Claude Code adds capabilities with each release, and a gateway that doesn't forward them breaks the corresponding features, so the gateway product needs to be kept updated as Claude Code evolves. The [gateway protocol reference](/en/llm-gateway-protocol) covers what to forward.
148 38
149#### Unified endpoint (recommended)39## How a gateway works
150 40
151Using LiteLLM's [Anthropic format endpoint](https://docs.litellm.ai/docs/anthropic_unified):41By default, Claude Code sends requests directly to Anthropic's API at `api.anthropic.com`. To route through a gateway, set `ANTHROPIC_BASE_URL` to the gateway's address; Claude Code sends the same requests there instead. The gateway authenticates the developer, attaches your organization's provider credential, and forwards each request to whichever provider it's configured for.
152 42
153```bash theme={null}43`ANTHROPIC_BASE_URL` is the address variable for most gateways. A gateway that fronts a specific cloud provider, such as Bedrock, Vertex, Foundry, or the Claude Platform on AWS, uses that provider's base URL variable instead; [API formats](/en/llm-gateway-protocol#api-formats) lists which variable goes with each configuration.
154export ANTHROPIC_BASE_URL=https://litellm-server:4000
155```
156 44
157**Benefits of the unified endpoint over pass-through endpoints:**45<Frame>
46 <img src="https://mintcdn.com/claude-code/zIcIE_SQv4Z0Zbhc/images/llm-gateway-flow.svg?fit=max&auto=format&n=zIcIE_SQv4Z0Zbhc&q=85&s=490607d033d235694efb49a73a5b9e4b" alt="Diagram showing Claude Code routing through an LLM gateway. In a developer machines zone, the Claude Code CLI, VS Code extension, and CI or Agent SDK clients send requests to the gateway, with the base URL variable for the gateway's API format pointing at it and each developer holding a per-developer credential, and the desktop app reaches the same gateway through organization-distributed configuration. In a zone labeled your infrastructure, the LLM gateway handles authentication, usage tracking, budgets, and routing, and forwards requests with your organization's credential. In a model providers zone, a solid arrow leads to the provider you configure, shown as the Anthropic API, and dashed arrows lead to other provider options, illustrated with Amazon Bedrock, Google Vertex AI, and Microsoft Foundry as examples." width="780" height="322" data-path="images/llm-gateway-flow.svg" />
47</Frame>
158 48
159* Load balancing49Two kinds of credential are involved:
160* Fallbacks
161* Consistent support for cost tracking and end-user tracking
162 50
163#### Provider-specific pass-through endpoints (alternative)51* **Developer credentials**: each developer holds their own, issued by the gateway. It authenticates them to the gateway and identifies them in usage tracking
52* **Provider credential**: the gateway holds one credential for your provider account, shared by all forwarded traffic. You don't provision provider keys per developer
164 53
165##### Claude API through LiteLLM54The gateway forwards each request to the provider you configure, such as the Anthropic API, [Amazon Bedrock](/en/amazon-bedrock), [Google Vertex AI](/en/google-vertex-ai), [Microsoft Foundry](/en/microsoft-foundry), or the [Claude Platform on AWS](/en/claude-platform-on-aws). Because Claude Code talks only to the gateway, the provider choice is the gateway's configuration, not the client's.
166 55
167Using [pass-through endpoint](https://docs.litellm.ai/docs/pass_through/anthropic_completion):56## Roll out a gateway
168 57
169```bash theme={null}58When you're ready to roll out an LLM gateway to your organization, the sequence is the same whichever gateway product you choose:
170export ANTHROPIC_BASE_URL=https://litellm-server:4000/anthropic
171```
172 59
173##### Amazon Bedrock through LiteLLM601. Deploy the gateway and give it your provider credential, so it can authenticate the requests it forwards.
612. Issue each developer a gateway credential, so usage is attributed to the developer and offboarding revokes one credential.
623. Distribute the configuration through a [managed settings file](/en/settings#settings-files) and your secrets tooling, so every machine receives the base URL and a credential. When both are distributed, developers don't configure anything. If you don't have settings distribution in place, developers follow the [connect page](/en/llm-gateway-connect) to set the variables themselves.
634. Have each developer [check for the configuration in Claude Code](/en/llm-gateway-connect#check-for-an-existing-configuration), so distribution problems surface before they depend on the gateway.
174 64
175Using [pass-through endpoint](https://docs.litellm.ai/docs/pass_through/bedrock):65[Roll out an LLM gateway for your organization](/en/llm-gateway-rollout) walks each step and shows the configuration files to distribute at each one. The gateway is one part of organization setup; for policy enforcement, usage visibility, and data handling decisions, see [Set up Claude Code for your organization](/en/admin-setup).
176 66
177```bash theme={null}67## Third-party gateways
178export ANTHROPIC_BEDROCK_BASE_URL=https://litellm-server:4000/bedrock
179export CLAUDE_CODE_SKIP_BEDROCK_AUTH=1
180export CLAUDE_CODE_USE_BEDROCK=1
181```
182 68
183##### Google Vertex AI through LiteLLM69Any gateway that exposes a [supported API format](/en/llm-gateway-protocol#api-formats) works. Anthropic doesn't endorse, maintain, or audit third-party gateway products. Deploy them following their own documentation, then complete the Claude Code side of the rollout with the [rollout steps](/en/llm-gateway-rollout).
184 70
185Using [pass-through endpoint](https://docs.litellm.ai/docs/pass_through/vertex_ai):71## Subscriptions and gateways
186 72
187```bash theme={null}73While a [gateway credential variable](/en/llm-gateway-connect#set-the-credential-variable) or `apiKeyHelper` is active, a developer's claude.ai subscription isn't used: the credential replaces the subscription login for that session, and the subscription's usage limits don't apply. That traffic is billed per token to whoever owns the credential the gateway forwards, such as your organization's Anthropic Console account, or your Bedrock, Vertex, or Foundry account when the gateway routes there.
188export ANTHROPIC_VERTEX_BASE_URL=https://litellm-server:4000/vertex_ai/v1
189export ANTHROPIC_VERTEX_PROJECT_ID=your-gcp-project-id
190export CLAUDE_CODE_SKIP_VERTEX_AUTH=1
191export CLAUDE_CODE_USE_VERTEX=1
192export CLOUD_ML_REGION=us-east5
193```
194 74
195##### Claude Platform on AWS through a gateway75Setting only `ANTHROPIC_BASE_URL`, without a gateway credential, doesn't replace the subscription. Requests still route through the gateway, but a saved claude.ai login remains the active credential, so its usage limits and billing apply. Gateways that pass this traffic on to Anthropic must forward the OAuth capability in `anthropic-beta`; see the [request headers reference](/en/llm-gateway-protocol#request-headers).
196 76
197Route to a gateway that forwards to the [Claude Platform on AWS](/en/claude-platform-on-aws) endpoint:77## Configure separately from the gateway
198 78
199```bash theme={null}79A gateway determines where model API requests are sent. Model selection, the rest of Claude Code's network traffic, and corporate proxies are configured separately:
200export ANTHROPIC_AWS_BASE_URL=https://litellm-server:4000/anthropic-aws
201export ANTHROPIC_AWS_WORKSPACE_ID=wrkspc_01ABCDEFGHIJKLMN
202export CLAUDE_CODE_SKIP_ANTHROPIC_AWS_AUTH=1
203export CLAUDE_CODE_USE_ANTHROPIC_AWS=1
204```
205 80
206For more detailed information, refer to the [LiteLLM documentation](https://docs.litellm.ai/).81* **Model selection**: the base URL decides where requests go, not which model answers them. Pick the model with the `/model` command or the model environment variables; see [how to set your model](/en/model-config#setting-your-model)
82* **Client-side traffic**: version checks and optional client telemetry, both disabled with [`CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC`](/en/env-vars), and login traffic when a claude.ai or Console login is in use, go to Anthropic's update and authentication endpoints rather than the gateway. See [network access requirements](/en/network-config#network-access-requirements) for the domains
83* **Corporate proxies**: a proxy set with `HTTPS_PROXY` sits between Claude Code and every server it talks to, including the gateway. If your network requires a proxy, configure both; see [proxy configuration](/en/network-config#proxy-configuration)
207 84
208## Additional resources85## Related pages
209 86
210* [LiteLLM documentation](https://docs.litellm.ai/)87* [Connect Claude Code to an LLM gateway](/en/llm-gateway-connect): set the base URL and credential on your own machine, with per-surface configuration and a troubleshooting table
211* [Claude Code settings](/en/settings)88* [Roll out an LLM gateway for your organization](/en/llm-gateway-rollout): the admin checklist for deploying a gateway, issuing developer credentials, and distributing managed settings
212* [Enterprise network configuration](/en/network-config)89* [Gateway protocol reference](/en/llm-gateway-protocol): what Claude Code sends to a gateway, for operators configuring one, covering endpoints, headers to forward, and feature pass-through
213* [Third-party integrations overview](/en/third-party-integrations)90* [Set up Claude Code for your organization](/en/admin-setup): the wider rollout decisions a gateway is one part of, including policy enforcement and usage visibility