Introduction
Claude Code is Anthropic's powerful command-line coding assistant that brings AI directly into your terminal, while OpenRouter is an API aggregator that provides access to multiple AI models through a unified interface. The claude-code-router package bridges these two tools, allowing you to route Claude Code requests through OpenRouter instead of Anthropic's direct API.
This guide covers installation and configuration on Linux systems only.
Prerequisites
Before beginning, ensure your system meets these requirements:
System Requirements
- OS: Any modern Linux distribution (Ubuntu 20.04+, Debian 11+, Fedora 36+, Arch Linux, etc.)
- Node.js: Version 18.0.0 or higher
- npm: Version 8.0.0 or higher
- Git: For cloning repositories if needed
Verify Your Setup
# Check Node.js version
node --version
# Check npm version
npm --version
# Check Git version
git --version
If Node.js is not installed or outdated, install it using your distribution's package manager or NodeSource:
# Ubuntu/Debian - Using NodeSource
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
# Fedora
sudo dnf install nodejs npm
# Arch Linux
sudo pacman -S nodejs npm
Obtaining an OpenRouter API Key
- Navigate to openrouter.ai
- Create an account or sign in
- Go to Keys in your account dashboard
- Click Create Key
- Copy the generated API key and store it securely
Installing claude-code-router
The claude-code-router package should be installed globally alongside Anthropic's official CLI tool.
npm install -g claude-code-router @anthropic-ai/claude-code
Configuration
Method 1: Environment Variables
Create or edit your shell configuration file:
# For Bash
nano ~/.bashrc
# For Zsh
nano ~/.zshrc
Add the following lines:
# Claude Code Router Configuration
export OPENROUTER_API_KEY="your-openrouter-api-key-here"
export CLAUDE_CODE_ROUTER_MODEL="anthropic/claude-3.7-sonnet"
Apply the changes:
# For Bash
source ~/.bashrc
# For Zsh
source ~/.zshrc
Method 2: Configuration File
Alternatively, create a configuration file in your home directory:
mkdir -p ~/.claude-code-router
nano ~/.claude-code-router/config.json
Add your configuration (updated with the correct Claude 3.7 model string):
{
"apiKey": "your-openrouter-api-key-here",
"model": "anthropic/claude-3.7-sonnet",
"baseUrl": "https://openrouter.ai/api/v1",
"maxTokens": 8192,
"temperature": 0.7
}
Secure Your API Key
If you prefer using an environment file over a JSON config, ensure you lock down the file permissions so other users on the Linux system cannot read your API key:
# Create a secure environment file
touch ~/.claude-code-router/.env
chmod 600 ~/.claude-code-router/.env
nano ~/.claude-code-router/.env
Add the key inside the file:
OPENROUTER_API_KEY=sk-or-v1-your-actual-key-here
Token Cost Optimization & Prompt Caching
One of the primary benefits of routing Claude Code through OpenRouter is natively leveraging Prompt Caching to heavily optimize your API costs.
When working in large codebases, Claude Code sends significant contextual data (file structures, previous messages, and active file contents) with every single request. OpenRouter automatically supports Anthropic's prompt caching functionality:
- Initial Requests: Standard input token pricing applies as the context is written to the cache.
- Subsequent Requests: As long as your prompt prefix remains identical (within the 5-minute cache TTL), you receive up to a 90% discount on cached input tokens.
No extra configuration is required in claude-code-router to enable this feature—OpenRouter handles the upstream caching headers automatically when translating requests to Anthropic's backend.
Usage
Configure Claude Code to point to the local proxy, then initialize the router:
export ANTHROPIC_BASE_URL="http://localhost:8080"
ccr code
Configure LLM Models
If you want to quickly switch models without editing config files manually:
ccr ui
Note: This command redirects to a local web UI to configure your preferred OpenRouter models.
Check OpenRouter's model list for the most current routing options and model strings.
Conclusion
The claude-code-router package provides a seamless way to use Claude Code through OpenRouter on Linux, giving you absolute flexibility in model selection while drastically cutting costs via prompt caching. By following this guide, you now have a secure, system-integrated proxy bridging Claude Code with OpenRouter's API infrastructure.
For additional help, consult the package's documentation on npm or GitHub, and periodically check OpenRouter's documentation for API changes or new model additions.
Top comments (0)