DEV Community

Mike Buyco
Mike Buyco

Posted on

Using Claude Code with OpenRouter via `claude-code-router` on Linux

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Obtaining an OpenRouter API Key

  1. Navigate to openrouter.ai
  2. Create an account or sign in
  3. Go to Keys in your account dashboard
  4. Click Create Key
  5. 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
Enter fullscreen mode Exit fullscreen mode

Configuration

Method 1: Environment Variables

Create or edit your shell configuration file:

# For Bash
nano ~/.bashrc

# For Zsh
nano ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

Apply the changes:

# For Bash
source ~/.bashrc

# For Zsh
source ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

Method 2: Configuration File

Alternatively, create a configuration file in your home directory:

mkdir -p ~/.claude-code-router
nano ~/.claude-code-router/config.json
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Add the key inside the file:

OPENROUTER_API_KEY=sk-or-v1-your-actual-key-here
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Configure LLM Models

If you want to quickly switch models without editing config files manually:

ccr ui 
Enter fullscreen mode Exit fullscreen mode

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)