How to Use Claude Code With an API Gateway
AnyRouter3 min read
TL;DR
Claude Code talks to any server that speaks the Anthropic Messages API. Set ANTHROPIC_BASE_URL to the gateway root, set ANTHROPIC_AUTH_TOKEN to your gateway key, and set ANTHROPIC_MODEL to a model the gateway offers. No plugin or code change is needed.
Why run Claude Code through a gateway?
Claude Code sends every request to one base URL, so that URL decides where your traffic goes. Pointing it at a gateway gives you a few things that a single vendor key does not:
- One key for several models. You can try another model by changing an environment variable.
- Failover. If one upstream fails before it starts answering, the gateway can send the request to another one.
- Usage in one place. Requests, tokens and cost are recorded per key, which helps when a team shares a budget.
Step 1: Create an API key
Sign in to the console and create a key under API Keys. Give each machine or teammate its own key, so that usage stays readable and one key can be revoked without touching the others.
Step 2: Set the environment variables
On Linux or macOS:
export ANTHROPIC_BASE_URL=https://ai.cloudtay.comexport ANTHROPIC_AUTH_TOKEN=your_api_keyexport ANTHROPIC_MODEL=your_modelOn Windows PowerShell:
$env:ANTHROPIC_BASE_URL="https://ai.cloudtay.com"$env:ANTHROPIC_AUTH_TOKEN="your_api_key"$env:ANTHROPIC_MODEL="your_model"Use the root address. Claude Code adds /v1/messages by itself, so do not append /v1 or /anthropic.
Pick a model name from the pricing page. To use one model for everything, including the background tasks Claude Code runs on a smaller model, also set the tier overrides:
export ANTHROPIC_DEFAULT_OPUS_MODEL=your_modelexport ANTHROPIC_DEFAULT_SONNET_MODEL=your_modelexport ANTHROPIC_DEFAULT_HAIKU_MODEL=your_modelStep 3: Start Claude Code
cd /path/to/my-projectclaudeSend a short prompt, then open the usage page in the console. The request should appear there with the model you chose. If it does, the setup is working.
Step 4: Make it permanent
Environment variables only last for the current shell. To keep the setup, put the same values under env in ~/.claude/settings.json:
{ "env": { "ANTHROPIC_BASE_URL": "https://ai.cloudtay.com", "ANTHROPIC_AUTH_TOKEN": "your_api_key", "ANTHROPIC_MODEL": "your_model" }}The Claude Code guide covers the remaining options.
How do you fix common Claude Code gateway errors?
| Symptom | Likely cause | Fix |
|---|---|---|
401 | The key is wrong, disabled or expired | Copy the key again and check that it is active |
404 on /v1/v1/messages | /v1 was added to the base URL | Use the root address |
| Model not found | The name does not match a listed model | Copy the exact name from the pricing page |
| Claude Code still uses the old account | A login or an earlier variable takes priority | Check for old ANTHROPIC_ variables and remove them |
Frequently asked questions
Do I need an Anthropic account to use Claude Code with a gateway?
No. Claude Code only needs a server that speaks the Anthropic Messages API and a token that server accepts.
Can Claude Code use non-Claude models?
Yes, when the gateway translates the Anthropic Messages format to the protocol of the upstream model. Tool calling is what Claude Code depends on most, so test a real coding task with any model you plan to keep.
What is the difference between ANTHROPIC_API_KEY and ANTHROPIC_AUTH_TOKEN?
ANTHROPIC_API_KEY is sent in the x-api-key header, and ANTHROPIC_AUTH_TOKEN is sent as an Authorization: Bearer header. Gateways usually document the one they expect. Use ANTHROPIC_AUTH_TOKEN here.
Next steps
- Read the Base URL FAQ if your client uses a different field name.
- Compare models and prices on the pricing page.