Claude Code Proxy: Set a Custom Base URL and Auth Token

Route Claude Code through a proxy with a custom base URL and auth token. Covers environment variables, settings.json, and the base URL mistake that breaks setups.

O
OurToken Team//11 min
Claude Code Proxy: Set a Custom Base URL and Auth Token

A Claude Code proxy setup comes down to two environment variables and one line you must not add. Point ANTHROPIC_BASE_URL at a gateway instead of Anthropic, provide a gateway credential through ANTHROPIC_AUTH_TOKEN, and Claude Code sends its Messages API traffic to your chosen endpoint. The line you must not add is /v1 at the end of that base URL — Claude Code appends /v1/messages itself, and the most common proxy failure is a request path that comes out as /v1/v1/messages.

This guide walks through the full configuration: which variables do what, the exact settings.json block for routing Claude Code through OurToken's OpenAI-compatible gateway, how to verify the connection, how to map which Claude model answers, and how the proxy cost compares with paying Anthropic directly. It is a configuration tutorial, not a product comparison — the goal is a working proxy setup on your machine.

Verification status: Configuration values and model pricing in this article are dated 2026-10-09 and were verified against the live OurToken Claude Code custom API guide, the Claude Opus 4.8 model page, and the Claude Sonnet 4.6 model page, plus the official Claude Code settings reference. Prices and variable names can change; confirm both before committing a team to a proxy.

Why Route Claude Code Through a Proxy

Claude Code is built to talk to the Anthropic API, but its transport is configurable. The two things a proxy changes are where requests go and how they are billed.

Where requests go. By default Claude Code reaches api.anthropic.com with either a claude.ai OAuth login or an Anthropic API key. A proxy changes that destination to a third-party gateway that speaks the same Messages API shape. Nothing about the prompts, the tools, or the agent loop changes; the request just lands on a different server that forwards it.

How requests are billed. With a saved claude.ai login, usage is limited by your subscription plan. With ANTHROPIC_AUTH_TOKEN set to a gateway key, Claude Code switches to API-key authentication, and the gateway bills the tokens you actually use at its own rates. This is the practical reason teams set up a proxy: the Claude Code alternative discussion usually lands on "use the coding agent, but pay per token instead of per seat."

The setup is the same whether your gateway serves real Claude models or other Anthropic-compatible routes. What differs is only the base URL, the token, and the model IDs.

The Two Environment Variables That Do the Work

Claude Code reads its gateway configuration from environment variables, and two of them matter for a proxy. Understanding the difference between them prevents the most time-consuming misconfiguration.

VariableEffect
ANTHROPIC_BASE_URLThe gateway root URL. Claude Code appends /v1/messages to this value.
ANTHROPIC_AUTH_TOKENSent as Authorization: Bearer <value>. Use this for a bearer-token gateway.
ANTHROPIC_API_KEYSent as x-api-key instead. If both are set, ANTHROPIC_AUTH_TOKEN wins.

The distinction between the last two is a classic trial-and-error trap. A gateway that expects Authorization: Bearer will reject an x-api-key header, and vice versa. OurToken's gateway expects a bearer token, so the correct variable is ANTHROPIC_AUTH_TOKEN. If you set ANTHROPIC_API_KEY by habit from the raw API docs, you will get an authentication error that looks confusingly like a valid setup.

There are also provider-specific variables such as ANTHROPIC_BEDROCK_BASE_URL for the AWS Bedrock route, but a standard OpenAI-compatible or Anthropic-compatible gateway uses the two variables above.

The base URL rule

ANTHROPIC_BASE_URL takes the gateway root, not the full endpoint. For OurToken the correct value is:

https://api.ourtoken.ai

Do not append /v1, /v1/messages, or anything else. Claude Code constructs the Messages API path itself, so the final request goes to https://api.ourtoken.ai/v1/messages. If you set the base URL to https://api.ourtoken.ai/v1, Claude Code still appends its own /v1/messages and the request lands on a non-existent /v1/v1/messages path.

This is the single most common Claude Code proxy failure, and it is worth stating twice: the base URL is the root, not the versioned endpoint.

Configure Claude Code Through OurToken

The configuration lives in Claude Code's settings.json file. Variables inside the env block are read once at startup, so any change requires restarting the claude process afterward.

The file path depends on your platform:

  • macOS / Linux: ~/.claude/settings.json
  • Windows: C:\Users\<username>\.claude\settings.json

Open that file and place the following block inside it, replacing the token with your own OurToken API key (create one on the OurToken API Keys page):

