Claude Code with an API gateway: setup, rates, and options

Last updated 2026-09-07 · Part of the LivRouter guides

Claude Code is Anthropic's terminal coding agent, and it is where API costs bite hardest: the agent re-reads your project on every turn, so a heavy day can burn tens of millions of input tokens. Routing Claude Code through an API gateway instead of the official endpoint is the most common cost optimization in the Claude ecosystem — this guide covers how the routing works, the exact setup, what the rates look like after the swap, and the trade-offs nobody puts in the quickstart.

Your four options for running Claude Code

OptionHow it billsTrade-off
Official Anthropic APIOfficial per-token ratesFull first-party support; highest per-token cost
Claude subscription (Pro/Max)Flat monthly fee, session limitsPredictable cost for light use; rate limits and usage caps apply, and heavy agent sessions exhaust them
Multi-model gateway (e.g. OpenRouter)Platform per-model creditsModel breadth; verify Claude-specific tooling compatibility
Anthropic-compatible gateway (e.g. LivRouter)Pay-as-you-go credit at discounted per-model ratesPurpose-built for Claude tooling: ~15–30% of official rates on plans, one bill with your other models

The rest of this guide uses LivRouter as the gateway example — it exposes an Anthropic Messages compatibility layer specifically so Claude Code and Anthropic SDKs work without modification.

Setup: three steps

Prerequisites: Node.js 18+ and a LivRouter API key (create one in the dashboard under API Keys; keys look like sk-livrouter-...).

  1. Install Claude Code: npm install -g @anthropic-ai/claude-code
  2. Point it at the gateway — add these to your ~/.zshrc or ~/.bashrc:
    export ANTHROPIC_BASE_URL="https://api.livrouter.com"
    export ANTHROPIC_API_KEY="your-livrouter-api-key"
  3. Verify: run claude in a project directory, send a short prompt, then confirm the call shows up in your LivRouter usage log with the model name and token counts.

The full walkthrough with troubleshooting lives in the Claude Code docs section.

What the rates look like after routing

The gateway serves the same Claude models at plan-discounted rates (snapshot September 2026; live numbers in the Claude pricing catalog):

ModelOfficial (in / out) per 1MMax plan (in / out) per 1M
claude-sonnet-5$3.00 / $15.00$0.45 / $2.25
claude-opus-5$5.00 / $25.00$0.75 / $3.75
claude-haiku-4-5$1.00 / $5.00$0.15 / $0.75

On the worked-example month from the cost calculator guide — 60M input and 15M output tokens on sonnet — official rates total $405 while the Max plan totals $60.75. Prompt caching compounds the saving because Claude Code caches its system prompt and file context, and cached tokens bill near the 10% cache rate before the plan discount.

The honest trade-offs

  • Provider policy. Anthropic states publicly that it does not endorse, maintain, or audit third-party gateways, and does not support routing Claude Code to non-Claude models through them. Pay-as-you-go API access for Claude requests is the standard API billing path — but if you are on a Claude subscription and considering routing it through unofficial endpoints, read Anthropic's current policy first.
  • Support boundary. Agent-side bugs go to the Claude Code repository; request-side issues (auth, model availability, billing) go to your gateway. LivRouter support runs over email and Discord.
  • Feature latency. Gateway compatibility layers track the Anthropic API surface; brand-new beta endpoints can lag the official API by days. Core chat, streaming, tool use, and vision are stable paths.

Troubleshooting quick list

  • 404 on /v1/v1/messages — you added /v1 to the base URL; remove it.
  • 401 unauthorized — the key is wrong or the shell did not reload; open a fresh terminal or re-source your profile.
  • Model not found — align the model name with the LivRouter catalog naming.
  • Works in one terminal only — the exports were set for the current session only; persist them in your shell profile.

FAQ

Do I add /v1 to ANTHROPIC_BASE_URL?

No. Claude Code (and the Anthropic SDK generally) appends /v1/messages itself, so the base URL must be the bare host — e.g. https://api.livrouter.com. Adding /v1 produces requests to /v1/v1/messages and fails with 404s.

Does routing Claude Code through a gateway violate Anthropic's terms?

Anthropic officially states it does not endorse, maintain, or audit third-party gateway products and does not support routing Claude Code to non-Claude models through gateways. Using a pay-as-you-go API endpoint for Claude requests is the standard API billing path, but review Anthropic's current usage policy for subscription-based routing before relying on it.

Which models can Claude Code use through LivRouter?

Claude Code requests Claude models by name, and LivRouter's compatibility layer serves them from the Claude family in the catalog at LivRouter rates. Switching models inside Claude Code keeps working.

How do I verify my session is actually routed through the gateway?

Send one short prompt after setup, then check the usage log in your LivRouter dashboard — the request should appear with the model name and token counts within moments.