This guide explains how to configure OpenRouter for both Claude Code CLI and @link-assistant/agent, enabling you to use 500+ AI models from 60+ providers through a unified API.
- Overview
- Prerequisites
- Claude Code CLI with OpenRouter
- Agent CLI with OpenRouter
- Model Selection
- Verification
- Troubleshooting
OpenRouter provides a unified API gateway that allows you to access various AI models without requiring individual subscriptions. Benefits include:
- 500+ Models: Access to models from OpenAI, Anthropic, Google, Meta, and 60+ providers
- Pay-as-you-go: No monthly subscriptions required
- Unified API: Single API key works across all providers
- Fallback Support: Automatic failover between providers
- OpenRouter Account: Sign up at openrouter.ai
- API Key: Get your API key from OpenRouter Keys
- Claude Code CLI and/or @link-assistant/agent installed
Claude Code CLI can connect to OpenRouter using Anthropic's native protocol.
Add these to your shell profile (~/.bashrc, ~/.zshrc, or ~/.config/fish/config.fish):
# Required: Point Claude Code to OpenRouter
export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
# Required: Your OpenRouter API key
export ANTHROPIC_AUTH_TOKEN="sk-or-v1-your-api-key-here"
# Required: Must be explicitly blank to prevent conflicts
export ANTHROPIC_API_KEY=""Override default models with OpenRouter-compatible alternatives:
# Use specific models from OpenRouter
export ANTHROPIC_DEFAULT_SONNET_MODEL="anthropic/claude-sonnet-4"
export ANTHROPIC_DEFAULT_OPUS_MODEL="anthropic/claude-opus-4"
export ANTHROPIC_SMALL_FAST_MODEL="anthropic/claude-haiku"# Reload shell profile
source ~/.bashrc # or ~/.zshrcCreate .claude/settings.local.json in your project root:
{
"env": {
"ANTHROPIC_BASE_URL": "https://openrouter.ai/api",
"ANTHROPIC_AUTH_TOKEN": "sk-or-v1-your-api-key-here",
"ANTHROPIC_API_KEY": ""
}
}Note: Add .claude/settings.local.json to .gitignore to protect your API key.
cd /path/to/your/project
claude@link-assistant/agent supports OpenRouter through the agent auth login command or environment variables.
# Start interactive login
agent auth login
# Select "openrouter" from the provider list
# Enter your OpenRouter API key when promptedexport OPENROUTER_API_KEY="sk-or-v1-your-api-key-here"# Use any OpenRouter model with the openrouter/ prefix
echo "hello" | agent --model openrouter/anthropic/claude-sonnet-4
# Or use OpenCode Zen models (default)
echo "hello" | agent --model opencode/grok-code# List configured credentials
agent auth list
# Should show:
# ◆ openrouter api-key| Use Case | Environment Variable | Example Value |
|---|---|---|
| Main model | ANTHROPIC_DEFAULT_SONNET_MODEL |
anthropic/claude-sonnet-4 |
| Powerful model | ANTHROPIC_DEFAULT_OPUS_MODEL |
anthropic/claude-opus-4 |
| Fast/cheap model | ANTHROPIC_SMALL_FAST_MODEL |
anthropic/claude-haiku |
Use the openrouter/ prefix followed by the provider and model:
# Anthropic models
agent --model openrouter/anthropic/claude-sonnet-4
# OpenAI models
agent --model openrouter/openai/gpt-4o
# Google models
agent --model openrouter/google/gemini-2.0-flash
# Meta models
agent --model openrouter/meta-llama/llama-3.1-405b-instructWhen selecting alternative models, ensure they support tool use capabilities. Claude Code and agent rely on tools to:
- Read and write files
- Execute terminal commands
- Search codebases
- Perform web searches
Models without tool use support will not function properly.
Run /status within Claude Code to verify the connection:
Claude Code v1.x.x
Connected to: openrouter.ai
Model: anthropic/claude-sonnet-4
Also check the OpenRouter Activity Dashboard for real-time request logs.
# Simple test
echo "What is 2+2?" | agent --model openrouter/anthropic/claude-sonnet-4
# Check configured credentials
agent auth list- Verify your API key is correct at openrouter.ai/keys
- Ensure
ANTHROPIC_API_KEY=""is explicitly set (blank) for Claude Code - Check for typos in the
ANTHROPIC_AUTH_TOKENvalue
- Verify the model ID at openrouter.ai/models
- Use the full model path:
provider/model-name - Check if the model is available in your region
- Add credits at openrouter.ai/credits
- Check your usage at openrouter.ai/activity
Verify environment variables are set:
echo $ANTHROPIC_BASE_URL
# Should output: https://openrouter.ai/api
echo $ANTHROPIC_AUTH_TOKEN
# Should output: sk-or-v1-...
echo $ANTHROPIC_API_KEY
# Should be empty# Remove existing credentials
agent auth logout
# Select "openrouter"
# Re-authenticate
agent auth login
# Select "openrouter" and enter your API key- Never commit API keys: Add configuration files to
.gitignore - Use environment variables: Prefer shell profile over project files
- Rotate keys regularly: Generate new keys at openrouter.ai/keys
- Monitor usage: Check activity dashboard for suspicious requests