Claude Code with Bring Your Own Key (BYOK)
Use Claude Code with your own Anthropic credentials through the LiteLLM proxy. Claude Code authenticates to Anthropic one of two ways: /login sends an OAuth token as Authorization: Bearer <token> (tied to your Claude.ai Free, Pro, Max, or Enterprise seat), while a configured Anthropic API key is sent as x-api-key. LiteLLM forwards either credential to Anthropic instead of using proxy-configured keys, so you pay Anthropic directly while still benefiting from LiteLLM's routing, logging, and guardrails.
How It Works
- Claude Code authentication: with
/login, Claude Code sends your Anthropic OAuth token asAuthorization: Bearer <token>; with a configured API key, it sendsx-api-keyinstead. - LiteLLM authentication: you pass your LiteLLM proxy key via
ANTHROPIC_CUSTOM_HEADERS(e.g.x-litellm-api-key) so the proxy can authenticate and track your usage without touching the Anthropic credential headers above. - Key forwarding: LiteLLM forwards the OAuth
Authorizationheader to Anthropic automatically. Thex-api-keypath additionally needsforward_llm_provider_auth_headers: true, since LiteLLM stripsx-api-keyby default.
Prerequisites
- Claude Code installed
- Anthropic API key (from console.anthropic.com)
- LiteLLM proxy with a virtual key for authentication
Step 1: Configure LiteLLM Proxy
If you're authenticating with an Anthropic API key rather than /login, enable forwarding of LLM provider auth headers so your key takes precedence:
model_list:
- model_name: claude-sonnet-5
litellm_params:
model: anthropic/claude-sonnet-5
# No api_key needed — client's key will be used
litellm_settings:
forward_llm_provider_auth_headers: true # Required for the x-api-key path; not needed for /login
forward_llm_provider_auth_headers?By default, LiteLLM strips x-api-key from client requests for security. Setting this to true allows a client-provided Anthropic API key to be forwarded to Anthropic, overriding any proxy-configured key. This setting only governs x-api-key and similar provider-key headers; it has no effect on /login, which authenticates via an Authorization: Bearer OAuth token that LiteLLM forwards regardless of this setting, provided you authenticate to the proxy with a different header (such as x-litellm-api-key) rather than Authorization.
You can also complete this setup from the LiteLLM admin UI:
- Add the model via Models → Add Model, leaving the API Key field blank.
- Enable the toggle at Settings → UI Settings → "Forward LLM provider auth headers".
Both UI actions write to the database and override config.yaml at runtime.
Step 2: Create a LiteLLM Virtual Key
Create a virtual key in the LiteLLM UI or via API.
# Example: Create key via API
curl -X POST "http://localhost:4000/key/generate" \
-H "Authorization: Bearer sk-your-master-key" \
-H "Content-Type: application/json" \
-d '{"key_alias": "claude-code-byok", "models": ["claude-sonnet-5"]}'
Step 3: Configure Claude Code
Set environment variables so Claude Code uses LiteLLM and sends your LiteLLM key for proxy auth:
# Point Claude Code to your LiteLLM proxy
export ANTHROPIC_BASE_URL="http://localhost:4000"
# Model name from your config
export ANTHROPIC_MODEL="claude-sonnet-5"
# LiteLLM proxy auth: this is added to every request
# Use x-litellm-api-key so the proxy authenticates you; your Anthropic credential goes
# via Authorization (from /login) or x-api-key (if you configured a key directly)
export ANTHROPIC_CUSTOM_HEADERS="x-litellm-api-key: $LITELLM_API_KEY"
Replace sk-<your-litellm-api-key> with your actual LiteLLM virtual key.
For multiple headers, use newline-separated values:
export ANTHROPIC_CUSTOM_HEADERS="x-litellm-api-key: $LITELLM_API_KEY
x-litellm-user-id: my-user-id"
Step 4: Sign In with Claude Code
-
Launch Claude Code:
claude -
Use
/loginand sign in with your Anthropic account (or use your API key directly). -
Claude Code sends
x-litellm-api-key(your LiteLLM key, fromANTHROPIC_CUSTOM_HEADERS) plus one of the following, depending on how you authenticated in step 2:Authorization: Bearer <oauth-token>, if you used/loginx-api-key, if you configured an Anthropic API key directly
-
LiteLLM authenticates you via
x-litellm-api-key. It forwards the OAuthAuthorizationheader to Anthropic automatically; forwardingx-api-keyadditionally requiresforward_llm_provider_auth_headers: true. Either way, your Anthropic credential takes precedence over any proxy-configured key.
Summary
| Header | Source | Purpose | Needs forward_llm_provider_auth_headers? |
|---|---|---|---|
Authorization: Bearer <token> | Claude Code /login (OAuth) | Sent to Anthropic for API calls | No, forwarded by default |
x-api-key | Configured Anthropic API key | Sent to Anthropic for API calls | Yes |
x-litellm-api-key | ANTHROPIC_CUSTOM_HEADERS | Proxy authentication, tracking, rate limits | N/A |
Troubleshooting
Requests fail with "invalid x-api-key"
This applies to the configured-API-key path, not /login.
- Ensure
forward_llm_provider_auth_headers: trueis set inlitellm_settings(orgeneral_settings). - Restart the LiteLLM proxy after changing the config.
- Verify your Anthropic API key is configured correctly on the client.
Proxy returns 401
- Check that
ANTHROPIC_CUSTOM_HEADERSincludesx-litellm-api-key: <your-key>. - Ensure the LiteLLM key is valid and has access to the model.
Proxy key is used instead of my Anthropic key
This applies to the x-api-key path; the OAuth Authorization header from /login is forwarded regardless of this setting.
- Confirm
forward_llm_provider_auth_headers: trueis in your config. - The setting can be in
litellm_settingsorgeneral_settingsdepending on your config structure. - Enable debug logging:
LITELLM_LOG=DEBUGto see which key is being forwarded.
Model is unavailable for everyone after one user hits their limit
If a healthy account receives 429 No deployments available for selected model, ask the proxy operator to check for an existing deployment cooldown. Wait for it to expire, or clear it with the steps below.
Recovery before expiry briefly interrupts proxy traffic. Have the proxy operator:
- Stop all proxy workers that use the affected deployment.
- If Redis is configured, remove only that deployment's
deployment:<model_id>:cooldownkey using your Redis management tool. Use the deployment'smodel_info.id, not its model name, and include the configured Redis namespace prefix if present. Preserve all other keys. - Start the proxy workers and send a request from a healthy account. Confirm it succeeds in Logs.
With Redis, restarting workers alone can reload the old entry. Without Redis, restarting all workers clears the in-memory cooldown. This recovery does not reset the provider's account quota.
Related
- Forward Client Headers — Full BYOK and header forwarding docs
- Claude Code Max Subscription — Using Claude Code with OAuth/Max subscription through LiteLLM