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.
Before beginning, ensure your system meets these requirements:
node --version
npm --version
git --version
If Node.js is not installed or outdated, install it using your distribution's package manager or NodeSource:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo dnf install nodejs npm
sudo pacman -S nodejs npm
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
Create or edit your shell configuration file:
nano ~/.bashrc
nano ~/.zshrc
Add the following lines:
export OPENROUTER_API_KEY="your-openrouter-api-key-here"
export CLAUDE_CODE_ROUTER_MODEL="anthropic/claude-3.7-sonnet"
Apply the changes:
source ~/.bashrc
source ~/.zshrc
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
}
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:
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
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:
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.
Configure Claude Code to point to the local proxy, then initialize the router:
export ANTHROPIC_BASE_URL="http://localhost:8080"
ccr code
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.
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.