Pointing Claude Code at the Aggregator Through Env Vars Only: ANTHROPIC_BASE_URL, Auth Token, and the Order They Take Effect
This guide explains how to redirect Claude Code to an aggregator API using only shell environment variables. It covers the precedence of ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN (or ANTHROPIC_API_KEY), how to verify which endpoint a session used, and practical debugging tips.
How Claude Code Reads Configuration
Claude Code, like many CLI tools, reads configuration from environment variables. For API access, the key variables are:
ANTHROPICBASEURL– overrides the default API endpoint.ANTHROPICAPIKEY– your API key for Anthropic.ANTHROPICAUTHTOKEN– an alternative authentication token, often used for custom endpoints or proxies.
When both an Anthropic API key and an aggregator key are present, Claude Code follows a specific precedence. This article explains that order and how to confirm which endpoint your session actually talked to.
Variable Precedence: Which One Wins?
Claude Code checks environment variables in a defined order. Understanding this prevents misconfiguration.
ANTHROPICBASEURLtakes effect first. If set, it overrides the default Anthropic API endpoint (usuallyhttps://api.anthropic.com). All requests go to this base URL.- For authentication,
ANTHROPICAUTHTOKENis typically prioritized overANTHROPICAPIKEYwhen both are set. This allows you to use a token intended for the aggregator while keeping an Anthropic key for other tools. - If only
ANTHROPICAPIKEYis set, it is used as the authentication credential. - If neither authentication variable is set, Claude Code may prompt for login or fail.
Important: The exact precedence can vary by version. Always verify with the method below.
Setting the Variables for the Aggregator
To route Claude Code to the aggregator, set both the base URL and the auth token. Use your aggregator API key as the auth token.
export ANTHROPIC_BASE_URL="https://api.aggregator.example.com"
export ANTHROPIC_AUTH_TOKEN="your-aggregator-api-key"
If you also have an Anthropic API key in your environment, the aggregator token will be used because ANTHROPICAUTHTOKEN takes precedence. To be safe, you can unset the other variable:
unset ANTHROPIC_API_KEY
Verifying Which Endpoint a Session Used
After starting Claude Code, confirm that requests are going to the aggregator, not Anthropic.
Method 1: Check Environment in the Session
If Claude Code provides a shell escape or debug mode, you can inspect the active environment. For example, in some versions you can run a command within the tool:
env | grep ANTHROPIC
This shows the values Claude Code sees.
Method 2: Use Verbose Logging
Enable verbose output to see the API endpoint being called. This often requires setting a debug variable:
export ANTHROPIC_LOG=debug
Then run Claude Code and look for log lines containing the base URL.
Method 3: Network Inspection
Use a tool like tcpdump or a proxy to observe outgoing requests. For example:
sudo tcpdump -i any -A -s 0 'tcp port 443' | grep -i 'Host:'
This displays the hostnames of HTTPS connections. You should see your aggregator's domain, not api.anthropic.com.
Method 4: Aggregator Dashboard
Most aggregators provide a dashboard showing request logs. After a session, check if requests appear there. If they do, you are correctly routed.
Common Pitfalls
- Variable name typos – Ensure
ANTHROPICBASEURLis spelled correctly and includes the protocol (https://). - Trailing slashes – Some endpoints require no trailing slash; check aggregator documentation.
- Proxy interference – If you use a proxy, ensure it does not rewrite the Host header.
- Multiple keys – If both
ANTHROPICAPIKEYandANTHROPICAUTHTOKENare set, confirm which one your Claude Code version uses. When in doubt, unset the one you do not need.
Why This Matters for Cost and Access
Using the aggregator with a single API key lets you access multiple models (Claude, GPT, DeepSeek, Qwen, GLM, Kimi) without managing separate accounts. You pay the official price multiplied by 1.3, and key contributors are credited back at 1.1× (or 1.2× for premium models) in USDC. Topping up with USDC on Base requires no KYC.
Verifying the Auth Token Source
If you suspect the wrong token is being used, you can test by temporarily setting only one variable:
unset ANTHROPIC_API_KEY
export ANTHROPIC_AUTH_TOKEN="your-aggregator-api-key"
claude --version # or any claude command that makes an API call
Then check the aggregator logs. If requests appear, the token is correct. If not, try the other variable.
Conclusion
Redirecting Claude Code to an aggregator is straightforward with environment variables, but understanding precedence and verifying the active endpoint is essential. Always confirm with logs or network inspection, and keep your environment clean to avoid conflicts. With the right configuration, you can seamlessly use multiple models through a single API key.