{
  "env": {
    "ANTHROPIC_AUTH_TOKEN": "your ourtoken api key",
    "ANTHROPIC_BASE_URL": "https://api.ourtoken.ai",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL": "claude-opus-4-8",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL_NAME": "claude-opus-4-8",
    "ANTHROPIC_DEFAULT_OPUS_MODEL": "claude-opus-4-8",
    "ANTHROPIC_DEFAULT_OPUS_MODEL_NAME": "claude-opus-4-8",
    "ANTHROPIC_DEFAULT_SONNET_MODEL": "claude-sonnet-4-6",
    "ANTHROPIC_DEFAULT_SONNET_MODEL_NAME": "claude-sonnet-4-6",
    "ANTHROPIC_MODEL": "claude-sonnet-4-6",
    "ENABLE_TOOL_SEARCH": "true"
  },
  "model": "sonnet",
  "effortLevel": "medium",
  "autoUpdatesChannel": "latest"
}

This block does three things at once. It sets the destination and credential (ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN), it makes Sonnet 4.6 the default interactive model (ANTHROPIC_MODEL), and it remaps the built-in Opus, Sonnet, and Haiku tiers to OurToken model IDs so that background work, plan mode, and the model picker all resolve to real gateway routes.

Verify the connection

After saving settings.json, restart Claude Code and confirm the route took effect.

claude

Once inside, two checks tell you the proxy is live. First, run /status and confirm the Anthropic base URL line shows https://api.ourtoken.ai rather than api.anthropic.com. Second, send a trivial message:

Hello

A normal reply means the request reached the gateway and came back through the Messages API. If instead you see a login prompt at startup, the credential did not reach the process — which usually means the env block is malformed or Claude Code was not restarted after the change.

You can also confirm on the OurToken dashboard that the request produced an API call record and a small balance deduction. If the base URL is wrong, the failure is silent and fast: Claude Code reports a network or 404 error rather than a model response.

Model Mapping: Which Claude Model Answers

A proxy does not change which models exist; it changes which model ID each tier resolves to. Claude Code has a fixed idea of "Opus," "Sonnet," and "Haiku," and it sends built-in requests to those tiers for background tasks. If those tiers point at IDs the gateway does not serve, background requests fail even though your interactive prompt works.

The OurToken block above maps them deliberately:

