Skip to content
  1. Home
  2. Guides
  3. Guide

How to Route Claude Code Through OpenRouter Without Breaking Tool Calls

For a Claude Code router setup through OpenRouter, point Claude Code at https://openrouter.ai/api, put the OpenRouter key in ANTHROPIC_AUTH_TOKEN, explicitly blank ANTHROPIC_API_KEY, and run /logout if an Anthropic login is cached. This is a direct connection: OpenRouter’s Claude Code integration guide says no local proxy is required, but Claude Code is only guaranteed to work with OpenRouter’s Anthropic first-party provider, which it recommends making the top priority. Tool calls also depend on the request sequence: OpenRouter requires the tools parameter on every request, including the request that returns a tool result, so a successful model response alone does not prove that the tool loop is intact.

Set up the Claude Code router

Put the routing variables in the environment from which Claude Code starts. OpenRouter specifies these three values; in a POSIX shell, they can be applied with:

export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
export ANTHROPIC_AUTH_TOKEN="<your OpenRouter API key>"
export ANTHROPIC_API_KEY=""

The empty ANTHROPIC_API_KEY assignment is deliberate. OpenRouter instructs users to provide the OpenRouter key through ANTHROPIC_AUTH_TOKEN and explicitly blank ANTHROPIC_API_KEY to prevent conflicts, as shown in its Claude Code integration guide.

Do not depend on a project-level .env file for this setup. OpenRouter warns that the native Claude Code installer does not read standard .env files. Put the values in the launch environment instead; the documentation does not prescribe one universal startup file for every Claude Code installation method.

The base URL changes the destination, but it does not authenticate the request. Anthropic likewise explains that ANTHROPIC_BASE_URL points Claude Code at a gateway and that setting only that variable, without a gateway credential, does not replace a claude.ai subscription login in its LLM gateway guidance.

If Claude Code previously used an Anthropic account, clear that cached session before checking the new route. Open the Claude Code interface and run:

/logout

OpenRouter specifically says to run /logout once when a previous Anthropic login left a cached session. It then directs you to confirm the connection with /status, covered in the next section.

The direct setup does not require another listening process between Claude Code and OpenRouter. OpenRouter says Claude Code speaks its native protocol directly to the configured endpoint and that its Anthropic Skin handles model mapping while passing through Thinking blocks and native tool use. Those are documented protocol-handling claims, not a guarantee that every provider or MCP feature will behave identically.

Make the Anthropic first-party provider the top priority in OpenRouter’s provider configuration. That priority is not encoded by the three environment variables above, so verify it separately. OpenRouter’s guarantee is limited to the Anthropic first-party provider; keeping that provider first avoids treating model routing as a broad compatibility promise.

A separate local topology

The name Claude Code Router also refers to an open-source local model gateway and control plane. Its project README describes a stable local endpoint with providers, models, accounts, routing rules, and tools managed behind it. That is a different arrangement from pointing Claude Code directly at OpenRouter.

The project’s npm CLI requires Node.js 22 or newer and starts the gateway with a browser-based management interface:

npm install -g @musistudio/claude-code-router
ccr ui

The management interface is at http://127.0.0.1:3458, while the model gateway remains at http://127.0.0.1:3456. Those commands start the local project’s services, but they do not document an OpenRouter-specific provider value or prove that native tool calls work. If you choose this topology, configure the provider and model through the project, then run the connection and tool-loop checks below. The README describes general provider management; it does not make the startup command alone an OpenRouter integration check.

Check the route and tool calls

Return to Claude Code and run:

/status

OpenRouter says this command confirms the connection in its Claude Code integration guide. For the direct setup, also confirm that Claude Code is using https://openrouter.ai/api as its base URL rather than a local gateway address.

A healthy connection does not exercise the complete tool loop. Give Claude Code a harmless, read-only task that requires a tool result, such as reading a file you identify. Confirm that the tool invocation occurs, that its returned data reaches the agent, and that the final response reflects the result. Avoid using a deployment, payment, or other irreversible action as the first tool-call check.

Pay particular attention to the return step. OpenRouter’s Tool & Function Calling documentation says the tools parameter must appear in every request, including the request that returns tool results, so the router can validate the tool schema on each call. Removing tool definitions after the model chooses a tool breaks that contract, even if the first response and the tool execution itself appear successful.

In our own agent setup, we treat connection and completion as separate gates. We may route drafting or noisy searches to another model, but planning, file writes, deploys, and verification stay with the main session. A status response, subagent summary, or exit code is a signal; the task is done only after the result has been read from where a user would see it.

Keep real credential values out of repository files and visible logs. In our setup, logs and screenshots carry variable names such as ANTHROPIC_AUTH_TOKEN, never the credential itself.

Where it breaks

A 401 points to the credential header

The two credential variables are not interchangeable. Anthropic’s gateway connection guide explains that ANTHROPIC_AUTH_TOKEN is sent in the Authorization: Bearer header, while ANTHROPIC_API_KEY is sent in x-api-key. A credential placed in the variable the gateway does not read can produce a 401.

For this OpenRouter route, keep the OpenRouter key in ANTHROPIC_AUTH_TOKEN and leave ANTHROPIC_API_KEY blank. If /status fails with 401, check the active environment and the header mapping before investigating model routing or tool schemas.

A 400 naming unrecognized fields

Some gateways forward requests to an upstream that rejects fields Claude Code sends to Anthropic-format endpoints. Anthropic documents CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1 as the response to errors naming unrecognized fields; it suppresses most pre-release fields according to the gateway connection guide.

export CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1

Use that setting for the documented field-mismatch case rather than treating it as a universal repair for every 400 response. An authentication failure, malformed tool schema, and rejected request field are different problems.

MCP tool search is disabled by the fallback

Claude Code normally defers MCP tools and discovers them on demand. Anthropic’s MCP documentation says Claude Code disables tool search when ANTHROPIC_BASE_URL points to a non-first-party host because most proxies do not forward tool_reference blocks. ENABLE_TOOL_SEARCH can be set explicitly to override that fallback.

Do not treat that override as a generic tool repair. OpenRouter’s pass-through of native tool use does not, by itself, establish that a particular MCP deployment forwards every tool_reference block. If your workflow depends on deferred MCP discovery, check the current MCP guidance and confirm that the selected route supports the block before forcing the fallback off.

The selected provider crosses the support boundary

OpenRouter says Claude Code is optimized for Anthropic models and may not work correctly with other providers. Anthropic also states that it does not support routing Claude Code to non-Claude models through a gateway, even though a gateway exposing a supported API format can otherwise work with Claude Code. These limits appear in OpenRouter’s integration guidance and Anthropic’s gateway overview.

Use OpenRouter as the gateway without treating it as approval to replace Claude with an unrelated model. If a non-Claude route starts dropping tools, rejecting fields, or producing an incomplete agent loop, the provider boundary is the first place to check.

Context compaction can resemble a failed tool loop

Claude Code assumes a 200k context window unless the model name ends in [1m]. OpenRouter says that when the configured model supports a 1M context window, appending [1m] to its slug prevents sessions from compacting earlier than required. This setting is described in the Claude Code integration guide.

A long sequence of file reads and tool results can consume context quickly. Premature compaction may make an agent appear to have lost tool state even though the request format remains valid. Check the model suffix before treating missing context as a broken tool definition.

Finally, the Claude Code integration documentation does not state OpenRouter account or provider rate limits. Check current OpenRouter documentation before interpreting capacity controls as tool-protocol failures, and do not infer a quota from this setup.

Sources