你正深陷一次重构之中。Claude Code 已经跨十几个文件拉取了上下文,规划好了 diff,并开始执行。然后会话在完成到 70% 时因速率限制而中断。
将 Claude Code 通过 OpenRouter 路由,就是让该会话保持存活的办法。OpenRouter 作为可靠性与管理层,位于 Claude Code 和 Anthropic 的 API 之间。它增加了提供商故障转移、预算控制和使用情况可见性,而且无需运行本地代理。设置只需三个环境变量。本文涵盖该设置、模型路由、Fast Mode 以及成本计算。
团队场景同样实用。多名开发者同时运行 Claude Code,意味着多个 Anthropic 账号、没有共享的支出视图,也没有按开发者设置的上限,直到账单到来。一个 OpenRouter 密钥就能统管这一切:共享计费、按密钥限制,以及一个显示每次会话成本的 Activity 仪表盘。
三步连接 Claude Code
整个设置只需三个环境变量,记录在 Claude Code 集成指南中,无需代理、无需 Docker,也无需运行本地端口。
# Add to ~/.zshrc or ~/.bashrc
export OPENROUTER_API_KEY="<your-openrouter-api-key>"
export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
export ANTHROPIC_API_KEY="" # must be explicitly empty 有三点需要做对。基础 URL 是 https://openrouter.ai/api。认证 token 是你的 OpenRouter 密钥,它以 sk-or- 开头。而 ANTHROPIC_API_KEY 必须是空字符串,而不是未设置,否则 Claude Code 可能会回退到直接向 Anthropic 进行认证。
想把它限定到单个项目?把同样的三个值放到env块中,位于.claude/settings.local.json项目根目录下。不要使用普通的.env文件,因为原生安装程序不会读取它。
如果你之前使用 Anthropic 账号登录过 Claude Code,请先运行一次 /logout 并重新启动,否则缓存的登录信息会覆盖你的变量,导致出现令人困惑的模型未找到错误。用 /status 确认切换:
> /status
Auth token: ANTHROPIC_AUTH_TOKEN
Anthropic base URL: https://openrouter.ai/api 你的请求也应该会在几秒内显示在活动仪表盘中。
Anthropic Skin 如何在没有代理的情况下工作
OpenRouter 提供了一个与 Anthropic Messages API 兼容的端点,它称之为 Anthropic Skin。Claude Code 直接以原生协议与 OpenRouter 通信,而 Skin 负责处理模型映射,并将高级功能原样透传。
这意味着 Thinking 块、原生工具调用、流式传输以及多轮上下文都能像直接对接 Anthropic 时一样正常工作。早期的方案依赖 claude-code-router 这类本地代理来转换请求,这意味着需要一个 Node.js 运行时、一个本地端口,以及一个需要持续维护健康的转换层。原生路由为 Anthropic 工作流省去了这整个层级。
在底层,OpenRouter 会在为某个模型提供服务的多个 Anthropic 供应商之间进行负载均衡。如果它首先尝试的那家对你限流,它就会把同一个模型路由到另一家为其提供服务的供应商,而你只需为最终成功的那次调用付费。对于一个跨多次调用保持状态的多步骤智能体任务来说,这种发生在 Claude Code 之下的故障转移,决定了任务究竟是彻底完成,还是只应用了一半的编辑。
将每类任务路由到合适的模型
Claude Code 会把工作拆分到多个模型槽位上。你可以逐一覆盖,让每个槽位指向 OpenRouter 上的某个特定模型。~author/model-latest 别名始终解析到某个模型族中的最新版本,因此不会过时:
export ANTHROPIC_DEFAULT_OPUS_MODEL="~anthropic/claude-opus-latest"
export ANTHROPIC_DEFAULT_SONNET_MODEL="~anthropic/claude-sonnet-latest"
export ANTHROPIC_DEFAULT_HAIKU_MODEL="~anthropic/claude-haiku-latest"
export CLAUDE_CODE_SUBAGENT_MODEL="~anthropic/claude-opus-latest" 一个合理的划分:Opus 负责架构和深度推理,Sonnet 负责日常编码,Haiku 负责快速转换和分类。把这些连同 base URL 和 token 一起放进同一个 shell 配置文件或项目设置文件中。
不过,模型仍要保留 Anthropic 的。Claude Code 是围绕 Anthropic 的请求语义构建的,该集成只保证与 Anthropic 第一方供应商兼容。为获得最大兼容性,请将 Anthropic 1P 设为你优先级最高的供应商。
Opus 会话的 Fast Mode
Anthropic 的 Fast Mode 以更高定价提供最高 2.5 倍的输出速度,仅由 Anthropic 第一方供应商提供。它适用于 Claude Opus 4.6、4.7 和 4.8,不适用于其他任何模型。
Claude Code 有一个内置的 /fast 开关。开启后,Claude Code 会在配置的 Opus 模型之外一并发送 speed: "fast",OpenRouter 会重新路由到匹配的 -fast 变体。要使用它,只需设置一个变量:
export CLAUDE_CODE_SKIP_FAST_MODE_ORG_CHECK=1 这需要 Claude Code v2.1.96 或更新版本。把 speed: "fast" 发送给不支持它的模型时,OpenRouter 会直接丢弃该参数,因此请求会以标准速度和价格运行。在交互式会话和实时调试中可以使用它,这些场景下延迟是你最能感受到的。在生产环境开启之前,请查看 定价页面了解当前的 Opus 费率。
费用是多少,以及免费额度
OpenRouter 不对 token 定价加价。你支付的每 token 费率与提供商收取的相同,显示在 模型目录中,而购买额度会收取 5.5% 的手续费,最低 $0.80。
相对于实际用量,这笔费用很小。以每月 10M tokens 使用 Claude Sonnet 4.5、输入/输出按 80/20 分配为例。按每百万输入 $3、每百万输出 $15 计算,直接的 token 成本约为 $54,5.5% 的额度手续费大约再加 $3。作为回报,你能获得故障转移、按密钥的预算上限,以及跨团队的统一账单视图。
还有一个免费层级可供试用。免费模型每天最多可运行 50 次请求,充值 10 美元额度后每天可提升至 1,000 次。它们的上下文窗口比付费的 Anthropic 模型更小,因此适合用来学习 Claude Code 的工作流程,而不适合用于生产会话。在活动仪表盘中实时查看花费,这是发现某个会话被路由到比任务所需更重的模型的最快方式。
将其接入 CI 和你的终端
同样的路由方式在本地 shell 之外同样适用。
对于 CI,官方的Claude Code GitHub Action需要两处改动:通过 anthropic_api_key 传入你的 OpenRouter 密钥,并在该步骤的 env 中设置 base URL。
- name: Run Claude Code
uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: ${{ secrets.OPENROUTER_API_KEY }}
env:
ANTHROPIC_BASE_URL: https://openrouter.ai/api 要在终端中实时查看成本,openrouter-examples 仓库提供了一个状态栏脚本,可显示提供商、模型、运行成本和缓存折扣。将你的 ~/.claude/settings.json 指向它:
{
"statusLine": {
"type": "command",
"command": "/path/to/statusline.sh"
}
} 要在 Claude Code 之上构建智能体?Anthropic Agent SDK使用 Claude Code 运行时,因此同样的三个环境变量即可将其请求路由到 OpenRouter,无需额外配置。
常见问题
使用 OpenRouter 配合 Claude Code 需要 Anthropic 订阅吗?
不会。请求通过你的 OpenRouter 额度路由,因此你需要一个 OpenRouter 账户和一个 API key,而不是 Anthropic 套餐。如果你之前使用 Anthropic 账户登录过 Claude Code,请运行 /logout 一次,以清除缓存的会话,然后再切换。
我可以通过 OpenRouter 在 Claude Code 中使用非 Anthropic 模型吗?
该原生集成是为 Anthropic 模型构建的,仅保证与 Anthropic 第一方提供商配合使用。Claude Code 期望 Anthropic 的请求语义,因此通过原生端点不支持非 Anthropic 模型。
通过 OpenRouter 使用 Claude Code 需要多少费用?
你需支付提供商的每 token 费率,外加额度购买时 5.5% 的手续费,最低为 $0.80。推理本身仍按提供商费率计费。活动仪表盘会实时显示每次会话的精确费用。
OpenRouter 会记录我的源代码吗?
You’re deep into a refactor. Claude Code has pulled context across a dozen files, planned the diff, and started executing. Then the session stops on a rate limit, 70% of the way through.
Routing Claude Code through OpenRouter is how you keep that session alive. OpenRouter sits between Claude Code and Anthropic’s API as a reliability and management layer. It adds provider failover, budget controls, and usage visibility, and it does that with no local proxy to run. The setup is three environment variables. This covers that setup, model routing, Fast Mode, and the cost math.
The team case is just as practical. Several developers running Claude Code at once means several Anthropic accounts, no shared spend view, and no per-developer cap until the invoice lands. One OpenRouter key fronts all of it: shared billing, per-key limits, and an Activity dashboard that shows cost per session.
Connect Claude Code in three steps
The whole setup is three environment variables, documented in the Claude Code integration guide, with no proxy, no Docker, and no local port to run.
# Add to ~/.zshrc or ~/.bashrc
export OPENROUTER_API_KEY="<your-openrouter-api-key>"
export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
export ANTHROPIC_API_KEY="" # must be explicitly empty Three things to get right. The base URL is https://openrouter.ai/api. The auth token is your OpenRouter key, which starts with sk-or-. And ANTHROPIC_API_KEY has to be an empty string, not unset, or Claude Code can fall back to authenticating against Anthropic directly.
Prefer to scope it to one project? Put the same three values under an env block in .claude/settings.local.json at the project root. Don’t use a plain .env file, since the native installer doesn’t read one.
If you logged in to Claude Code with an Anthropic account before, run /logout once and relaunch, otherwise the cached login overrides your variables and you get confusing model-not-found errors. Confirm the switch with /status:
> /status
Auth token: ANTHROPIC_AUTH_TOKEN
Anthropic base URL: https://openrouter.ai/api Your request should also show up in the Activity dashboard within seconds.
How the Anthropic Skin works without a proxy
OpenRouter exposes an endpoint compatible with the Anthropic Messages API, which it calls the Anthropic Skin. Claude Code speaks its native protocol straight to OpenRouter, and the Skin handles model mapping and passes advanced features through untouched.
That means Thinking blocks, native tool use, streaming, and multi-turn context all work as they do against Anthropic directly. Older setups leaned on a local proxy like claude-code-router to translate requests, which meant a Node.js runtime, a local port, and a translation layer to keep healthy. Native routing drops that whole tier for Anthropic workflows.
Underneath, OpenRouter load-balances across the Anthropic providers serving a model. If the one it tries first is rate-limiting you, it routes the same model to another provider that serves it, and you pay only for the call that lands. For a multi-step agentic task that holds state across many calls, that failover happening below Claude Code is the difference between a finished task and a half-applied edit.
Route each task class to the right model
Claude Code splits work across model slots. Override each one to point at a specific model on OpenRouter. The ~author/model-latest aliases always resolve to the newest version in a family, so they don’t go stale:
export ANTHROPIC_DEFAULT_OPUS_MODEL="~anthropic/claude-opus-latest"
export ANTHROPIC_DEFAULT_SONNET_MODEL="~anthropic/claude-sonnet-latest"
export ANTHROPIC_DEFAULT_HAIKU_MODEL="~anthropic/claude-haiku-latest"
export CLAUDE_CODE_SUBAGENT_MODEL="~anthropic/claude-opus-latest" A reasonable split: Opus for architecture and deep reasoning, Sonnet for everyday coding, Haiku for quick transformations and classification. Put these in the same shell profile or project settings file as the base URL and token.
Keep the models Anthropic, though. Claude Code is built around Anthropic request semantics, and the integration is only guaranteed to work with the Anthropic first-party provider. For maximum compatibility, set Anthropic 1P as your top-priority provider.
Fast Mode for Opus sessions
Anthropic’s Fast Mode delivers up to 2.5x faster output at premium pricing, served only by the Anthropic first-party provider. It applies to Claude Opus 4.6, 4.7, and 4.8, and no other models.
Claude Code has a built-in /fast toggle. When it’s on, Claude Code sends speed: "fast" alongside the configured Opus model, and OpenRouter reroutes to the matching -fast variant. To use it, set one variable:
export CLAUDE_CODE_SKIP_FAST_MODE_ORG_CHECK=1 This needs Claude Code v2.1.96 or newer. Send speed: "fast" to a model that doesn’t support it and OpenRouter just drops the parameter, so the request runs at standard speed and price. Reach for it on interactive sessions and live debugging, where latency is the thing you feel. Check the pricing page for current Opus rates before turning it on for production.
What it costs, and the free tier
OpenRouter doesn’t mark up token pricing. You pay the same per-token rate the provider charges, shown in the model catalog, and credit purchases carry a 5.5% fee with a $0.80 minimum.
The fee is small against real usage. Take 10M tokens a month on Claude Sonnet 4.5 at an 80/20 input/output split. At $3 per million input and $15 per million output, that’s about $54 in direct token cost, and the 5.5% credit fee adds roughly $3. For that you get failover, per-key budget caps, and one billing view across the team.
There’s also a free tier for trying things. Free models run at up to 50 requests a day, rising to 1,000 a day once you add $10 in credits. They carry smaller context windows than paid Anthropic models, so they’re good for learning Claude Code’s workflow, not for production sessions. Watch spend live in the Activity dashboard, the fastest way to catch a session routed to a heavier model than the task needs.
Wire it into CI and your terminal
The same routing works beyond your local shell.
For CI, the official Claude Code GitHub Action needs two changes: pass your OpenRouter key through anthropic_api_key, and set the base URL in the step’s env.
- name: Run Claude Code
uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: ${{ secrets.OPENROUTER_API_KEY }}
env:
ANTHROPIC_BASE_URL: https://openrouter.ai/api For live cost in your terminal, the openrouter-examples repository ships a statusline script that shows provider, model, running cost, and cache discount. Point your ~/.claude/settings.json at it:
{
"statusLine": {
"type": "command",
"command": "/path/to/statusline.sh"
}
} Building agents on top of Claude Code? The Anthropic Agent SDK uses the Claude Code runtime, so the same three environment variables route its requests through OpenRouter with no extra config.
Frequently asked questions
Do I need an Anthropic subscription to use Claude Code with OpenRouter?
No. Requests route through your OpenRouter credits, so you need an OpenRouter account and an API key, not an Anthropic plan. If you previously logged in to Claude Code with an Anthropic account, run /logout once to clear the cached session before switching.
Can I use non-Anthropic models with Claude Code through OpenRouter?
The native integration is built for Anthropic models and is only guaranteed to work with the Anthropic first-party provider. Claude Code expects Anthropic request semantics, so non-Anthropic models aren’t supported through the native endpoint.
How much does Claude Code cost through OpenRouter?
You pay the provider’s per-token rate plus a 5.5% fee on credit purchases, with a $0.80 minimum. Inference itself stays at provider rates. The Activity dashboard shows exact per-session cost in real time.