Point the official Claude Code CLI at this gateway using two environment variables to access Claude models.
POST /v1/messages
Claude Code is Anthropic's official command-line AI assistant, defaults to `api.anthropic.com`. By pointing it to **our gateway** (`api.router.ai`), you immediately gain access to all Claude models on the platform, unified billing, and multi-model routing capabilities, with only 0 code changes. ## One: two core environment variables The gateway configuration for Claude Code requires only these two variables: | Variable | Value | Purpose | |---|---|---| | `ANTHROPIC_BASE_URL` | `https://api.router.ai` | Gateway root address. **Fill in the root domain only, do not add `/v1` or `/v1/messages`** | | `ANTHROPIC_AUTH_TOKEN` | `sk-你的 token` | Platform proxy token, sent as `Authorization: Bearer xxx` | > ⚠️ **Do not use `ANTHROPIC_API_KEY`** — that would send an `x-api-key` header, which our gateway only recognizes as standard `Authorization: Bearer`. > ⚠️ **Do not add `/v1`** — the Claude Code HTTP client automatically appends `/v1/messages` to BASE_URL. Adding `/v1` results in a request formed as `/v1/v1/messages` matching 404 (as per Anthropic's official LLM Gateway documentation). --- ## Two: three configuration methods (choose any one) ### Method 1 · Temporary environment variables (recommended for testing, fastest) ```bash export ANTHROPIC_BASE_URL=https://api.router.ai export ANTHROPIC_AUTH_TOKEN=sk-你的token claude ``` Effective only for the current terminal session, lost when closed, suitable for **first verifying connectivity**. --- ### Method 2 · Write into shell configuration (permanent, most commonly used) **macOS** (default zsh): ```bash cat >> ~/.zshrc <<'EOF' export ANTHROPIC_BASE_URL=https://api.router.ai export ANTHROPIC_AUTH_TOKEN=sk-你的token EOF source ~/.zshrc ``` **Linux** (bash): ```bash cat >> ~/.bashrc <<'EOF' export ANTHROPIC_BASE_URL=https://api.router.ai export ANTHROPIC_AUTH_TOKEN=sk-你的token EOF source ~/.bashrc ``` **Windows PowerShell**: ```powershell $env:ANTHROPIC_BASE_URL = "https://api.router.ai" $env:ANTHROPIC_AUTH_TOKEN = "sk-你的token" ``` To take effect permanently, add to `$PROFILE` or in the system environment variables. --- ### Method 3 · Write into Claude Code configuration file (recommended, does not pollute global settings) Edit `~/.claude/settings.json` (create if it doesn't exist): ```json { "env": { "ANTHROPIC_BASE_URL": "https://api.router.ai", "ANTHROPIC_AUTH_TOKEN": "sk-你的token" } } ``` This method **only applies to Claude Code**, and does not affect your other tools' ANTHROPIC_* variables. Recommended for long-term use. --- ## Three: model name mapping (as needed) Our gateway defaults to pass-through the Claude Code-sent model name (e.g. `claude-sonnet-4-5-20250929` / `claude-opus-4-7`). If you want to **force override** the default model (e.g. set all to Haiku for cost savings), add these three variables: ```bash export ANTHROPIC_DEFAULT_OPUS_MODEL=claude-opus-4-7 export ANTHROPIC_DEFAULT_SONNET_MODEL=claude-sonnet-4-5-20250929 export ANTHROPIC_DEFAULT_HAIKU_MODEL=claude-haiku-4-5-20251001 ``` See the complete list of supported Claude models in **Model Plaza → Claude Series**. Incorrect model names will return upstream errors to 400, and the client can see the original error. --- ## Four: Verification of Effectiveness After startup, input the `/status` command and you should see: ``` Base URL: https://api.router.ai Auth: Bearer sk-...**** Model: claude-... ``` If the Base URL is still `api.anthropic.com`, it means the environment variables did not take effect. Check via: 1. Reopen the terminal (shell configuration changes require a source command) 2. `echo $ANTHROPIC_BASE_URL` should match the root domain (**without /v1**) 3. Check the 3 method's settings.json for correct JSON syntax --- ## Five: Quick Reference for Common Pitfalls | Symptom | Cause | Solution | |---|---|---| | 404 / `Not Found` contains path `/v1/v1/messages` | `BASE_URL` added an extra `/v1` suffix → SDK appends `/v1/messages` again, forming double `/v1` | Remove `/v1`, keeping only the root domain | | Still routing to official / 401 / `please log in` | `~/.claude/.credentials.json` retains OAuth token, which has higher priority than environment variables | Delete the file: `rm ~/.claude/.credentials.json` | | `model not found` class error | Client-sent model name is not in our supported list | Use the above `ANTHROPIC_DEFAULT_*_MODEL` to force map, or check the Model Plaza | | `401 Unauthorized` | Token configured incorrectly or expired | Go to cloud admin backend → Token Management, and regenerate | | `429 / 限流` | Token RPM/TPM exceeds limit | Adjust token rate-limiting configuration in the backend or switch to a higher-tier token | | Streaming response does not work | Intermediate proxy (Cloudflare self-built / company firewall) buffer SSE | Directly connect to `api.router.ai` without going through intermediate proxy | | All requests to 422 | curl testing with prompt containing bare newline (Claude Code SDK will not encounter this) | Use SDK instead of manually writing curl | --- ## Six. Complete one-click script (copy-paste version) ```bash # 1. 配置 (一次性) mkdir -p ~/.claude cat > ~/.claude/settings.json <<'EOF' { "env": { "ANTHROPIC_BASE_URL": "https://api.router.ai", "ANTHROPIC_AUTH_TOKEN": "sk-在这里贴你的token" } } EOF # 2. 清理可能残留的 OAuth (跳到 Step 3 如果是新机器) rm -f ~/.claude/.credentials.json # 3. 启动 + 验证 claude # 进入 Claude Code 后: # /status — 应看到 Base URL = https://api.router.ai # 你好 (随便一句中文测试) — 应正常返回 ``` --- ## Seven. Coexistence with official accounts Hope to **retain official account but temporarily switch to our version**? Use temporary variable 1 to achieve this, automatically returning to official account after terminal exit. Alternatively, maintain two settings files: ```bash # 切到我方 ln -sf ~/.claude/settings.router.json ~/.claude/settings.json # 切回官方 ln -sf ~/.claude/settings.official.json ~/.claude/settings.json ``` --- ## Eight. Observability The platform backend records each Claude Code call as follows: - Request + response token count, duration, model matched - usage / cache_read_tokens / cache_creation_tokens (Claude prompt cache transparently follows) - Streamed chunk count, TTFB, full response content (required for compliance and audit) Proxy portal can view all historical records in the "**Billing Flow**" or "**Chat Invocations**" section. --- ## Nine. Technical Details (only for on-demand review) When Claude Code starts, it will request `${ANTHROPIC_BASE_URL}/v1/models` to probe available models. During calls, it requests `${ANTHROPIC_BASE_URL}/v1/messages` (streaming / non-streaming) and possibly `${ANTHROPIC_BASE_URL}/v1/messages/count_tokens` (token estimation). Our gateway has implemented all three endpoints and is compatible with Anthropic's official protocol.
200 — Success