> ## Documentation Index
> Fetch the complete documentation index at: https://docs.llmrouter.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Claude Code

> Route your terminal-based Claude Code agents through LLM Router to automatically apply context compression, enforce PII redaction, and handle provider fallbacks.

***

# Claude Code and Claude Agent SDK

LLM Router provides a fully compatible Anthropic endpoint (`/v1`), meaning you can point [Claude Code](https://www.claude.com/product/claude-code) and the [Claude Agent SDK](https://docs.anthropic.com/en/docs/agent-sdk/overview) 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.

<Note>
  **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.
</Note>

## Configuring Claude Code (CLI)

[Claude Code](https://code.claude.com/docs) 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:

```bash theme={null}
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`):

```bash theme={null}
export ANTHROPIC_BASE_URL="https://api.llmrouter.app/v1"
export ANTHROPIC_AUTH_TOKEN="sk-router-your-api-key"
export ANTHROPIC_API_KEY=""
```

<Warning>
  **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.
</Warning>

### 3. Run Claude Code

Reload your terminal (e.g., `source ~/.zshrc`) and start Claude Code:

```bash theme={null}
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:

```bash theme={null}
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:

```bash theme={null}
export ANTHROPIC_AUTH_TOKEN=$(security find-generic-password -a "$USER" -s "ANTHROPIC_AUTH_TOKEN" -w)
```

***

## With the Claude Agent SDK

The [Claude Agent SDK](https://docs.anthropic.com/en/docs/agent-sdk/overview) (`@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:

```typescript agent.ts theme={null}
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.
