Claude Code Router Setup: Use a Custom API Gateway Safely
Configure Claude Code with a custom Anthropic-compatible gateway, verify the route, set a hard key budget, and understand when claude-code-router is needed.
Claude Code can connect to a custom API gateway by setting ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN. If the gateway supports Anthropic's Messages API, no local translation proxy is required. RouterPlex supports that route for every chat model, so the setup is one settings file and a dedicated API key.
This guide covers the direct route. The full reference is also available in the RouterPlex Claude Code documentation.
1. Install Claude Code #
npm install -g @anthropic-ai/claude-code
Confirm that the command is available:
claude --version
2. Create a dedicated, budgeted key #
Create an API key specifically for Claude Code. Do not reuse a production application key. A coding agent can issue many requests, run tools, and continue working longer than an interactive chat.
For the first session, set a hard key budget equal to the maximum amount you are comfortable losing if the configuration or agent loop behaves badly. A $5 or $10 cap is enough to validate the route.
RouterPlex key budgets are configured in the API key dashboard. The cap is enforced at the gateway, so deleting a local environment variable is not the only line of defense.
3. Configure the direct Anthropic-compatible route #
Create or edit the Claude Code settings file:
- macOS and Linux:
~/.claude/settings.json - Windows:
%USERPROFILE%\.claude\settings.json
{"env": {"ANTHROPIC_BASE_URL": "https://api.routerplex.com","ANTHROPIC_AUTH_TOKEN": "sk-...","ANTHROPIC_MODEL": "claude-opus-4-8","ANTHROPIC_SMALL_FAST_MODEL": "claude-haiku-4-5"}}
ANTHROPIC_MODEL controls normal turns. ANTHROPIC_SMALL_FAST_MODEL gives Claude Code a lower-cost model for background work. Both accept any chat ID from the model catalog, because RouterPlex translates the Messages format across providers. Model capabilities and names still vary over time.
You can also set the route for one shell session instead of editing the file:
export ANTHROPIC_BASE_URL=https://api.routerplex.comexport ANTHROPIC_AUTH_TOKEN=sk-...export ANTHROPIC_MODEL=claude-opus-4-8claude
PowerShell uses this form:
$env:ANTHROPIC_BASE_URL = "https://api.routerplex.com"$env:ANTHROPIC_AUTH_TOKEN = "sk-..."$env:ANTHROPIC_MODEL = "claude-opus-4-8"claude
4. Verify before giving the agent a real repository #
Start Claude Code in an empty directory and run /status. Confirm the custom base URL and selected model. Then ask for a tiny response that does not require tools or repository access.
Check four things in the RouterPlex dashboard:
- One API call appears under the dedicated key.
- The expected Claude model is listed.
- Token usage and cost look plausible.
- The remaining key budget decreased by the same amount.
Only after this test should you open Claude Code in a real project.
Direct route versus claude-code-router #
Claude Code's native protocol is Anthropic Messages, not OpenAI Chat Completions. That creates two routing patterns.
| Pattern | Best for | Extra local process |
|---|---|---|
| Direct RouterPlex Messages endpoint | Any RouterPlex chat model | No |
| claude-code-router | Custom routing rules or local transformations | Yes |
For Claude, GPT, DeepSeek, Gemini, and other RouterPlex chat models, use the direct route. It has fewer moving parts and preserves Anthropic request and response shapes at the client boundary.
The community claude-code-router project remains useful when you need custom routing or local transformations. In that setup, RouterPlex can still be the upstream provider:
{"Providers": [{"name": "routerplex","api_base_url": "https://api.routerplex.com/v1/chat/completions","api_key": "sk-...","models": ["gpt-5.5", "deepseek-v4-pro"]}]}
The extra router adds another process, configuration file, and failure point. Use it for its routing logic, not just to reach a non-Claude model.
Cost controls for coding agents #
- Give Claude Code its own key and hard budget.
- Set a cheaper small/fast model for background tasks.
- Avoid running several agent sessions on one key during the first test.
- Review spend by key after each long task.
- Rotate the key if it appears in shell history, logs, or a committed file.
The fastest route to a working setup is also the safest: start with $5, cap the Claude Code key, verify one request, and expand the budget only after the usage data is real.
Frequently asked questions
Can Claude Code use a custom API base URL?
Yes. Set ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN in Claude Code settings or in the shell environment. The gateway must support the Anthropic Messages API shape.
Do I need the claude-code-router project for RouterPlex?
No. RouterPlex exposes Anthropic-compatible Messages for every chat model, including GPT, Gemini, and DeepSeek. Use a community router only when you need additional local routing rules or transformations.
Which budget should I use for Claude Code?
Start with a dedicated key capped at the amount you are willing to spend during the test, such as $5 or $10. Increase it only after reviewing real usage.
Run the smallest paid test.
Add $5, cap the key, and verify the result with your own workload.