Claude Code

Use Claude Code with OpenRouter

Claude Code is a powerful agentic tool. While you can use any model via OpenRouter, we recommend sticking to highly capable models (like Claude Opus 4.5, GPT 5.2, etc.) for the best experience, as complex coding tasks require strong reasoning.

Quick Start

This guide will get you running Claude Code powered by OpenRouter in just a few minutes.

Step 1: Install Claude Code

Step 2: Connect Claude to OpenRouter

Instead of logging in with Anthropic directly, connect Claude Code to OpenRouter. This requires setting a few environment variables.

Requirements:

  1. Use https://openrouter.ai/api for the base url
  2. Provide your OpenRouter API key as the auth token
  3. Important: Explicitly blank out the Anthropic API key to prevent conflicts

Add these environment variables to your shell profile:

$# Set these in your shell (e.g., ~/.bashrc, ~/.zshrc)
>export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
>export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
>export ANTHROPIC_API_KEY="" # Important: Must be explicitly empty

Persistence: We recommend adding these lines to your shell profile (~/.bashrc, ~/.zshrc, or ~/.config/fish/config.fish).

Do not put these in a project-level .env file. The native Claude Code installer does not read standard .env files.

Step 3: Start your session

Navigate to your project directory and start Claude Code:

$cd /path/to/your/project
>claude

You are now connected! Any prompt you send will be routed through OpenRouter.

Step 4: Verify

You can confirm your connection by running the /status command inside Claude Code.

> /status
Auth token: ANTHROPIC_AUTH_TOKEN
Anthropic base URL: https://openrouter.ai/api

You can also check the OpenRouter Activity Dashboard to see your requests appearing in real-time.

Changing Models

By default, Claude Code uses specific Anthropic models aliases (like “Sonnet”, “Opus”, “Haiku”). OpenRouter automatically maps these to the correct Anthropic models.

Overriding Default Models

You can configure Claude Code to use any model on OpenRouter (including OpenAI, Google, or Llama models) by overriding the default model aliases using environment variables.

Tool Use Required: Claude Code relies on tool use capabilities to perform actions like reading files, running terminal commands, and editing code. When selecting alternative models, make sure they support tool use. See the full list of models that support tool use.

For example, to swap the default “Sonnet” alias for GPT-5.1 Codex Max:

$export ANTHROPIC_DEFAULT_SONNET_MODEL="openai/gpt-5.1-codex-max"

You can override other tiers as well:

$export ANTHROPIC_DEFAULT_OPUS_MODEL="openai/gpt-5.2-pro"
>export ANTHROPIC_DEFAULT_HAIKU_MODEL="minimax/minimax-m2:exacto"

Tip: While you can use any model, Claude Code relies deeply on agentic behaviors (tool use, precise formatting). For best results, we recommend using Exacto—our specialized coding and tool-use model—designed for strong code generation and reliable agentic performance. Learn more about Exacto on our Exacto variant page.

Advanced Configuration with Presets

For more complex configurations—such as defining fallback models, custom system prompts, or specific provider routing—you can use OpenRouter Presets.

  1. Create a preset at openrouter.ai/settings/presets (e.g., with slug my-coding-setup).
  2. Use the preset as your model override:
$export ANTHROPIC_DEFAULT_SONNET_MODEL="@preset/my-coding-setup"

This allows you to manage model settings and fallbacks on OpenRouter without changing your local environment variables.

How It Works

OpenRouter exposes an input that is compatible with the Anthropic Messages API.

  1. Direct Connection: When you set ANTHROPIC_BASE_URL to https://openrouter.ai/api, Claude Code speaks its native protocol directly to OpenRouter. No local proxy server is required.
  2. Anthropic Skin: OpenRouter’s “Anthropic Skin” behaves exactly like the Anthropic API. It handles model mapping and passes through advanced features like “Thinking” blocks and native tool use.
  3. Billing: You are billed using your OpenRouter credits. Usage (including reasoning tokens) appears in your OpenRouter dashboard.

Using Your Claude Subscription

If you have a Claude subscription, you can use it for Anthropic models while routing non-Anthropic models through OpenRouter. This gives you the best of both worlds: your Claude subscription handles Anthropic models, while OpenRouter provides access to its full model catalog.

How It Works

When you use Claude Code with OpenRouter and have a Claude subscription, Claude Code automatically sends your subscription token via the x-api-key header. OpenRouter detects this and routes accordingly:

  • Anthropic models (Claude Sonnet, Opus, Haiku) → Routed using your Claude subscription
  • Non-Anthropic models (GPT, Gemini, Llama, etc.) → Routed through OpenRouter, billed to your OpenRouter credits
  • Fallback → If your Claude subscription limits are reached, requests automatically fall back to OpenRouter

No additional configuration is needed beyond the standard setup above. Claude Code handles sending your subscription credentials automatically.

Agent SDK

The Anthropic Agent SDK lets you build AI agents programmatically using Python or TypeScript. Since the Agent SDK uses Claude Code as its runtime, you can connect it to OpenRouter using the same environment variables described above.

For complete setup instructions and code examples, see our Anthropic Agent SDK integration guide.

GitHub Action

You can use OpenRouter with the official Claude Code GitHub Action.To adapt the example workflow for OpenRouter, make two changes to the action step:

  1. Pass your OpenRouter API key via anthropic_api_key (store it as a GitHub secret named OPENROUTER_API_KEY)
  2. Set the ANTHROPIC_BASE_URL environment variable to https://openrouter.ai/api
1- name: Run Claude Code
2 uses: anthropics/claude-code-action@v1
3 with:
4 anthropic_api_key: ${{ secrets.OPENROUTER_API_KEY }}
5 env:
6 ANTHROPIC_BASE_URL: https://openrouter.ai/api

Cost Tracking Statusline

You can add a custom statusline to Claude Code that tracks your OpenRouter API costs in real-time. The statusline displays the provider, model, cumulative cost, and cache discounts for your session.

Claude Code statusline showing OpenRouter cost tracking

Download the statusline scripts from the openrouter-examples repository, make them executable, and add the following to your ~/.claude/settings.json:

1{
2 "statusLine": {
3 "type": "command",
4 "command": "/path/to/statusline.sh"
5 }
6}

The script uses your ANTHROPIC_AUTH_TOKEN environment variable, which should already be set to your OpenRouter API key if you followed the setup above.

Troubleshooting

  • Tool Use Errors: If you see errors about tool use not being supported, the model you selected does not support tool use capabilities. Claude Code requires tool use to read files, run commands, and edit code. Switch to a model that supports tool use.
  • Auth Errors: Ensure ANTHROPIC_API_KEY is set to an empty string (""). If it is unset (null), Claude Code might fall back to its default behavior and try to authenticate with Anthropic servers.
  • Context Length Errors: If you swap deep-reasoning tasks to smaller models, you may hit context limits. Use models with at least 128k context windows for best results.
  • Privacy: OpenRouter does not log your source code prompts unless you explicitly opt-in to prompt logging in your account settings. See our Privacy Policy for details.