{"slug": "using-claude-code-with-openrouter-via-claude-code-router-on-linux", "title": "Using Claude Code with OpenRouter via `claude-code-router` on Linux", "summary": "A developer has created claude-code-router, a package that bridges Anthropic's Claude Code CLI with OpenRouter's API aggregator on Linux, enabling model selection flexibility and cost savings via prompt caching. The tool routes Claude Code requests through OpenRouter instead of Anthropic's direct API, supporting configuration via environment variables or JSON files. It leverages OpenRouter's automatic prompt caching to reduce API costs when working with large codebases.", "body_md": "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`\n\npackage bridges these two tools, allowing you to route Claude Code requests through OpenRouter instead of Anthropic's direct API.\n\nThis guide covers installation and configuration on Linux systems only.\n\nBefore beginning, ensure your system meets these requirements:\n\n```\n# Check Node.js version\nnode --version\n\n# Check npm version\nnpm --version\n\n# Check Git version\ngit --version\n```\n\nIf Node.js is not installed or outdated, install it using your distribution's package manager or NodeSource:\n\n```\n# Ubuntu/Debian - Using NodeSource\ncurl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -\nsudo apt-get install -y nodejs\n\n# Fedora\nsudo dnf install nodejs npm\n\n# Arch Linux\nsudo pacman -S nodejs npm\n```\n\n`claude-code-router`\n\nThe `claude-code-router`\n\npackage should be installed globally alongside Anthropic's official CLI tool.\n\n```\nnpm install -g claude-code-router @anthropic-ai/claude-code\n```\n\nCreate or edit your shell configuration file:\n\n```\n# For Bash\nnano ~/.bashrc\n\n# For Zsh\nnano ~/.zshrc\n```\n\nAdd the following lines:\n\n```\n# Claude Code Router Configuration\nexport OPENROUTER_API_KEY=\"your-openrouter-api-key-here\"\nexport CLAUDE_CODE_ROUTER_MODEL=\"anthropic/claude-3.7-sonnet\"\n```\n\nApply the changes:\n\n```\n# For Bash\nsource ~/.bashrc\n\n# For Zsh\nsource ~/.zshrc\n```\n\nAlternatively, create a configuration file in your home directory:\n\n```\nmkdir -p ~/.claude-code-router\nnano ~/.claude-code-router/config.json\n```\n\nAdd your configuration (updated with the correct Claude 3.7 model string):\n\n```\n{\n  \"apiKey\": \"your-openrouter-api-key-here\",\n  \"model\": \"anthropic/claude-3.7-sonnet\",\n  \"baseUrl\": \"https://openrouter.ai/api/v1\",\n  \"maxTokens\": 8192,\n  \"temperature\": 0.7\n}\n```\n\nIf 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:\n\n```\n# Create a secure environment file\ntouch ~/.claude-code-router/.env\nchmod 600 ~/.claude-code-router/.env\nnano ~/.claude-code-router/.env\n```\n\nAdd the key inside the file:\n\n```\nOPENROUTER_API_KEY=sk-or-v1-your-actual-key-here\n```\n\nOne of the primary benefits of routing Claude Code through OpenRouter is natively leveraging **Prompt Caching** to heavily optimize your API costs.\n\nWhen 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:\n\nNo extra configuration is required in `claude-code-router`\n\nto enable this feature—OpenRouter handles the upstream caching headers automatically when translating requests to Anthropic's backend.\n\nConfigure Claude Code to point to the local proxy, then initialize the router:\n\n```\nexport ANTHROPIC_BASE_URL=\"http://localhost:8080\"\nccr code\n```\n\nIf you want to quickly switch models without editing config files manually:\n\n```\nccr ui\n```\n\n*Note: This command redirects to a local web UI to configure your preferred OpenRouter models.*\n\nCheck [OpenRouter's model list](https://openrouter.ai/models) for the most current routing options and model strings.\n\nThe `claude-code-router`\n\npackage 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.\n\nFor additional help, consult the package's documentation on npm or GitHub, and periodically check OpenRouter's documentation for API changes or new model additions.", "url": "https://wpnews.pro/news/using-claude-code-with-openrouter-via-claude-code-router-on-linux", "canonical_source": "https://dev.to/mbuyco/using-claude-code-with-openrouter-via-claude-code-router-on-linux-1f8p", "published_at": "2026-06-29 08:13:52+00:00", "updated_at": "2026-06-29 08:27:24.295113+00:00", "lang": "en", "topics": ["developer-tools", "large-language-models", "ai-tools"], "entities": ["Claude Code", "OpenRouter", "Anthropic", "claude-code-router"], "alternates": {"html": "https://wpnews.pro/news/using-claude-code-with-openrouter-via-claude-code-router-on-linux", "markdown": "https://wpnews.pro/news/using-claude-code-with-openrouter-via-claude-code-router-on-linux.md", "text": "https://wpnews.pro/news/using-claude-code-with-openrouter-via-claude-code-router-on-linux.txt", "jsonld": "https://wpnews.pro/news/using-claude-code-with-openrouter-via-claude-code-router-on-linux.jsonld"}}