Using Routic with Claude Code

Route Claude Code CLI through Routic's Anthropic-compatible endpoint to use Claude models at a lower cost.

How it works

Routic supports both OpenAI-compatible and Anthropic-compatible message formats. Claude Code uses Anthropic's /v1/messages API path by default. Routic proxies this path and routes requests to the appropriate backend model.

Prerequisites

  • Claude Code CLI installed (npm install -g @anthropic-ai/claude-code or via Anthropic's official installer)
  • A Routic Base URL and Virtual Key

Setup

Option 1: Environment variables (recommended)

Add to your shell config (~/.bashrc, ~/.zshrc, etc.):

export ANTHROPIC_BASE_URL="https://YOUR_GATEWAY_HOST"
export ANTHROPIC_API_KEY="YOUR_ROUTIC_VIRTUAL_KEY"

Then source ~/.zshrc (or restart your terminal) and use claude as normal.

Note: Set ANTHROPIC_BASE_URL to the host only — no trailing /v1. Claude Code appends the path automatically.

Option 2: Inline per command

ANTHROPIC_BASE_URL="https://YOUR_GATEWAY_HOST" \
ANTHROPIC_API_KEY="YOUR_ROUTIC_VIRTUAL_KEY" \
claude "Review this function for me"

Option 3: Config file

Create or edit a config under the project directory or ~/.claude/ (check Claude Code docs for the exact format for your version):

{
  "api": {
    "baseURL": "https://YOUR_GATEWAY_HOST",
    "apiKey": "YOUR_ROUTIC_VIRTUAL_KEY"
  }
}

Verify

claude "hello, reply with pong"

A normal response confirms the setup is working. A 401 or authentication_error means the Virtual Key is wrong or expired.

Specify a model

Claude Code uses the latest Claude Sonnet / Opus by default. To pin a specific version:

claude --model claude-3-5-sonnet-20241022 "your prompt"

Model names must match Routic's catalog exactly.

Troubleshooting

invalid_api_key
The Virtual Key is malformed or expired. Regenerate it from the console.

Still hitting Anthropic directly after setting ANTHROPIC_BASE_URL
Confirm the variable is exported: echo $ANTHROPIC_BASE_URL. You may need to restart the terminal session.

Responses look wrong
You may have specified a model name not in the Routic catalog. Check routing & models.

Related