Skip to main content

Claude Code and Claude Agent SDK

LLM Router provides a fully compatible Anthropic endpoint (/v1), meaning you can point Claude Code and the Claude Agent SDK directly to your gateway. By routing Claude Code through LLM Router, you instantly unlock:
  • Cost Savings: Automatic context compression on long, token-heavy terminal sessions.
  • Security: Enterprise PII redaction (masking API keys and passwords in your local .env files from being sent to Anthropic).
  • Resilience: Automatic failover to other providers (like Amazon Bedrock or Google Vertex AI) if the primary Anthropic API goes down.
Provider Fallback Note: If you configure LLM Router to fall back from Anthropic to Amazon Bedrock or Vertex AI, be aware that Claude Code automatically injects Anthropic-specific beta headers. To prevent errors during fallback, set CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1 in your environment.

Configuring Claude Code (CLI)

Claude Code is Anthropic’s agentic coding tool that runs in your terminal. You can easily configure it to send all traffic through LLM Router.

1. Clear Existing Auth

First, log out if you are already logged into the default Anthropic service:
claude /logout

2. Configure Environment Variables

To point Claude Code to LLM Router, add the following variables to your shell configuration file (e.g., ~/.zshrc or ~/.bashrc):
export ANTHROPIC_BASE_URL="https://api.llmrouter.app/v1"
export ANTHROPIC_AUTH_TOKEN="sk-router-your-api-key"
export ANTHROPIC_API_KEY=""
Important: Setting ANTHROPIC_API_KEY to an empty string "" is required. Claude Code checks this variable first. If it has a value, Claude Code will ignore the ANTHROPIC_AUTH_TOKEN and attempt to bypass the gateway.

3. Run Claude Code

Reload your terminal (e.g., source ~/.zshrc) and start Claude Code:
claude
Your local coding agent is now securely routed through LLM Router.

(Optional) macOS: Secure Token Storage

If you are on a Mac and prefer to manage your LLM Router API key through Keychain for better local security, run this command:
security add-generic-password -a "$USER" -s "ANTHROPIC_AUTH_TOKEN" -w "sk-router-your-api-key"
Then, update your ~/.zshrc export to read from the Keychain:
export ANTHROPIC_AUTH_TOKEN=$(security find-generic-password -a "$USER" -s "ANTHROPIC_AUTH_TOKEN" -w)

With the Claude Agent SDK

The Claude Agent SDK (@anthropic-ai/claude-agent-sdk) lets you build custom AI agents using the same core loop that powers Claude Code. You can route these custom SDK requests through LLM Router by overriding the env option in your query:
agent.ts
import { query } from "@anthropic-ai/claude-agent-sdk";

async function runAgent() {
  for await (const message of query({
    prompt: "Find and fix the database connection bug in auth.ts",
    options: {
      model: "anthropic/claude-3-5-sonnet",
      allowedTools: ["Read", "Edit", "Bash"],

      // Override the environment to point to LLM Router
      env: {
        ...process.env,
        ANTHROPIC_BASE_URL: "https://api.llmrouter.app/v1",
        ANTHROPIC_AUTH_TOKEN: process.env.LLM_ROUTER_API_KEY,
        ANTHROPIC_API_KEY: "", // Must be empty
      },
    },
  })) {
    if ("result" in message) {
      console.log(message.result);
    }
  }
}

runAgent();
Because the Agent SDK spawns Claude Code as a subprocess, the same environment variables apply. All file reads, tool executions, and LLM calls will now benefit from LLM Router’s context compression and redaction features.