<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: AlwaysAgent</title>
    <description>The latest articles on DEV Community by AlwaysAgent (@alwaysagent).</description>
    <link>https://dev.to/alwaysagent</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4260102%2Fcb883070-d1f9-4d99-9076-be8efa5729a5.png</url>
      <title>DEV Community: AlwaysAgent</title>
      <link>https://dev.to/alwaysagent</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alwaysagent"/>
    <language>en</language>
    <item>
      <title>How to use an OpenRouter API key in Claude Code</title>
      <dc:creator>AlwaysAgent</dc:creator>
      <pubDate>Mon, 23 Nov 2026 08:58:29 +0000</pubDate>
      <link>https://dev.to/alwaysagent/how-to-use-an-openrouter-api-key-in-claude-code-2b9m</link>
      <guid>https://dev.to/alwaysagent/how-to-use-an-openrouter-api-key-in-claude-code-2b9m</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://alwaysagent.app/claude-code-openrouter" rel="noopener noreferrer"&gt;alwaysagent.app&lt;/a&gt;. The original is kept up to date.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;OpenRouter has an official Claude Code integration. Four environment variables and a logout are all it takes. This guide covers the full config, verification, model switching, costs, troubleshooting, server setups, and the open-source Claude Code Router.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The short answer: four variables
&lt;/h2&gt;

&lt;p&gt;To use an OpenRouter API key in Claude Code, point &lt;code&gt;ANTHROPIC_BASE_URL&lt;/code&gt; at &lt;code&gt;https://openrouter.ai/api&lt;/code&gt;, put your key in &lt;code&gt;ANTHROPIC_AUTH_TOKEN&lt;/code&gt;, set &lt;code&gt;ANTHROPIC_API_KEY&lt;/code&gt; to an empty string, and log out of any Anthropic account. This is the setup described in OpenRouter's official Claude Code integration guide.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Terminal&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;OPENROUTER_API_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;your-openrouter-key&amp;gt;"&lt;/span&gt;
&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;ANTHROPIC_BASE_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"https://openrouter.ai/api"&lt;/span&gt;
&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;ANTHROPIC_AUTH_TOKEN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"$OPENROUTER_API_KEY"&lt;/span&gt;
&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;ANTHROPIC_API_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The base URL is &lt;code&gt;https://openrouter.ai/api&lt;/code&gt;, with no &lt;code&gt;/v1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The key goes in &lt;code&gt;ANTHROPIC_AUTH_TOKEN&lt;/code&gt;, which Claude Code sends as &lt;code&gt;Authorization: Bearer&lt;/code&gt;. &lt;code&gt;ANTHROPIC_API_KEY&lt;/code&gt; is sent as &lt;code&gt;x-api-key&lt;/code&gt; and treated as a direct Anthropic credential, so it must stay blank.&lt;/li&gt;
&lt;li&gt;Define &lt;code&gt;OPENROUTER_API_KEY&lt;/code&gt; before the line that references it, or the token expands to nothing and you get an auth error.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Shell variables or settings.json?
&lt;/h2&gt;

&lt;p&gt;Both work. Shell variables are the fastest way to try it; &lt;code&gt;settings.json&lt;/code&gt; is better when you want the config to apply however &lt;code&gt;claude&lt;/code&gt; is launched.&lt;/p&gt;

&lt;h3&gt;
  
  
  Option 1: shell profile
&lt;/h3&gt;

&lt;p&gt;Add the exports above to &lt;code&gt;~/.zshrc&lt;/code&gt;, &lt;code&gt;~/.bashrc&lt;/code&gt;, or your fish config, then reload it with &lt;code&gt;source ~/.zshrc&lt;/code&gt; or open a new terminal. OpenRouter also suggests an optional &lt;code&gt;CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1&lt;/code&gt;, which turns on a gateway model picker (more on that below).&lt;/p&gt;

&lt;h3&gt;
  
  
  Option 2: settings.json
&lt;/h3&gt;

