AnRouter integration guide
From your first request to Codex, Claude Code, OpenCode, OpenClaw, and Hermes Agent. Every example distinguishes a Base URL from a full endpoint to prevent duplicate paths.
5-minute quickstart
Create an API key in the AnRouter dashboard, then copy a model ID from the models page. The examples below use the currently listed glm-5.3.
Python requires the official openai package. The Node.js example requires the openai package and must be saved as an ES module, for example example.mjs. Set ANROUTER_API_KEY in the process environment before running either example.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["ANROUTER_API_KEY"],
base_url="https://api.anrouter.com/v1",
)
response = client.chat.completions.create(
model="glm-5.3",
messages=[{"role": "user", "content": "Introduce yourself in three sentences."}],
)
print(response.choices[0].message.content)
// Save as example.mjs, then run: node example.mjs
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.ANROUTER_API_KEY,
baseURL: "https://api.anrouter.com/v1",
});
const response = await client.chat.completions.create({
model: "glm-5.3",
messages: [{ role: "user", content: "Introduce yourself in three sentences." }],
});
console.log(response.choices[0].message.content);
curl https://api.anrouter.com/v1/chat/completions \
-H "Authorization: Bearer $ANROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "glm-5.3",
"messages": [{"role": "user", "content": "Introduce yourself."}]
}'
Codex CLI / desktop app
Codex CLI, IDE extensions, and Codex in the ChatGPT desktop app share the user-level ~/.codex/config.toml. Configure AnRouter as a separate provider. Do not edit auth.json to impersonate the built-in OpenAI login.
1. Install and verify
# macOS / Linux
curl -fsSL https://chatgpt.com/codex/install.sh | sh
# Check
codex --version
Windows PowerShell: irm https://chatgpt.com/codex/install.ps1 | iex
2. Verify the Responses route first
curl https://api.anrouter.com/v1/responses \
-H "Authorization: Bearer $ANROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"glm-5.3","input":"Reply with only: ok"}'
3. Add the user-level configuration
# ~/.codex/config.toml
model = "glm-5.3"
model_provider = "anrouter"
[model_providers.anrouter]
name = "AnRouter"
base_url = "https://api.anrouter.com/v1"
env_key = "ANROUTER_API_KEY"
wire_api = "responses"
~/.codex/ in Finder and edit config.toml. On Windows, use %USERPROFILE%\.codex\config.toml. Open the image at full size for detail.responses is the only wire API supported for a Codex custom provider and is also the default when omitted. The selected AnRouter model and channel must work through POST /v1/responses.4. Supply the key and launch
export ANROUTER_API_KEY="your_anrouter_api_key"
codex --model glm-5.3
# Open the desktop app
codex app
The desktop app or IDE extension may not inherit shell variables. Put ANROUTER_API_KEY=… in ~/.codex/.env, fully restart the app, and start a new session.
auth.json and the built-in openai provider belong to the OpenAI sign-in flow. A separate AnRouter provider is clearer and does not overwrite an existing OpenAI login.Reference: OpenAI Codex custom model providers
Claude Code / VS Code
Claude Code sends requests to an Anthropic Messages endpoint, so its Base URL must omit /v1. AnRouter accepts that protocol, but Anthropic does not promise full Claude Code compatibility for non-Claude models. Validate tool use and editing workflows before using one on a real project.
1. Install
# macOS / Linux / WSL
curl -fsSL https://claude.ai/install.sh | bash
claude --version
claude doctor
Windows PowerShell: irm https://claude.ai/install.ps1 | iex
2. Run a temporary test
export ANTHROPIC_BASE_URL="https://api.anrouter.com"
export ANTHROPIC_AUTH_TOKEN="your_anrouter_api_key"
export ANTHROPIC_MODEL="glm-5.3"
claude
3. Persist the Claude Code settings
{
"$schema": "https://json.schemastore.org/claude-code-settings.json",
"env": {
"ANTHROPIC_BASE_URL": "https://api.anrouter.com",
"ANTHROPIC_AUTH_TOKEN": "your_anrouter_api_key",
"ANTHROPIC_MODEL": "glm-5.3"
}
}
claude or anthropic. AnRouter IDs such as glm-5.3 do not appear automatically, so specify ANTHROPIC_MODEL explicitly.POST /v1/messages but not Claude Code's optional POST /v1/messages/count_tokens route. Ordinary Messages requests can work, but a client feature that specifically calls server-side token counting may fail. Do not treat one successful chat as proof of full Claude Code compatibility.4. VS Code extension
- Install the official Claude Code extension and first confirm that
claudecan complete a request through AnRouter in a terminal. - The extension and CLI share
~/.claude/settings.jsonby default, so prefer the shared configuration above. - If the extension does not read the shared variables or still prompts for sign-in, press ⌘/Ctrl + Shift + P and run Preferences: Open User Settings (JSON).
- After changing variables, run Developer: Reload Window and send a short test request.
Only when extension-specific settings are needed, merge the following fields into the existing VS Code user settings without deleting other entries:
{
"claudeCode.disableLoginPrompt": true,
"claudeCode.environmentVariables": [
{ "name": "ANTHROPIC_BASE_URL", "value": "https://api.anrouter.com" },
{ "name": "ANTHROPIC_AUTH_TOKEN", "value": "your_anrouter_api_key" },
{ "name": "ANTHROPIC_MODEL", "value": "glm-5.3" }
]
}
5. Claude Desktop: the Code tab
Local Code sessions in Claude Desktop can reuse Claude Code settings. If the desktop app does not inherit shell variables, add them through the Local environment settings. This path still uses ANTHROPIC_BASE_URL=https://api.anrouter.com without /v1.
6. Claude Desktop: third-party inference
- In Claude Desktop, open Help → Troubleshooting → Enable Developer Mode.
- Open Developer → Configure Third-Party Inference… and choose Gateway.
- Set Gateway base URL to
https://api.anrouter.com, enter the AnRouter API key, choose bearer authentication, and select Static API key. - Test the connection, enter an exact AnRouter model ID in Models, then choose Apply locally.
https://api.anrouter.com without /v1; Claude Desktop uses /v1/messages when it sends the request. Open the image at full size for detail.https://api.anrouter.com. Do not enter the complete /v1/messages request URL either. OpenAI-compatible clients such as Hermes and OpenCode use https://api.anrouter.com/v1. A read-only window indicates that the organization has locked these values through managed configuration; contact the administrator.References: Claude Code LLM gateway · VS Code integration · Claude Desktop third-party inference
OpenCode
OpenCode can connect to Chat Completions through @ai-sdk/openai-compatible. Its TUI, desktop app, and IDE interfaces can reuse the same provider configuration.
1. Desktop app: add a custom provider
- Open the provider settings in OpenCode Desktop and add a custom provider.
- Set Provider ID to
anrouterand the display name to AnRouter. - Set Base URL to
https://api.anrouter.com/v1and enter your AnRouter API key.
- Add a model under Models. Its ID must exactly match the AnRouter models page or
GET /v1/models, for exampleglm-5.3. - Leave request Headers empty unless a documented integration requires them, submit, then choose
anrouter/glm-5.3from the model picker.
2. TUI / CLI: store credentials
- Start OpenCode and enter
/connectin the TUI. - Choose Other and enter
anrouteras the provider ID. - Enter your AnRouter API key. This stores credentials only; the provider configuration below is still required.
3. Configure the provider manually
"attachment": true and "modalities": { "input": ["text", "image"], "output": ["text"] } on every AnRouter model you want to use with images. If these fields are omitted, OpenCode may display and resize an image but remove it before the API request, causing the model to say it cannot view images.{
"$schema": "https://opencode.ai/config.json",
"model": "anrouter/glm-5.3",
"provider": {
"anrouter": {
"npm": "@ai-sdk/openai-compatible",
"name": "AnRouter",
"options": {
"baseURL": "https://api.anrouter.com/v1"
},
"models": {
"glm-5.3": {
"name": "GLM 5.3",
"attachment": true,
"modalities": { "input": ["text", "image"], "output": ["text"] }
},
"glm-5.3-flash": {
"name": "GLM 5.3 Flash",
"attachment": true,
"modalities": { "input": ["text", "image"], "output": ["text"] }
},
"deepseek-v4-flash-0731": {
"name": "DeepSeek V4 Flash 0731",
"attachment": true,
"modalities": { "input": ["text", "image"], "output": ["text"] }
},
"kimi-k2.6": {
"name": "Kimi K2.6",
"attachment": true,
"modalities": { "input": ["text", "image"], "output": ["text"] }
},
"minimax-m3": {
"name": "MiniMax M3",
"attachment": true,
"modalities": { "input": ["text", "image"], "output": ["text"] }
},
"qwen-3.8-27b": {
"name": "Qwen3.8 27B",
"attachment": true,
"modalities": { "input": ["text", "image"], "output": ["text"] }
}
}
}
}
}
Put project configuration in opencode.json at the repository root, or personal global configuration in ~/.config/opencode/opencode.json. The example intentionally omits hard-coded context and output limits so client metadata cannot drift from the service's actual capabilities.
4. Verify
opencode auth list
opencode
# In the TUI
/models
@ai-sdk/openai-compatible maps to /v1/chat/completions. Use @ai-sdk/openai only for a model/provider that is explicitly intended to use Responses.Reference: OpenCode custom providers
Keys and authentication
OpenAI-compatible requests use a Bearer token. The AnRouter Anthropic Messages route accepts both the Bearer authentication used by Claude Code and the x-api-key header commonly used by Anthropic SDKs.
Authorization: Bearer YOUR_ANROUTER_API_KEYAuthorization: Bearer YOUR_ANROUTER_API_KEYx-api-key: YOUR_ANROUTER_API_KEYEndpoints and Base URLs
A Base URL is the prefix to which a client appends a route; it is not always the full request URL. Different clients append different paths, so one URL cannot be copied mechanically into every app.
| Use case | Enter this | Final request |
|---|---|---|
| OpenAI SDK / OpenCode / OpenClaw / Hermes | https://api.anrouter.com/v1 | /v1/chat/completions |
| Codex custom provider | https://api.anrouter.com/v1 | /v1/responses |
| Claude Code / Claude Desktop Gateway / Anthropic SDK | https://api.anrouter.com | /v1/messages |
| Raw HTTP | Use the complete URL | https://api.anrouter.com/v1/… |
| Native Gemini REST | https://api.anrouter.com | /v1beta/models/{model}:generateContent |
/v1/chat/completions endpoint into an SDK Base URL, or giving Claude Code https://api.anrouter.com/v1, can produce an invalid /v1/v1/… request.Primary AnRouter API routes
POST /v1/responses
POST /v1/responses/compact
POST /v1/messages
GET /v1/models/{model}
POST /v1/embeddings
POST /v1/rerank
POST /v1/images/edits
POST /v1/audio/transcriptions
POST /v1/audio/speech
POST /v1beta/models/{model}:streamGenerateContent
A route existing does not mean every model supports that capability. The model, upstream channel, and request fields must all be compatible.
Protocols and request examples
OpenAI Chat Completions
curl https://api.anrouter.com/v1/chat/completions \
-H "Authorization: Bearer $ANROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"glm-5.3","messages":[{"role":"user","content":"Hello"}]}'
OpenAI Responses
curl https://api.anrouter.com/v1/responses \
-H "Authorization: Bearer $ANROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"glm-5.3","input":"Review the edge cases in this function."}'
Anthropic Messages
curl https://api.anrouter.com/v1/messages \
-H "x-api-key: $ANROUTER_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model":"glm-5.3",
"max_tokens":1024,
"messages":[{"role":"user","content":"Hello"}]
}'
List current models
curl https://api.anrouter.com/v1/models \
-H "Authorization: Bearer $ANROUTER_API_KEY"
Troubleshooting
Check that the key is complete and has not been revoked. OpenAI-compatible requests require Authorization: Bearer; when Claude Code uses Bearer authentication, set ANTHROPIC_AUTH_TOKEN.
Inspect the final URL. Look for /v1/v1, a full endpoint entered as a Base URL, or a model ID that does not exactly match GET /v1/models.
This can indicate token, concurrency, or upstream capacity limits. Use exponential backoff with jitter and a bounded retry count.
Agents usually also depend on tool calling, streaming, and a particular protocol. Test Codex on /v1/responses and Claude Code on /v1/messages.
OpenClaw
An OpenClaw model provider key and a Gateway login token are different credentials. Use the AnRouter API key only for the model provider; do not put it in gateway.auth.token.
1. Install and check
curl -fsSL https://openclaw.ai/install.sh | bash
openclaw --version
openclaw doctor
2. Put the key in the global runtime environment
# ~/.openclaw/.env
ANROUTER_API_KEY=your_anrouter_api_key
3. Add the provider without replacing existing configuration
Run the additive command below on the machine that actually runs the Gateway. OpenClaw protects provider maps from destructive replacement; --merge preserves existing channels, tools, skills, providers, and Gateway settings.
| Gateway location | Configuration to edit |
|---|---|
| Local macOS / Linux | ~/.openclaw/openclaw.json |
| Native Windows | %USERPROFILE%\.openclaw\openclaw.json |
| Windows Hub / WSL | Edit ~/.openclaw/openclaw.json inside the WSL environment that actually runs the Gateway |
| Remote Gateway | Sign in to the remote host and edit the remote user's configuration |
openclaw config set models.providers.anrouter \
'{"baseUrl":"https://api.anrouter.com/v1","apiKey":"${ANROUTER_API_KEY}","api":"openai-completions","models":[{"id":"glm-5.3","name":"GLM 5.3"},{"id":"glm-5.3-flash","name":"GLM 5.3 Flash"},{"id":"deepseek-v4-flash-0731","name":"DeepSeek V4 Flash 0731"},{"id":"kimi-k2.6","name":"Kimi K2.6"},{"id":"minimax-m3","name":"MiniMax M3"},{"id":"qwen-3.8-27b","name":"Qwen3.8 27B"}]}' \
--strict-json --merge
4. Validate and restart the Gateway
openclaw config validate
openclaw models list
openclaw models set anrouter/glm-5.3
openclaw gateway restart
openclaw gateway status
openai-completions is the Chat Completions adapter. OpenClaw is a tool-intensive agent, so validate tool calling, streaming, and long-context behavior—not only ordinary chat—before production.References: OpenClaw custom providers · safe config set edits
Hermes Agent
The recommended setup uses hermes model to add a custom endpoint. Current Hermes documentation uses the top-level custom_providers list for named custom endpoints; the manual example below follows that documented schema.
1. Hermes Desktop visual setup
- Open model/provider setup during first launch, or reopen it later from the model settings.
- Choose Local / custom endpoint, set Endpoint to
https://api.anrouter.com/v1, and enter the AnRouter API key. - Use an exact model ID such as
glm-5.3. If API mode appears, choosechat_completions. - Save, start a new session, and send a short test message.
https://api.anrouter.com/v1 in Endpoint. Enter your own AnRouter key on the next line and never publish a screenshot containing it. Open the image at full size for detail.2. Install the CLI
# macOS / Linux / WSL2
curl -fsSL https://hermes-agent.nousresearch.com/install.sh | bash
hermes --version
hermes doctor
Windows PowerShell: iex (irm https://hermes-agent.nousresearch.com/install.ps1)
3. CLI recommended: interactive setup
hermes model
| Provider | Custom endpoint |
| API Base URL | https://api.anrouter.com/v1 |
| API Key | AnRouter API key |
| Model | glm-5.3 |
| API Mode | chat_completions |
| Context length | Leave blank for endpoint discovery when possible; otherwise use the model catalog |
4. Optional: named provider configuration
# ~/.hermes/config.yaml
custom_providers:
- name: anrouter
base_url: https://api.anrouter.com/v1
key_env: ANROUTER_API_KEY
api_mode: chat_completions
model:
provider: custom:anrouter
default: glm-5.3
# ~/.hermes/.env
ANROUTER_API_KEY=your_anrouter_api_key
5. Verify
- Run
hermes doctorto check the installation and configuration. - Start
hermesand send a short prompt first. - Use
/model custom:anrouter:glm-5.3in a session. Exit and runhermes modelto add or change a provider.
context_length manually when it matches the current AnRouter model catalog and actual endpoint behavior.References: Hermes Desktop · Hermes custom endpoints
Other compatible clients
When a client explicitly supports an “OpenAI-compatible” or “Custom OpenAI endpoint,” the four values below will usually connect it. A similar product name does not prove that a custom Base URL is supported; do not force undocumented environment-variable overrides into software with no public configuration path.
| Provider type | OpenAI compatible |
| Base URL | https://api.anrouter.com/v1 |
| API key | AnRouter API key |
| Model | Copy from the models page or GET /v1/models |
- Confirm whether the final client request uses
/v1/chat/completionsor/v1/responses. - Start with a minimal text request, then test streaming, tools, vision, JSON, and other required capabilities.
- If a client accepts only an official vendor key and exposes no Base URL field, do not claim that it natively supports AnRouter.
Security and production checklist
Create separate keys for people, CI, and production services. Do not share one long-lived key across a team. Use environment variables, a system secret manager, or the client's credential store.
Validate the exact model ID, protocol, streaming, tool calls, timeouts, and maximum inputs with realistic requests. Do not put an agent into production merely because ordinary chat succeeds.
Retry only recoverable failures, with exponential backoff, jitter, and a maximum attempt count. Log request IDs—not complete keys or sensitive prompts.
Clients and models change. After a client upgrade, protocol change, or model switch, rerun the minimal request and agent capability checks in a test environment.