Using a Custom API Key with Claude Code
Claude Code can connect to LLMrouter instead of sending requests directly to the Anthropic API.
Prerequisites
Before starting, you need:
- Claude Code installed
- At least one provider has been enabled in the LLMrouter routing strategy
- An API key has been created in LLMrouter
Example values:
Base URL: https://proxy.llmrouter.eu
API key: sk-llmrouter-example
Model: claude-sonnet-5
Environment variables
Configure the following environment variables before starting Claude Code:
| Variable | Value |
|---|---|
ANTHROPIC_BASE_URL | Base URL of the custom API |
ANTHROPIC_AUTH_TOKEN | Custom API key created in LLMrouter |
ANTHROPIC_DEFAULT_SONNET_MODEL | Optional Exact model name copied from the LLMrouter model table |
ANTHROPIC_AUTH_TOKEN is recommended because Claude Code sends it as:
Authorization: Bearer <API_KEY>
Windows PowerShell
Configure the current terminal session
$env:ANTHROPIC_BASE_URL = "https://proxy.llmrouter.eu"
$env:ANTHROPIC_AUTH_TOKEN = "sk-llmrouter-example"
$env:ANTHROPIC_DEFAULT_SONNET_MODEL = "claude-sonnet-5"
Start Claude Code:
claude
These variables remain active only until the PowerShell window is closed.
Save the configuration permanently
[Environment]::SetEnvironmentVariable(
"ANTHROPIC_BASE_URL",
"https://proxy.llmrouter.eu",
"User"
)
[Environment]::SetEnvironmentVariable(
"ANTHROPIC_AUTH_TOKEN",
"sk-llmrouter-example",
"User"
)
[Environment]::SetEnvironmentVariable(
"ANTHROPIC_DEFAULT_SONNET_MODEL",
"claude-sonnet-5",
"User"
)
Close and reopen PowerShell after running these commands.
Verify the configuration:
$env:ANTHROPIC_BASE_URL
$env:ANTHROPIC_DEFAULT_SONNET_MODEL
Avoid printing ANTHROPIC_AUTH_TOKEN in shared terminals or screenshots.
Linux and macOS
Configure the current shell:
export ANTHROPIC_BASE_URL="https://proxy.llmrouter.eu"
export ANTHROPIC_AUTH_TOKEN="sk-llmrouter-example"
export ANTHROPIC_DEFAULT_SONNET_MODEL="claude-sonnet-5"
Start Claude Code:
claude
To persist the configuration, add the variables to ~/.bashrc or ~/.zshrc, but consider storing the API key in a dedicated secret manager instead of a plain-text shell configuration file.
Selecting a model
A model can be selected when Claude Code starts:
claude --model claude-sonnet-5
It can also be changed inside Claude Code:
/model
The values must exactly match the model names exposed by LLMrouter.
Verifying the connection
Start Claude Code and execute:
/status
Then send a simple prompt:
Reply with the name of the model you are using.
Common problems
For more troubleshooting information, see the official Claude Code documentation.
Claude Code still uses a Claude subscription
ANTHROPIC_AUTH_TOKEN takes precedence over a saved Claude.ai login. Make sure the variable is available in the same terminal from which Claude Code is started.
Check it without displaying the complete secret:
if ($env:ANTHROPIC_AUTH_TOKEN) { "Token is configured" }
HTTP 401 Unauthorized
Possible causes:
- The API key is invalid or expired.
ANTHROPIC_API_KEYwas used instead ofANTHROPIC_AUTH_TOKEN.- The key is not authorized to access the selected model.
- Claude Code was started before the environment variables were configured.
- No provider has been enabled in the LLMrouter routing strategy
Remove-Item Env:ANTHROPIC_API_KEY -ErrorAction SilentlyContinue
$env:ANTHROPIC_AUTH_TOKEN = "sk-llmrouter-example"
HTTP 404 Not Found
Do not include /v1/messages in ANTHROPIC_BASE_URL. Normally the base URL should be:
https://proxy.llmrouter.eu
Model not found
The model name passed by Claude Code must exactly match a model alias configured in LLMrouter:
$env:ANTHROPIC_DEFAULT_SONNET_MODEL = "exact-llmrouter-model-name"
A button for copying the model name is available next to each model in the LLMrouter model table.
Alternatively:
claude --model exact-llmrouter-model-name
Requests still go to api.anthropic.com
Confirm that the variable is visible:
$env:ANTHROPIC_BASE_URL
Then completely restart Claude Code and the terminal or IDE from which it is launched.
Security recommendations
- Create a personal API key and store it securely.
- Never commit the key to Git.
- Avoid including the key directly in scripts, documentation, screenshots, or CI logs.
- Rotate the key immediately if it is accidentally exposed.
While custom authentication is active, Claude Code does not consume the user's Claude Pro or Max subscription allowance. Usage is accounted for and billed through the configured gateway or its upstream provider.