Tier variableOurToken model IDRole
ANTHROPIC_MODELclaude-sonnet-4-6Default interactive model (the picker's sonnet)
ANTHROPIC_DEFAULT_OPUS_MODELclaude-opus-4-8The opus tier, used for harder tasks and plan mode
ANTHROPIC_DEFAULT_HAIKU_MODELclaude-opus-4-8The haiku tier, remapped because OurToken has no Haiku route

The Haiku remapping is worth understanding. Claude Code uses a Haiku-class model for cheap background work. OurToken does not currently list a Haiku route, so the block points the Haiku tier at Opus 4.8 as a stand-in. That works functionally, but it is worth knowing that those background requests will bill at Opus rates. If your gateway later adds a small-model route, remap ANTHROPIC_DEFAULT_HAIKU_MODEL to it to reclaim those savings.

Both claude-opus-4-8 and claude-sonnet-4-6 are OpenAI-compatible model IDs served by OurToken through the Messages endpoint https://api.ourtoken.ai/v1/messages. Use those exact strings; a display name like "Claude Sonnet 4.6" is not a model ID.

What It Costs: Proxy vs Official

The proxy's advantage is the price. OurToken lists its Claude routes at a flat 40% of the official Anthropic reference, which is where the savings comes from.

At the time of writing, the two Claude models you can route Claude Code to are priced as follows:

Token categoryClaude Opus 4.8 (OurToken)Opus officialClaude Sonnet 4.6 (OurToken)Sonnet official
Input$2.00 / 1M$5.00 / 1M$1.20 / 1M$3.00 / 1M
Output$10.00 / 1M$25.00 / 1M$6.00 / 1M$15.00 / 1M
Cached input$0.20 / 1M$0.50 / 1M$0.12 / 1M$0.30 / 1M
Cache writes$2.50 / 1M$6.25 / 1M$1.50 / 1M$3.75 / 1M

The gap is large on output, which is where coding agents spend most of their tokens. A long Claude Code session can emit tens of thousands of output tokens across its reasoning and file edits; at $10.00 versus $25.00 per million output tokens, the proxy route is 60% cheaper on the line item that dominates the bill.

The trade-off is pay-as-you-go versus subscription. The Claude Code subscription path caps usage and includes the claude.ai login flow; a proxy with ANTHROPIC_AUTH_TOKEN set switches to API-key billing with no per-seat cap. Which is cheaper depends on your volume, but the proxy gives you a per-token rate you can compare directly against the official reference, and it bills only what you use. For a team that already has an OurToken key for other models, adding Claude Code to the same balance and dashboard is a single config block, not a new contract.

The same base URL and auth token work for the other tools in the OurToken integration set, so one key covers Claude Code, the Codex CLI custom API route, and the OpenCode custom provider route if you use those alongside it.

Set the Proxy for One Session Without Editing settings.json

The settings.json block is the durable setup, but you can also pass the variables inline when you launch Claude Code. This is useful for testing a proxy before committing it, for a throwaway machine, or for keeping a work gateway separate from a personal one.

On macOS or Linux:

export ANTHROPIC_BASE_URL="https://api.ourtoken.ai"
export ANTHROPIC_AUTH_TOKEN="your ourtoken api key"
claude

On Windows PowerShell:

$env:ANTHROPIC_BASE_URL = "https://api.ourtoken.ai"
$env:ANTHROPIC_AUTH_TOKEN = "your ourtoken api key"
claude

The inline form behaves identically to the env block: variables are read once at startup, so change them before launching, not mid-session. If a claude.ai login is saved on the machine, the inline ANTHROPIC_AUTH_TOKEN overrides it for that session, switching the session to API-key billing against the gateway. When you want to go back, unset the two variables and restart.

This is also the cleanest way to isolate profiles. A separate shell that exports the proxy variables, plus a CLAUDE_CONFIG_DIR pointing at a different settings directory, gives you a completely separate gateway identity without touching your primary configuration. That is more than most single-developer setups need, but it prevents a work proxy from contaminating personal usage and vice versa.

Troubleshooting a Claude Code Proxy

Most proxy failures fall into five buckets, and each has a specific signature.

SymptomLikely causeFix
Login prompt at startupenv block or variables did not loadCheck settings.json for JSON errors, then restart claude (variables are read once at startup)
/v1/v1/messages or 404 errorANTHROPIC_BASE_URL ends in /v1Set the base URL to the root, e.g. https://api.ourtoken.ai, with no path suffix
401 UnauthorizedWrong auth variable or tokenUse ANTHROPIC_AUTH_TOKEN for a bearer gateway; confirm the key is valid and not expired
400 with an x-api-key rejectionGateway expects a bearer tokenSwap ANTHROPIC_API_KEY for ANTHROPIC_AUTH_TOKEN
Interactive works but background requests failA model tier maps to an ID the gateway does not serveCheck ANTHROPIC_DEFAULT_OPUS_MODEL / _SONNET_MODEL / _HAIKU_MODEL against the gateway's model list

A /status check early in the session resolves most of these before you waste time on a request. It prints the resolved Anthropic base URL, so you can confirm the destination is the gateway and not api.anthropic.com. If /status still shows the official endpoint, the proxy variables are not in effect — stop and fix the configuration before sending real work through the wrong route.

One subtle case: ANTHROPIC_AUTH_TOKEN and ANTHROPIC_API_KEY both set. When both are present, ANTHROPIC_AUTH_TOKEN wins, and the x-api-key header is not sent. If you previously configured a raw Anthropic key and then added a gateway token, clear the old ANTHROPIC_API_KEY (set it to an empty string or remove it) so a leftover key does not produce a confusing mixed-auth request.

Conclusion

A Claude Code proxy is two environment variables plus a model map. Set ANTHROPIC_BASE_URL to the gateway root without a /v1 suffix, set ANTHROPIC_AUTH_TOKEN to a bearer-token gateway key, and remap the Opus, Sonnet, and Haiku tiers to model IDs the gateway actually serves. Verify with /status and a hello message, and watch the OurToken dashboard for the resulting call record.

The payoff is a per-token rate that, on the Claude models listed above, is 40% of the official Anthropic price — concentrated on output tokens, which coding agents consume fastest. If you already hold an OurToken key, the entire change is a single settings.json block and a restart.

FAQ

What is the Claude Code base URL for a proxy?

For OurToken, set ANTHROPIC_BASE_URL to https://api.ourtoken.ai. Do not append /v1 or /v1/messages; Claude Code adds the Messages API path itself.

Which environment variable holds the Claude Code auth token?

Use ANTHROPIC_AUTH_TOKEN for a gateway that expects Authorization: Bearer, which includes OurToken. Use ANTHROPIC_API_KEY for a gateway that expects an x-api-key header instead.

Why do I get a /v1/v1/messages error?

Because ANTHROPIC_BASE_URL already ends in /v1. Claude Code appends its own /v1/messages, so a base URL of https://api.ourtoken.ai/v1 produces the doubled path. Set the base URL to the root without /v1.

Why does Claude Code show a login prompt after I set the proxy?

The env block did not load. Either settings.json is malformed, or Claude Code was not restarted after the edit — environment variables are read once at startup.

Which Claude model does my proxy use by default?

With the OurToken block above, ANTHROPIC_MODEL defaults to claude-sonnet-4-6, and the Opus and Haiku tiers resolve to claude-opus-4-8 and claude-opus-4-8 respectively.

Is a Claude Code proxy cheaper than the official API?

For the Claude models listed on OurToken, the proxy rate is 40% of the official Anthropic reference across input, output, cached input, and cache writes. The comparison against the subscription tier depends on your token volume, since the proxy bills pay-per-use with no per-seat cap.