&lt;p&gt;Claude Code reads an &lt;code&gt;env&lt;/code&gt; block from its settings files. Use &lt;code&gt;~/.claude/settings.json&lt;/code&gt; for all projects, or &lt;code&gt;.claude/settings.local.json&lt;/code&gt; for one project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;~/.claude/settings.json&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"env"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"ANTHROPIC_BASE_URL"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"https://openrouter.ai/api"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"ANTHROPIC_AUTH_TOKEN"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;your-openrouter-key&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"ANTHROPIC_API_KEY"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"1"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Never put the key in a project's &lt;code&gt;.claude/settings.json&lt;/code&gt;. Anthropic's docs warn that this file is committed and shared with everyone who clones the repo.&lt;/li&gt;
&lt;li&gt;Don't rely on a project &lt;code&gt;.env&lt;/code&gt; file. OpenRouter notes the native installer does not read it.&lt;/li&gt;
&lt;li&gt;If the same variable is set both in your shell and in a settings &lt;code&gt;env&lt;/code&gt; block, Anthropic's docs say the settings value applies. To avoid confusion, configure it in one place only.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step-by-step setup and verification
&lt;/h2&gt;

&lt;p&gt;The whole process takes a few minutes, and &lt;code&gt;/status&lt;/code&gt; tells you whether it worked.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Install Claude Code&lt;/strong&gt; — Use the native installer from Anthropic (&lt;code&gt;curl -fsSL https://claude.ai/install.sh | bash&lt;/code&gt; on macOS, Linux, and WSL) or &lt;code&gt;npm install -g @anthropic-ai/claude-code&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Create an OpenRouter key&lt;/strong&gt; — Add credits and create an API key in your OpenRouter dashboard.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Set the variables&lt;/strong&gt; — Use either option above, not both.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Log out of Anthropic&lt;/strong&gt; — If you ever signed in with a claude.ai account, run &lt;code&gt;/logout&lt;/code&gt;, quit, and start &lt;code&gt;claude&lt;/code&gt; again. The gateway credential takes precedence over a saved login, but logging out avoids auth-conflict warnings.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check &lt;code&gt;/status&lt;/code&gt;&lt;/strong&gt; — Run &lt;code&gt;claude&lt;/code&gt;, then &lt;code&gt;/status&lt;/code&gt;. You should see &lt;code&gt;Auth token: ANTHROPIC_AUTH_TOKEN&lt;/code&gt; and &lt;code&gt;Anthropic base URL: https://openrouter.ai/api&lt;/code&gt;. If the auth line names &lt;code&gt;ANTHROPIC_API_KEY&lt;/code&gt; or a Claude account, reload your shell and restart &lt;code&gt;claude&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Send a prompt and check the dashboard&lt;/strong&gt; — Send any message. A normal reply confirms it works, and the request should appear in your OpenRouter Activity dashboard with the model name.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How to choose and switch models
&lt;/h2&gt;

&lt;p&gt;Claude Code picks models by alias (&lt;code&gt;opus&lt;/code&gt;, &lt;code&gt;sonnet&lt;/code&gt;, &lt;code&gt;haiku&lt;/code&gt;), and you can map each alias to an OpenRouter model ID with environment variables. Anthropic notes that &lt;code&gt;ANTHROPIC_BASE_URL&lt;/code&gt; changes where requests go, not which model answers them, so the mapping is what decides the model.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Inside a session:&lt;/strong&gt; &lt;code&gt;/model&lt;/code&gt; opens the picker; &lt;code&gt;/model sonnet&lt;/code&gt; switches immediately.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;At startup:&lt;/strong&gt; &lt;code&gt;claude --model opus&lt;/code&gt; applies to that session only.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Persistently:&lt;/strong&gt; set &lt;code&gt;"model"&lt;/code&gt; in your settings file, or &lt;code&gt;ANTHROPIC_MODEL&lt;/code&gt; in the environment.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To pin which OpenRouter model each alias resolves to, set these in your shell or &lt;code&gt;env&lt;/code&gt; block:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Terminal&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;ANTHROPIC_DEFAULT_OPUS_MODEL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;openrouter-model-id&amp;gt;"&lt;/span&gt;
&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;ANTHROPIC_DEFAULT_SONNET_MODEL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;openrouter-model-id&amp;gt;"&lt;/span&gt;
&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;ANTHROPIC_DEFAULT_HAIKU_MODEL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;openrouter-model-id&amp;gt;"&lt;/span&gt;
&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;CLAUDE_CODE_SUBAGENT_MODEL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;openrouter-model-id&amp;gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use the exact IDs from the OpenRouter model list, because names and versions change often. OpenRouter's guide also documents "latest" style aliases for the Claude families; check its page for the current spelling. After setting them, open &lt;code&gt;/model&lt;/code&gt; and confirm the expected entries appear, then check the model column in the OpenRouter dashboard.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ANTHROPIC_DEFAULT_HAIKU_MODEL&lt;/code&gt; also covers background tasks, so pin it too if you want predictable costs. &lt;code&gt;ANTHROPIC_SMALL_FAST_MODEL&lt;/code&gt; is deprecated.&lt;/p&gt;

&lt;h3&gt;
  
  
  Gateway model picker
&lt;/h3&gt;

&lt;p&gt;With &lt;code&gt;CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1&lt;/code&gt;, Claude Code asks the gateway for its model list at startup and adds those names to &lt;code&gt;/model&lt;/code&gt;. OpenRouter says this shows a curated set of top-ranked models rather than the full catalog, and that pinning a model through the variables above bypasses it. Restart Claude Code to refresh the list.&lt;/p&gt;

&lt;h3&gt;
  
  
  Context window
&lt;/h3&gt;

&lt;p&gt;OpenRouter's guide says Claude Code assumes a 200k context unless the model name ends in &lt;code&gt;[1m]&lt;/code&gt;, and that OpenRouter strips the marker before routing. If your context seems stuck at 200k, add the suffix to model IDs that support 1M.&lt;/p&gt;

&lt;h2&gt;
  
  
  Costs and credits
&lt;/h2&gt;

&lt;p&gt;With OpenRouter, you pay OpenRouter per token from your credit balance; there is no Claude subscription quota involved. Anthropic's docs confirm that once a gateway credential is set, requests no longer count against a claude.ai plan.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Per-request view:&lt;/strong&gt; OpenRouter's Activity dashboard shows usage, cost, and the model for each request.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Budgets:&lt;/strong&gt; OpenRouter says teams can set spending limits and allocate credits. Keep your balance topped up, since an empty balance shows up as request errors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In-terminal cost:&lt;/strong&gt; OpenRouter publishes an optional status line script (in its &lt;code&gt;openrouter-examples&lt;/code&gt; repo, under &lt;code&gt;claude-code&lt;/code&gt;) that shows session cost in Claude Code. It is configured through the &lt;code&gt;statusLine&lt;/code&gt; entry in &lt;code&gt;~/.claude/settings.json&lt;/code&gt;, and replaces any status line you already have.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prompt logging:&lt;/strong&gt; OpenRouter says prompts are not logged unless you opt in.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Troubleshooting common errors
&lt;/h2&gt;

&lt;p&gt;Most failures come from a leftover credential, a blank-token expansion, or a model that does not behave like Claude. Work through these in order.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Symptom&lt;/th&gt;
&lt;th&gt;Likely cause&lt;/th&gt;
&lt;th&gt;Fix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Auth error or invalid key&lt;/td&gt;
&lt;td&gt;ANTHROPIC_API_KEY is not empty, or the token expanded to blank&lt;/td&gt;
&lt;td&gt;Set ANTHROPIC_API_KEY="", define the key variable first, reload the shell, check /status&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Auth-conflict warning at startup&lt;/td&gt;
&lt;td&gt;A saved claude.ai login plus a gateway credential&lt;/td&gt;
&lt;td&gt;Run /logout, or unset the variable to go back to the login&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Still logged into Anthropic after /logout&lt;/td&gt;
&lt;td&gt;A real ANTHROPIC_API_KEY is exported in your shell profile&lt;/td&gt;
&lt;td&gt;/logout only clears the cached login, not shell variables; blank the variable and reload&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;"Model not found" for a model ID&lt;/td&gt;
&lt;td&gt;Auth conflict, or a wrong model ID&lt;/td&gt;
&lt;td&gt;OpenRouter lists this as usually an auth conflict; fix auth first, then verify the ID in OpenRouter's model list&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;401 errors&lt;/td&gt;
&lt;td&gt;Credential in the wrong variable&lt;/td&gt;
&lt;td&gt;Use ANTHROPIC_AUTH_TOKEN for bearer-style gateways such as OpenRouter&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Asked to log in even though the key works&lt;/td&gt;
&lt;td&gt;The env block is in a project settings file, which applies only after first-run setup and the trust prompt&lt;/td&gt;
&lt;td&gt;Put it in ~/.claude/settings.json or a shell export&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Context stuck at 200k&lt;/td&gt;
&lt;td&gt;Model ID lacks [1m]&lt;/td&gt;
&lt;td&gt;Append [1m] on models that support it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;400 errors about unrecognized fields&lt;/td&gt;
&lt;td&gt;An upstream provider rejects fields Claude Code sends&lt;/td&gt;
&lt;td&gt;Anthropic documents CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1 for this on gateways&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;OpenRouter states Claude Code "is only guaranteed to work with the Anthropic first-party provider." Claude Code is built for Anthropic models, so routing to other providers or non-Anthropic models may not work correctly, including tool calling. Anthropic's own docs say it does not support routing Claude Code to non-Claude models through any gateway. Test any non-Claude model on a small task before relying on it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Fast mode is only available when requests are routed to Anthropic first-party, and &lt;code&gt;/fast&lt;/code&gt; through OpenRouter needs &lt;code&gt;CLAUDE_CODE_SKIP_FAST_MODE_ORG_CHECK=1&lt;/code&gt; according to OpenRouter's guide.&lt;/li&gt;
&lt;li&gt;OpenRouter can fail over between providers. If you need consistency, pin the provider with &lt;code&gt;provider.order&lt;/code&gt; or &lt;code&gt;provider.only&lt;/code&gt;; OpenRouter recommends making Anthropic first-party the top priority.&lt;/li&gt;
&lt;li&gt;Using a gateway disables Anthropic's Remote Control and cloud sessions, which require a claude.ai login.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Using it on a server or in the cloud
&lt;/h2&gt;

&lt;p&gt;On a remote machine the setup is the same, but the main risk is where your key ends up. Treat the OpenRouter key like a password, because anyone who holds it can spend your credits.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Keep it out of git.&lt;/strong&gt; Use &lt;code&gt;~/.claude/settings.json&lt;/code&gt; in your home directory or a shell profile, never a committed project file. If you use &lt;code&gt;.claude/settings.local.json&lt;/code&gt;, add it to your gitignore first.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Restrict file permissions.&lt;/strong&gt; Run &lt;code&gt;chmod 600 ~/.claude/settings.json&lt;/code&gt; so other users on the machine cannot read it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Load from a secret store where you can.&lt;/strong&gt; OpenRouter suggests pulling the key from the OS keychain rather than hardcoding it. For CI, use your platform's secrets.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use settings.json for background processes.&lt;/strong&gt; Anthropic notes that shell-only exports do not reliably reach background agents, while a settings file does.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CI example.&lt;/strong&gt; For the Claude Code GitHub Action, OpenRouter's guide stores the key as a secret, passes it as &lt;code&gt;anthropic_api_key&lt;/code&gt; (only to satisfy the action's launch check), and sets &lt;code&gt;ANTHROPIC_BASE_URL&lt;/code&gt; and &lt;code&gt;ANTHROPIC_AUTH_TOKEN&lt;/code&gt; in the step's &lt;code&gt;env&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Anthropic's hosted cloud sessions will not use it.&lt;/strong&gt; They require a claude.ai login, and gateway variables set in their environment are not applied, so use your own server instead.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rotate on exposure.&lt;/strong&gt; If a key ever lands in a log, a screenshot, or a repo, delete it in OpenRouter and create a new one.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Claude Code Router: what it is and when to use it
&lt;/h2&gt;

&lt;p&gt;Claude Code Router (CCR) is a separate open-source project that runs a local gateway between Claude Code and many model providers. It is not made by Anthropic or OpenRouter. The project's GitHub README (&lt;a href="https://github.com/musistudio/claude-code-router" rel="noopener noreferrer"&gt;musistudio/claude-code-router&lt;/a&gt;, MIT license) describes it as a local control plane for coding agents that gives supported agents one stable local endpoint.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What it does:&lt;/strong&gt; you manage providers, models, accounts, and routing rules in one place. The README lists routing conditions, retries with ordered fallback models, and credential pools with key rotation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Providers:&lt;/strong&gt; it supports OpenAI-style, Anthropic Messages, and Gemini protocols, plus services such as OpenRouter, DeepSeek, Moonshot, and Z.AI, and custom compatible providers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Install:&lt;/strong&gt; the README gives &lt;code&gt;npm install -g @musistudio/claude-code-router&lt;/code&gt; and then &lt;code&gt;ccr ui&lt;/code&gt;, which opens a management UI locally. It requires Node.js 22 or newer, and a desktop app is also offered. Check the README for current steps.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  CCR vs. connecting directly to OpenRouter
&lt;/h3&gt;

&lt;p&gt;Direct OpenRouter is simpler: no extra process, four variables, and one provider's billing. CCR adds a layer you run and maintain, in exchange for flexibility.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Direct to OpenRouter&lt;/th&gt;
&lt;th&gt;Claude Code Router&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Setup&lt;/td&gt;
&lt;td&gt;Environment variables only&lt;/td&gt;
&lt;td&gt;Install and run a local service&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Providers&lt;/td&gt;
&lt;td&gt;Whatever OpenRouter offers&lt;/td&gt;
&lt;td&gt;OpenRouter and many other providers, mixed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Routing rules, fallbacks&lt;/td&gt;
&lt;td&gt;OpenRouter-side provider routing&lt;/td&gt;
&lt;td&gt;Your own rules, configured locally&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Moving parts&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;td&gt;A local gateway that must be running&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Use direct OpenRouter if you want Claude models with minimal setup. Consider CCR if you want to send different tasks to different providers or keep several accounts behind one local endpoint. Remember that Anthropic does not support non-Claude models behind any gateway, so quality and tool-calling behavior are on you to test.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run it on AlwaysAgent
&lt;/h2&gt;

&lt;p&gt;AlwaysAgent gives you a dedicated Linux server with Claude Code and Codex preinstalled, so the "on a server" steps above are already done. Paste your OpenRouter key in settings and the workspace configures the variables for you. Your key stays on your server; we never proxy your requests or add a markup, and your tasks keep running after you close the browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How do I use an OpenRouter API key in Claude Code?
&lt;/h3&gt;

&lt;p&gt;Set ANTHROPIC_BASE_URL to &lt;a href="https://openrouter.ai/api" rel="noopener noreferrer"&gt;https://openrouter.ai/api&lt;/a&gt;, put your OpenRouter key in ANTHROPIC_AUTH_TOKEN, set ANTHROPIC_API_KEY to an empty string, run /logout if you were signed in to Anthropic, then restart claude and check /status.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why does Claude Code say my OpenRouter key is invalid?
&lt;/h3&gt;

&lt;p&gt;Usually ANTHROPIC_API_KEY isn't blank, the key variable was referenced before being defined so the token is empty, or an old Anthropic login is still active. Blank the API key, run /logout, restart, and check /status.&lt;/p&gt;

&lt;h3&gt;
  
  
  Should I use ANTHROPIC_AUTH_TOKEN or ANTHROPIC_API_KEY for OpenRouter?
&lt;/h3&gt;

&lt;p&gt;Use ANTHROPIC_AUTH_TOKEN, which is sent as a Bearer token. OpenRouter's guide says ANTHROPIC_API_KEY must be explicitly empty, because it is sent as x-api-key and treated as a direct Anthropic credential.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I switch models when using OpenRouter in Claude Code?
&lt;/h3&gt;

&lt;p&gt;Use /model in a session or claude --model at startup. To control which OpenRouter model each alias (opus, sonnet, haiku) uses, set the ANTHROPIC_DEFAULT_*_MODEL variables to IDs from the OpenRouter model list.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I use non-Claude models through OpenRouter in Claude Code?
&lt;/h3&gt;

&lt;p&gt;You can point the model aliases at other models, but OpenRouter only guarantees Claude Code with the Anthropic first-party provider, and Anthropic doesn't support non-Claude models behind a gateway. Tool calling may break, so test before relying on it.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I see how much Claude Code is costing on OpenRouter?
&lt;/h3&gt;

&lt;p&gt;Open the Activity dashboard on OpenRouter to see usage, cost, and model per request. OpenRouter also publishes an optional status line script that shows session cost inside Claude Code.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Claude Code Router, and do I need it?
&lt;/h3&gt;

&lt;p&gt;Claude Code Router is a separate open-source local gateway that routes Claude Code to many providers with its own rules. You don't need it to use OpenRouter; connecting directly with environment variables is simpler.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I use Codex with OpenRouter too?
&lt;/h3&gt;

&lt;p&gt;Codex uses its own config: define a custom provider in ~/.codex/config.toml with a base_url and env_key. AlwaysAgent sets up Claude Code and Codex separately.&lt;/p&gt;

</description>
      <category>claude</category>
      <category>ai</category>
      <category>openrouter</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
