Skip to content

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:

text
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_tokens

Claude 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:

bash
# macOS/Linux/WSL/Git Bash
export ROUTMY_API_KEY="sk_your_key_here"
powershell
# Windows PowerShell
$env:ROUTMY_API_KEY="sk_your_key_here"
bat
:: Windows cmd.exe
set ROUTMY_API_KEY=sk_your_key_here

Then check the models list.

macOS/Linux/WSL/Git Bash:

bash
curl https://api.rout.my/v1/models \
  -H "Authorization: Bearer $ROUTMY_API_KEY"

Windows PowerShell:

powershell
curl.exe https://api.rout.my/v1/models -H "Authorization: Bearer $env:ROUTMY_API_KEY"

Windows cmd.exe:

bat
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:

text
anthropic/claude-sonnet-4.6

You do not need to choose a model manually. Use the default unless you intentionally want another option:

Use caseModel
Default Claude Code setupanthropic/claude-sonnet-4.6
Stronger, more expensive Claude setupanthropic/claude-opus-4.6

If you change the model, copy the exact ID from /v1/models.

Set the model ID in the same terminal:

bash
# macOS/Linux/WSL/Git Bash
export ROUTMY_MODEL="anthropic/claude-sonnet-4.6"
powershell
# Windows PowerShell
$env:ROUTMY_MODEL="anthropic/claude-sonnet-4.6"
bat
:: Windows cmd.exe
set ROUTMY_MODEL=anthropic/claude-sonnet-4.6

2. Test the Anthropic route

Before opening Claude Code, test the raw Messages endpoint:

bash
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:

powershell
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:

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:

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_MODEL

Windows cmd.exe:

bat
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:

bash
claude --model "$ANTHROPIC_MODEL"

Inside Claude Code, run:

text
/status

Confirm that the displayed API base URL is https://api.rout.my.

4. Optional model discovery

Claude Code can ask a gateway for models:

bash
export CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1
claude

Claude 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:

bash
# macOS/Linux/WSL
mkdir -p ~/.claude
nano ~/.claude/settings.json
powershell
# Windows PowerShell
New-Item -ItemType Directory -Force "$HOME\.claude" | Out-Null
notepad "$HOME\.claude\settings.json"
bat
:: 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:

json
{
  "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:

json
{
  "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:

bash
pip install anthropic

Example:

python
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:

bash
npm install @anthropic-ai/sdk

Example:

ts
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:

bash
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:

json
{ "input_tokens": 12 }

The exact number depends on the selected model and estimator.

Common errors

SymptomWhat to check
Request goes to /v1/v1/messagesThe client base URL includes /v1. Use ANTHROPIC_BASE_URL=https://api.rout.my.
PowerShell curl prints strange HTML or does not send headersUse curl.exe, not the PowerShell alias.
$ROUTMY_API_KEY works in Git Bash but not PowerShellVariables are per shell. In PowerShell the variable is $env:ROUTMY_API_KEY; in cmd.exe it is %ROUTMY_API_KEY%.
401Use 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_foundRun GET /v1/models with the same rout.my key and copy the exact model ID.
Claude Code ignores the URLStart claude from the same shell or put the variables in ~/.claude/settings.json. Check with /status.
Claude Code uses another modelSet ANTHROPIC_MODEL for the main session and ANTHROPIC_DEFAULT_HAIKU_MODEL for Haiku/background usage. Check with /status.
Model discovery shows nothingClaude Code only lists gateway models whose IDs start with claude or anthropic. Set ANTHROPIC_MODEL explicitly.
SDK still calls Anthropic directlyIn Python use base_url; in TypeScript use baseURL. The value should be https://api.rout.my.
anthropic-version error in curlInclude anthropic-version: 2023-06-01 for Anthropic-shaped manual requests. SDKs add the required headers for normal client calls.

Sources

API documentation for rout.my.