Point Claude Code at a Third-Party Endpoint: ANTHROPIC_BASE_URL and Auth Token Explained
Claude Code reads ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN to route requests to a custom endpoint. This guide explains each environment variable, shows how to set and verify them, and covers conflicts with other config sources like settings.json, ANTHROPIC_API_KEY, and project-level .env files. It also describes where an LLM API aggregator fits: one API key, many models, USDC top-up on Base, and pay-as-you-go pricing.
Why point Claude Code at a custom endpoint
Claude Code normally talks to Anthropic's API directly. If you run an API gateway, a proxy, or an LLM API aggregator, you can redirect it by setting a base URL and an auth token in your environment. That lets you:
- Use one API key across multiple model providers.
- Add logging, caching, or rate limiting without patching the client.
- Keep tool calls and streaming working the same way, because the endpoint speaks the Anthropic API protocol.
Claude Code does not need a special build for this. It reads environment variables at start-up.
The two environment variables that matter
ANTHROPICBASEURL
This overrides the base URL for all Anthropic API requests. Set it to the root of your gateway, without a trailing slash.
Example:
export ANTHROPIC_BASE_URL="https://api.your-gateway.example"
The client will call paths such as /v1/messages relative to that base.
ANTHROPICAUTHTOKEN
This sets the bearer token used for authentication. Use it when your endpoint expects an Authorization: Bearer <token> header rather than the x-api-key header that Anthropic uses natively.
export ANTHROPIC_AUTH_TOKEN="your-gateway-token"
If your gateway accepts the native x-api-key header, you can set ANTHROPICAPIKEY instead. Many aggregators accept both.
Which variables conflict
Claude Code resolves configuration from multiple sources. When they disagree, the precedence is not always obvious, so avoid setting conflicting values.
Common conflicts:
- ANTHROPICAPIKEY vs ANTHROPICAUTHTOKEN — if both are set, the client may choose one and ignore the other. Use the one your endpoint expects. If your gateway accepts both headers, either works, but setting both is redundant and can cause confusion.
- Environment variables vs settings.json — Claude Code stores some settings in a user-level or project-level
settings.json. If that file defines a base URL or token, it can override your shell exports. Check both.
- Shell profile vs current session — exporting a variable in
.bashrcor.zshrcaffects new shells, not the one you are already in. Verify withechobefore launching Claude Code.
- Project-level .env files — some setups load a
.envfile from the project directory. That can silently override your shell environment. Look for one and inspect its contents.
- Proxy and TLS variables —
HTTPPROXY,HTTPSPROXY, andNO_PROXYaffect how the client reaches the base URL. If a proxy is set, it can intercept or block requests even when the base URL is correct.
When debugging, unset the variables you are not using:
unset ANTHROPIC_API_KEY
unset HTTPS_PROXY
Then set only the ones your endpoint needs.
How to verify the change actually took effect
After setting the variables, confirm the client is using them.
- Check the environment — run
env | grep ANTHROPICin the same shell where you will start Claude Code. You should see exactly the variables you set, with the expected values.
- Look for a start-up banner or log line — many clients print the effective base URL or a redacted token at start-up. If Claude Code does, read it. If it does not, increase verbosity with a debug flag if one exists.
- Make a minimal request — ask Claude Code a trivial question that triggers one API call. If the response comes back, the base URL and token are at least valid enough to authenticate. If you get an authentication error, the token is wrong or the header name is wrong. If you get a connection error, the base URL or proxy is wrong.
- Inspect network traffic — run the client with a local proxy or
mitmproxyand look at the request host andAuthorizationheader. This is the most direct confirmation. Avoid logging the token itself.
- Check your gateway logs — if you control the endpoint, its access logs will show the incoming request. That proves the request reached your gateway rather than Anthropic.
A practical workflow
- Pick the endpoint and token you want to use.
- In a fresh terminal, export
ANTHROPICBASEURLand the token variable that matches your endpoint's auth scheme. - Unset any conflicting variables.
- Run
env | grep ANTHROPICand confirm. - Start Claude Code and make one small request.
- If it fails, check the error type: auth, DNS, TLS, or proxy.
- Once it works, persist the exports in your shell profile or a project-specific script so you do not repeat the steps.
Using an LLM API aggregator as the endpoint
An LLM API aggregator is a natural fit for this pattern. Instead of managing separate keys for each provider, you point ANTHROPICBASEURL at the aggregator and use one token. The aggregator translates your request to the appropriate upstream model.
Key points about this setup:
- One API key, many models — you can call Claude, GPT, DeepSeek, Qwen, GLM, Kimi, and others through the same endpoint by changing the model name in the request.
- USDC on Base, no KYC — you top up your balance with USDC on Base. No identity verification is required to add credit.
- Transparent pricing — you pay the official model price times 1.3. If you contribute keys, you are credited the official price times 1.1 (or times 1.2 for premium keys) in USDC.
- No lock-in — because the endpoint speaks the Anthropic API, you can switch back to Anthropic's base URL at any time by changing one environment variable.
This keeps Claude Code working as usual while giving you access to a wider set of models and a simpler billing path.
Common pitfalls
- Trailing slash —
https://api.example.com/andhttps://api.example.comcan behave differently in some clients. Use no trailing slash unless your gateway documents otherwise. - Wrong token type — a token meant for
x-api-keywill not work as a bearer token, and vice versa. Match the header your gateway expects. - Cached sessions — some clients cache DNS or TLS sessions. If you change the base URL, restart the client.
- Shell inheritance — GUI apps launched from a desktop environment may not inherit your shell exports. In that case, set the variables in the app's launch configuration or a system-wide location.
Summary
To point Claude Code at a third-party endpoint, set ANTHROPICBASEURL to the endpoint root and set either ANTHROPICAUTHTOKEN or ANTHROPICAPIKEY depending on the auth scheme. Avoid setting conflicting variables, check settings.json and .env files, and verify with env | grep ANTHROPIC and a single test request. An LLM API aggregator with one key, USDC on Base top-ups, and transparent per-model pricing is a practical way to use this pattern across many models.