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:

bash124 B
export ANTHROPIC_BASE_URL=https://ai.cloudtay.comexport ANTHROPIC_AUTH_TOKEN=your_api_keyexport ANTHROPIC_MODEL=your_model

On Windows PowerShell:

powershell124 B
$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:

bash143 B
export ANTHROPIC_DEFAULT_OPUS_MODEL=your_modelexport ANTHROPIC_DEFAULT_SONNET_MODEL=your_modelexport ANTHROPIC_DEFAULT_HAIKU_MODEL=your_model

Step 3: Start Claude Code

bash29 B
cd /path/to/my-projectclaude

Send 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:

json167 B
{    "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?

SymptomLikely causeFix
401The key is wrong, disabled or expiredCopy the key again and check that it is active
404 on /v1/v1/messages/v1 was added to the base URLUse the root address
Model not foundThe name does not match a listed modelCopy the exact name from the pricing page
Claude Code still uses the old accountA login or an earlier variable takes priorityCheck 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

One API for every major model

Use the OpenAI or Anthropic SDK you already have. Failover and billing are handled for you.