Skip to content

Agents and multimodal systemsGuide

Route Claude Code and Codex through an AI gateway: setup and caps

Coding agents are now one of the largest lines on many AI bills, and most of that spend runs on keys nobody can attribute. Routing Claude Code and Codex through a gateway fixes attribution, caps and offboarding with two settings per tool. Here is the exact configuration.

By INFRO Editorial

Published

6 min read

Several thin amber lines from separate terminals converging into one glass gateway node on a dark ground, then continuing as one controlled line

In short

Claude Code reads ANTHROPIC_BASE_URL and a credential in ANTHROPIC_AUTH_TOKEN (sent as Authorization: Bearer) or ANTHROPIC_API_KEY (sent as x-api-key); administrators distribute both through managed settings and can pin the gateway with allowedProviders. Codex takes a model_providers entry in config.toml with base_url, env_key and wire_api = responses. Give each developer their own gateway key with a spend limit, so usage is attributed and offboarding revokes one credential. Anthropic does not support Claude Code on non-Claude models.

Key takeaways

  • Claude Code: ANTHROPIC_BASE_URL plus ANTHROPIC_AUTH_TOKEN (Bearer) or ANTHROPIC_API_KEY (x-api-key); check the result with /status.
  • Codex: a [model_providers.<id>] entry with base_url, env_key and wire_api = "responses", the only value Codex accepts.
  • One gateway key per developer gives attribution, a spend limit each, and offboarding by revoking one credential.
  • Administrators can pin Claude Code to the gateway with allowedProviders = ["customEndpoint"] in managed settings.
  • Anthropic does not support Claude Code on non-Claude models; fail over between Claude providers, not to other model families.

How do you route Claude Code through a gateway? Set ANTHROPIC_BASE_URL to the gateway and put a gateway credential in ANTHROPIC_AUTH_TOKEN (sent as Authorization: Bearer) or ANTHROPIC_API_KEY (sent as x-api-key)1. Codex takes a custom model provider in ~/.codex/config.toml with base_url, env_key and wire_api = "responses"2. Give every developer their own gateway key, and each one gets a spend limit, attributed usage and a clean offboarding.

Coding agents run long sessions with large contexts, and they run them for every engineer at once. That makes them one of the fastest-growing lines on many AI bills, and usually the least attributed one. This guide gives the exact configuration for both tools, then the controls worth putting on them.

Why put coding agents behind a gateway

Anthropic's own gateway page lists what one gives an organization: provider keys stay server-side while developers hold gateway credentials; usage is attributed per developer or team; budgets and rate limits are enforced in one place; every request is logged; and the provider can be changed in the gateway without touching developer machines3. It also states the trade-off: the gateway becomes infrastructure you operate, and it has to keep forwarding what new Claude Code releases send3.

One billing consequence is easy to miss. While a gateway credential or apiKeyHelper is active, a developer's claude.ai subscription limits do not apply to those requests; they are billed per token to whoever owns the credential the gateway forwards3. Moving a team onto a gateway moves its coding-agent cost from subscriptions to your API account, so plan the budget before the rollout, not after the first invoice. Coding agent costs for teams covers the numbers.

Claude Code: the configuration

shell
export ANTHROPIC_BASE_URL="https://llm-gateway.example.com"
export ANTHROPIC_AUTH_TOKEN="gateway-key-for-this-developer"

The credential variable decides the header. ANTHROPIC_AUTH_TOKEN sends Authorization: Bearer; ANTHROPIC_API_KEY sends x-api-key; an apiKeyHelper command sends both. A credential in the variable the gateway does not read fails with 401, and Anthropic's advice when you were not told which kind you have is to start with ANTHROPIC_AUTH_TOKEN1.

Shell exports last for one terminal. To make the settings apply everywhere Claude Code runs, including background agents, put them in the env block of ~/.claude/settings.json, and never in a project's committed .claude/settings.json1:

~/.claude/settings.json
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://llm-gateway.example.com",
    "ANTHROPIC_AUTH_TOKEN": "gateway-key-for-this-developer",
    "ANTHROPIC_CUSTOM_HEADERS": "X-Team: payments"
  }
}

