Claude Code is Anthropic's official command-line coding agent, but official access assumes a working account and a reliable connection to Anthropic's endpoint. If either of those is a problem for you, the easiest workaround is routing it through an API relay. Here's how to set it up, what to look for in a provider, and how to debug the errors you'll actually hit.

How Claude Code talks to a relay

Under the hood, Claude Code just calls the Anthropic API. It supports overriding the endpoint and auth token via environment variables, so as long as your relay exposes an Anthropic-compatible interface, pointing the endpoint at the relay and swapping in its key is all it takes. Two variables do the work:

  • ANTHROPIC_BASE_URL: set to the API address your relay gives you;
  • ANTHROPIC_AUTH_TOKEN (or ANTHROPIC_API_KEY): set to the key the relay issued.

Exact variable names can vary slightly, so check the relay's own docs — any legitimate provider will publish a "Claude Code setup" page you can copy from directly. Set these in your shell config (.zshrc / .bashrc) to make them permanent, export them for a single session, or store them in a Claude Code settings file as described below.

Claude Code settings.json: where the relay URL and key go

Instead of exporting variables in your shell, you can put the same two values in a Claude Code settings file. Settings files are JSON, and the env field holds a map of environment variables that Claude Code applies to its own process. Pointing it at a relay looks like this:

{
  "env": {
    "ANTHROPIC_BASE_URL": "https://your-relay.example/",
    "ANTHROPIC_AUTH_TOKEN": "your-relay-key"
  }
}

ANTHROPIC_BASE_URL overrides the API endpoint so requests go through a proxy or gateway. For the key, Anthropic's documentation describes ANTHROPIC_AUTH_TOKEN as a custom value for the Authorization header (sent with a Bearer prefix) and ANTHROPIC_API_KEYas a key sent in the X-Api-Key header. Which one a relay expects depends on the relay, so use whichever its own Claude Code setup page tells you to. Per the same docs, when a variable is set both in your shell and in a settings file's env block, the settings file value applies in most sessions.

Which file you put it in decides who it applies to:

  • ~/.claude/settings.json (user level): applies to you, in every project on that machine. This is the usual home for a personal relay key.
  • .claude/settings.json(project level): applies to everyone working in that project, and is meant to be committed to source control so teammates get the same settings. Don't put a personal relay key here unless you really want it shared with everyone who clones the repo.
  • .claude/settings.local.json (project-local): applies to you in that one project only, and Claude Code keeps it out of git when it creates the file. If you create it by hand, add it to your .gitignore yourself.

For the full list of settings files, precedence rules, and environment variables, see Anthropic's Claude Code settings documentation.

Using Claude Code with OpenRouter or another gateway

Nothing in the settings above is specific to one relay. Claude Code just needs an endpoint that speaks Anthropic's API, so any gateway that offers one is configured with the same fields. OpenRouter is one example that publishes its own Claude Code guide: OpenRouter's Claude Code integration page. It uses the same variables described earlier in this article, so it is the same set of settings with OpenRouter's address and key swapped in. Per that page:

  • set ANTHROPIC_BASE_URL to https://openrouter.ai/api (the page says to use the full URL including the scheme);
  • set ANTHROPIC_AUTH_TOKEN to your OpenRouter API key;
  • set ANTHROPIC_API_KEY to an empty string, which the page marks as important so Claude Code does not fall back to authenticating against Anthropic directly.

The page gives a settings-file version of this too, in .claude/settings.local.json:

{
  "env": {
    "ANTHROPIC_BASE_URL": "https://openrouter.ai/api",
    "ANTHROPIC_AUTH_TOKEN": "<your-openrouter-api-key>",
    "ANTHROPIC_API_KEY": ""
  }
}

The same page also says to run /logout once if you were previously signed in to Claude Code with an Anthropic account, and that Claude Code with OpenRouter is only guaranteed to work with the Anthropic first-party provider. It lists further optional settings and troubleshooting steps; read it directly before relying on this summary, since the details can change. For a different gateway, use the address and key that gateway documents in the same fields.

What to look for in a relay for Claude Code specifically

Claude Code is a heavy, long-running workload, not the occasional one-off question — so the bar for picking a provider is higher:

  • Explicit Claude Code support: the provider's docs should have a dedicated Claude Code / Anthropic setup guide, not a generic OpenAI-format writeup you're trying to bend to fit;
  • Current, complete model coverage: Claude Code defaults to a fairly recent Claude model, so confirm the relay has that exact version available;
  • Stability over rock-bottom price: frequent dropouts or truncated responses seriously hurt coding productivity — a provider with a slightly higher multiplier but solid uptime is usually the better deal, as covered in what a billing multiplier actually is;
  • Transparent pricing: Claude Code burns through tokens fast, so check input/output multipliers up front and test with a small deposit before scaling up.

Debugging common errors

401 / authentication failed

Usually a wrong key, or the wrong variable name (using API_KEY when the provider expects ANTHROPIC_AUTH_TOKEN). Double-check the provider's docs for the exact variable name, and confirm the key has no stray whitespace and hasn't expired.

Connection timeouts / 5xx errors

Usually upstream instability or throttling on the relay's end. First confirm BASE_URL is correct; try again at a different time; if it stays flaky, that upstream isn't reliable enough and it's time to switch or fall back to a second provider.

Response quality drops noticeably

Watch for silent model substitution — you think you're on the flagship Claude model, but the backend has quietly routed you to something smaller and cheaper. It shows up as a sudden drop in coding ability. Switch providers the moment you see this; it's one of the worst things a relay can do.

"Model not found" / unsupported

Means the relay hasn't added the model version Claude Code defaults to yet. Either switch to a provider that already has it, or explicitly pin a model it does support per its docs.

The short version

Wiring Claude Code up to a relay is just two environment variables — the hard part is finding a provider that's stable, current on models, and doesn't silently swap models on you. Start small, and keep a backup provider ready. To find relays that explicitly support coding-agent workloads, browse the HowToken directory and filter by model, then test a couple with a small deposit. Before you commit, it's worth reading 7 things to check before choosing a relay.

FAQ

What is ANTHROPIC_BASE_URL?

ANTHROPIC_BASE_URL is an environment variable that Claude Code reads to override the API endpoint, so requests go through a proxy or gateway instead of Anthropic's own address. Per Anthropic's environment variables reference, a non-Anthropic host also changes some defaults, for example MCP tool search is disabled by default. You can export it in your shell or put it in the env block of a Claude Code settings file such as ~/.claude/settings.json, with the address your relay or gateway gives you.

Source: Claude Code environment variables reference.