Claude Code and Anthropic SDK
Use this guide for clients that speak the Anthropic Messages API: Claude Code, the Anthropic Python SDK, and the Anthropic TypeScript SDK.
Anthropic-native clients are different from OpenAI-compatible clients:
OpenAI SDK / Codex base URL: https://api.rout.my/v1
Claude Code / Anthropic SDK URL: https://api.rout.my
Anthropic request endpoint: https://api.rout.my/v1/messages
Anthropic token count endpoint: https://api.rout.my/v1/messages/count_tokensClaude Code and Anthropic SDK clients append /v1/messages themselves, so their configured base URL should be https://api.rout.my, not https://api.rout.my/v1.
1. Get a rout.my key
Set the key in the terminal you will use. Pick one:
# macOS/Linux/WSL/Git Bash
export ROUTMY_API_KEY="sk_your_key_here"# Windows PowerShell
$env:ROUTMY_API_KEY="sk_your_key_here":: Windows cmd.exe
set ROUTMY_API_KEY=sk_your_key_hereThen check the models list.
macOS/Linux/WSL/Git Bash:
curl https://api.rout.my/v1/models \
-H "Authorization: Bearer $ROUTMY_API_KEY"Windows PowerShell:
curl.exe https://api.rout.my/v1/models -H "Authorization: Bearer $env:ROUTMY_API_KEY"Windows cmd.exe:
curl.exe https://api.rout.my/v1/models -H "Authorization: Bearer %ROUTMY_API_KEY%"The ready-to-use Claude Code model in this guide is:
anthropic/claude-sonnet-4.6You do not need to choose a model manually. Use the default unless you intentionally want another option:
| Use case | Model |
|---|---|
| Default Claude Code setup | anthropic/claude-sonnet-4.6 |
| Stronger, more expensive Claude setup | anthropic/claude-opus-4.6 |
If you change the model, copy the exact ID from /v1/models.
Set the model ID in the same terminal:
# macOS/Linux/WSL/Git Bash
export ROUTMY_MODEL="anthropic/claude-sonnet-4.6"# Windows PowerShell
$env:ROUTMY_MODEL="anthropic/claude-sonnet-4.6":: Windows cmd.exe
set ROUTMY_MODEL=anthropic/claude-sonnet-4.62. Test the Anthropic route
Before opening Claude Code, test the raw Messages endpoint:
curl https://api.rout.my/v1/messages \
-H "Authorization: Bearer $ROUTMY_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-sonnet-4.6",
"max_tokens": 128,
"messages": [
{ "role": "user", "content": "Reply with OK." }
]
}'If this fails, fix the key, model ID, or account access first. Claude Code will fail the same way until this curl succeeds.
rout.my accepts both Bearer auth and Anthropic-style x-api-key auth on the Anthropic route. Claude Code sends either Authorization or x-api-key depending on which gateway credential variable you set.
On Windows PowerShell, use this equivalent check:
curl.exe https://api.rout.my/v1/messages `
-H "Authorization: Bearer $env:ROUTMY_API_KEY" `
-H "anthropic-version: 2023-06-01" `
-H "Content-Type: application/json" `
-d "{\"model\":\"$env:ROUTMY_MODEL\",\"max_tokens\":128,\"messages\":[{\"role\":\"user\",\"content\":\"Reply with OK.\"}]}"3. Configure Claude Code
Recommended shell setup.
macOS/Linux/WSL/Git Bash:
export ANTHROPIC_BASE_URL="https://api.rout.my"
export ANTHROPIC_AUTH_TOKEN="$ROUTMY_API_KEY"
export ANTHROPIC_MODEL="$ROUTMY_MODEL"
export ANTHROPIC_DEFAULT_HAIKU_MODEL="$ROUTMY_MODEL"Windows PowerShell:
$env:ANTHROPIC_BASE_URL="https://api.rout.my"
$env:ANTHROPIC_AUTH_TOKEN=$env:ROUTMY_API_KEY
$env:ANTHROPIC_MODEL=$env:ROUTMY_MODEL
$env:ANTHROPIC_DEFAULT_HAIKU_MODEL=$env:ROUTMY_MODELWindows cmd.exe:
set ANTHROPIC_BASE_URL=https://api.rout.my
set ANTHROPIC_AUTH_TOKEN=%ROUTMY_API_KEY%
set ANTHROPIC_MODEL=%ROUTMY_MODEL%
set ANTHROPIC_DEFAULT_HAIKU_MODEL=%ROUTMY_MODEL%Why ANTHROPIC_AUTH_TOKEN? Claude Code supports gateway auth through a bearer token, and rout.my accepts bearer auth. This also avoids overwriting a real ANTHROPIC_API_KEY you may use for direct Anthropic access.
Start Claude Code from the same shell:
claude --model "$ANTHROPIC_MODEL"Inside Claude Code, run:
/statusConfirm that the displayed API base URL is https://api.rout.my.
4. Optional model discovery
Claude Code can ask a gateway for models:
export CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1
claudeClaude Code's gateway model discovery calls GET /v1/models?limit=1000 on the configured base URL and requires Claude Code v2.1.129 or later. It only displays model IDs that start with claude or anthropic. If your rout.my model ID uses another prefix, set ANTHROPIC_MODEL explicitly as shown above.
5. Persist Claude Code settings
If users do not understand environment variables, this is usually the easiest path: put the values in the user-level Claude Code settings file.
Open the file:
# macOS/Linux/WSL
mkdir -p ~/.claude
nano ~/.claude/settings.json# Windows PowerShell
New-Item -ItemType Directory -Force "$HOME\.claude" | Out-Null
notepad "$HOME\.claude\settings.json":: Windows cmd.exe
mkdir "%USERPROFILE%\.claude"
notepad "%USERPROFILE%\.claude\settings.json"Paste this JSON and replace only sk_your_key_here. The model is already filled with the rout.my default:
{
"env": {
"ANTHROPIC_BASE_URL": "https://api.rout.my",
"ANTHROPIC_AUTH_TOKEN": "sk_your_key_here",
"ANTHROPIC_MODEL": "anthropic/claude-sonnet-4.6",
"ANTHROPIC_DEFAULT_HAIKU_MODEL": "anthropic/claude-sonnet-4.6",
"CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY": "1"
}
}Do not commit real API keys into a project repository. Use user-level settings or your shell environment for secrets.
6. VS Code extension
If you run Claude Code through the VS Code extension, pass the same variables through the extension setting:
{
"claudeCode.environmentVariables": [
{
"name": "ANTHROPIC_BASE_URL",
"value": "https://api.rout.my"
},
{
"name": "ANTHROPIC_AUTH_TOKEN",
"value": "sk_your_key_here"
},
{
"name": "ANTHROPIC_MODEL",
"value": "anthropic/claude-sonnet-4.6"
},
{
"name": "ANTHROPIC_DEFAULT_HAIKU_MODEL",
"value": "anthropic/claude-sonnet-4.6"
}
]
}Python SDK
Install the official Anthropic SDK:
pip install anthropicExample:
import os
from anthropic import Anthropic
client = Anthropic(
base_url="https://api.rout.my",
api_key=os.environ["ROUTMY_API_KEY"],
)
message = client.messages.create(
model=os.environ["ROUTMY_MODEL"],
max_tokens=256,
messages=[
{"role": "user", "content": "Write one short sentence."}
],
)
print(message.content[0].text)TypeScript SDK
Install the official Anthropic SDK:
npm install @anthropic-ai/sdkExample:
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
baseURL: 'https://api.rout.my',
apiKey: process.env.ROUTMY_API_KEY
});
const message = await client.messages.create({
model: process.env.ROUTMY_MODEL ?? 'anthropic/claude-sonnet-4.6',
max_tokens: 256,
messages: [{ role: 'user', content: 'Write one short sentence.' }]
});
console.log(message.content[0].type === 'text' ? message.content[0].text : message.content);The TypeScript SDK option is baseURL. The Python SDK option is base_url.
Token counting
Anthropic-native clients may call the token counting route:
curl https://api.rout.my/v1/messages/count_tokens \
-H "Authorization: Bearer $ROUTMY_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-sonnet-4.6",
"messages": [
{ "role": "user", "content": "Count this request." }
]
}'Expected response shape:
{ "input_tokens": 12 }The exact number depends on the selected model and estimator.
Common errors
| Symptom | What to check |
|---|---|
Request goes to /v1/v1/messages | The client base URL includes /v1. Use ANTHROPIC_BASE_URL=https://api.rout.my. |
PowerShell curl prints strange HTML or does not send headers | Use curl.exe, not the PowerShell alias. |
$ROUTMY_API_KEY works in Git Bash but not PowerShell | Variables are per shell. In PowerShell the variable is $env:ROUTMY_API_KEY; in cmd.exe it is %ROUTMY_API_KEY%. |
401 | Use either ANTHROPIC_AUTH_TOKEN=$ROUTMY_API_KEY for Claude Code or api_key=ROUTMY_API_KEY in SDKs. Do not accidentally send a real Anthropic key to rout.my. |
model_not_found | Run GET /v1/models with the same rout.my key and copy the exact model ID. |
| Claude Code ignores the URL | Start claude from the same shell or put the variables in ~/.claude/settings.json. Check with /status. |
| Claude Code uses another model | Set ANTHROPIC_MODEL for the main session and ANTHROPIC_DEFAULT_HAIKU_MODEL for Haiku/background usage. Check with /status. |
| Model discovery shows nothing | Claude Code only lists gateway models whose IDs start with claude or anthropic. Set ANTHROPIC_MODEL explicitly. |
| SDK still calls Anthropic directly | In Python use base_url; in TypeScript use baseURL. The value should be https://api.rout.my. |
anthropic-version error in curl | Include anthropic-version: 2023-06-01 for Anthropic-shaped manual requests. SDKs add the required headers for normal client calls. |