Then verify. Run /status: an Anthropic base URL line confirms the gateway address, and an Auth token or API key line confirms the gateway credential is active rather than a saved claude.ai login1. Before that, a one-token curl to $ANTHROPIC_BASE_URL/v1/messages tells you whether a failure is the gateway or your configuration.

Useful extras from the same page:

  • ANTHROPIC_CUSTOM_HEADERS adds headers such as a team or routing key, one Name: Value per line.
  • CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1 adds the gateway's model list to the /model picker.
  • apiKeyHelper runs a command to fetch a rotating credential, cached for five minutes by default.
  • CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 turns off version checks and telemetry outside the gateway path, at the cost of auto-updates1.

Rolling it out to a team

Anthropic's rollout sequence is: give the gateway your provider credential, issue each developer a gateway credential, distribute the base URL and credential through managed settings and your secrets tooling, and have each developer check /status3. To make the gateway the only destination a managed machine may use, set allowedProviders to ["customEndpoint"] in the managed settings file with the gateway's ANTHROPIC_BASE_URL in its env block; Claude Code then refuses sessions pointed anywhere else (v2.1.285 or later)3.

Codex: the configuration

~/.codex/config.toml
model = "gpt-6-sol"
model_provider = "gateway"

[model_providers.gateway]
name = "Company gateway"
base_url = "https://llm-gateway.example.com/v1"
env_key = "GATEWAY_API_KEY"
wire_api = "responses"
http_headers = { "X-Team" = "payments" }

The fields come from Codex's configuration reference: base_url is the provider's API base, env_key names the environment variable holding the key, http_headers adds fixed headers and env_http_headers adds headers from environment variables. wire_api accepts only responses, which is also the default. Codex retries failed HTTP requests four times by default (request_max_retries) and treats a stream idle for five minutes as failed (stream_idle_timeout_ms)2. If your gateway also retries, lower one of the two so a provider error does not become a long stall.

The controls worth turning on

  1. One key per developer, never a shared one. Usage is attributed to a person, a leaver is offboarded by revoking one credential, and a leaked key is contained to one seat.
  2. A spend limit on every key. Coding agents can loop. A daily or monthly limit per key turns a runaway session into a refused request instead of a surprise. Bounding AI agent loops covers step and token limits inside the agent.
  3. Attribution headers. A team or project header on every request lets you report cost per team without parsing anything.
  4. A model allowlist. Decide which models agents may use. The most capable model is rarely needed for every turn, and an allowlist makes the choice deliberate.
  5. MCP tool overhead. Every tool definition is input on every turn unless it is deferred; large MCP tool lists are a quiet multiplier on agent cost. MCP vs CLI token overhead measures it.

Failover: stay on Claude for Claude Code

Anthropic's gateway page is explicit that it does not support routing Claude Code to non-Claude models through any gateway3. A gateway may be able to translate the request, but the agent's behaviour depends on the model it was built for. For resilience, give Claude Code's traffic Claude targets on more than one provider — for example the Claude API and Claude on Google Cloud — rather than a fallback to another model family.

The errors you will meet first

SymptomUsual causeFix
401 on every requestCredential in the variable the gateway does not readSwitch between ANTHROPIC_AUTH_TOKEN and ANTHROPIC_API_KEY
Claude Code asks you to log in, though curl worksNo credential reached the CLI before first-run setupSet the token in a shell export or ~/.claude/settings.json
400 naming unknown fields or betasThe gateway's upstream rejects fields Claude Code sendsCLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1, or update the gateway
403 with an HTML body, nothing in the gateway's logsA web application firewall blocked the prompt bodyExempt /v1/messages from body inspection
TLS errors while curl worksCorporate TLS inspectionNODE_EXTRA_CA_CERTS pointing at the CA bundle
Adapted from the troubleshooting table on Anthropic's Claude Code gateway connection page, 2026-10-20.

Using INFRO as the gateway

INFRO answers Claude Code on POST /v1/messages in Anthropic's Messages shape and Codex on POST /v1/responses in OpenAI's Responses shape, so both tools work with the base URL and an INFRO gateway key: ANTHROPIC_BASE_URL=https://api.infro.io for Claude Code, base_url = "https://api.infro.io/v1" for Codex. On an Anthropic or Claude-on-Vertex connection the body Claude Code sends reaches the provider as written, including thinking blocks and cache_control breakpoints; set ENABLE_TOOL_SEARCH=true and MCP tools stay deferred. Each request runs on your own provider account, after your routes, allowlists and budgets; a key's spend limit refuses a request it cannot cover with 402 before the provider sees it, and the request log shows spend per key. X-INFRO-Workflow and the other attribution headers can be sent through ANTHROPIC_CUSTOM_HEADERS or Codex's http_headers.

Summary

Two settings per tool move coding agents onto a gateway. Do it with one key per developer, a spend limit on every key, attribution headers and managed settings that pin the destination, and keep Claude Code's failover between Claude providers.

Questions readers ask

How do I point Claude Code at a proxy or gateway?
Set ANTHROPIC_BASE_URL to the gateway's address and put the gateway credential in ANTHROPIC_AUTH_TOKEN (sent as Authorization: Bearer) or ANTHROPIC_API_KEY (sent as x-api-key). Set them in your shell or in the env block of ~/.claude/settings.json, then run /status to confirm the base URL and credential source.
Does a Claude subscription work through a gateway?
Not for requests that carry a gateway credential. Anthropic's documentation says that while a gateway credential or apiKeyHelper is active, the subscription's usage limits don't apply and the traffic is billed per token to whoever owns the credential the gateway forwards.
How do I configure Codex to use a custom endpoint?
Add a model provider to ~/.codex/config.toml with name, base_url, env_key (the environment variable holding the key) and wire_api = "responses", then set model_provider to its id. Codex sends requests to the base URL's /responses path.
Can Claude Code run on GPT or Gemini through a gateway?
Anthropic's documentation says it does not support routing Claude Code to non-Claude models through any gateway. For failover, route Claude Code between Claude providers, such as the Claude API and Claude on Google Cloud.

From the team that builds it

Where this shows up in INFRO

INFRO is a control plane in front of the provider accounts you already hold. The capabilities below are the parts of it this article touches, each with the documentation that defines its behaviour.

  • Claude Code and Codex through INFRO

    Available with a plan

    Point Claude Code, the Claude Agent SDK, Codex or the OpenAI Agents SDK at INFRO by changing the base URL and the key. Anthropic's Messages API and OpenAI's Responses API, with routing, budgets refused before the call, guardrails and the request log applied as on every other request.

  • Key limits and rotation

    Available with a plan

    Limit a gateway key to chosen providers or IP ranges, and rotate it with an overlap window so the old key keeps working while you deploy the new one.

    Read the documentation for Key limits and rotation
  • Budgets and spend ceilings

    Available with a plan

    A hard cap per organization, project, member, and key, with alerts at your own thresholds.

    Read the documentation for Budgets and spend ceilings
  • Spend attribution

    Available with a plan

    Group cost by model, project, key, or an end-user id you attach to the request.

    Read the documentation for Spend attribution

INFRO accounts are self-serve. Provider usage is billed to you by your providers; INFRO charges a software subscription and takes 0% of model spend. See plans.

Sources

Numbered in order of first citation. Each was opened and checked against the claim on the date shown.

  1. 1.Connect Claude Code to an LLM gateway (opens in a new tab) — Anthropic. Accessed Oct 20, 2026.
  2. 2.Configuration reference (opens in a new tab) — OpenAI. Accessed Oct 20, 2026.
  3. 3.Other LLM gateways (opens in a new tab) — Anthropic. Accessed Oct 20, 2026.

About this article

Client configuration is taken from Anthropic's Claude Code gateway pages and OpenAI's Codex configuration reference, opened on 2026-10-20. INFRO's behaviour is described from its Claude Code and Codex documentation. No benchmark was run. Written by the team that builds INFRO.

Last checked against its sources on . The next review is due by . No separate named reviewer is recorded for this article.

How articles are written, sourced and corrected is set out in the editorial standards.