用 OpenRouter 免费跑 Claude Code,省 $200/月订阅费
> "I've been using Claude Code through OpenRouter for 3 months. Zero cost. Same functionality as the $200/month plan. The setup is a bit hacky but once it's running, you forget you're not paying." — YouTube "Thetips4you" tutorial (92K views), 2026
引言
Claude Code 是 Anthropic 推出的命令行 AI 编程工具,本来是 Claude Pro($20/月)和 Claude Max($200/月)用户的专属功能。但 2026 年,开发者社区发现了一个巧妙的方法:通过 OpenRouter 免费模型路由,可以配置 Claude Code 使用免费模型,绕过付费墙。
YouTube 博主 Thetips4you 的一个教程(9.2 万播放)详细展示了这个方法。Reddit r/ClaudeAI 上也有大量用户验证了可行性。核心原理:Claude Code 支持自定义 API 端点,我们可以指向 OpenRouter 的免费模型。
---
具体步骤:配置 Claude Code + OpenRouter
第一步:准备条件
- 一个 OpenRouter 账号(注册免费,无需信用卡)
- 一个 Claude Code 客户端(2026 年已开源,可直接从 GitHub 下载)
- Node.js 18+ 环境
第二步:了解模型路线
2026 年通过 OpenRouter 可用的 Claude 相关模型:
| 模型 | OpenRouter 模型 ID | 免费状态 |
|---|---|---|
| Claude Sonnet 4.5 | anthropic/claude-sonnet-4.5 | 有限免费(200 RPD) |
| Claude Haiku 3.5 | anthropic/claude-3.5-haiku | 有限免费 |
| Claude Opus 4 | anthropic/claude-opus-4 | 付费(不计入免费) |
推荐使用 Sonnet 4.5,它在 OpenRouter 的免费配额内。
第三步:获取 OpenRouter API Key
- 登录 [openrouter.ai](https://openrouter.ai)
- 进入 Keys 页面,生成新 Key
- 复制 Key(格式:`sk-or-v1-xxx`)
第四步:配置 Claude Code
Claude Code 支持通过环境变量或配置文件设置 API 端点:
# 方法一:环境变量
export ANTHROPIC_BASE_URL="https://openrouter.ai/api/v1"
export ANTHROPIC_API_KEY="sk-or-v1-xxx"
# 启动 Claude Code
claude code
或者创建配置文件 ~/.claude/claude_code.json:
{
"apiKey": "sk-or-v1-xxx",
"apiUrl": "https://openrouter.ai/api/v1",
"model": "anthropic/claude-sonnet-4.5"
}
第五步:验证连接
启动 Claude Code 后,测试一个简单的命令:
# 在 Claude Code 中
> 用Python写一个斐波那契数列
如果正常工作,说明 Claude Code 通过 OpenRouter 成功连接到 Claude Sonnet 4.5。
第六步:Python 脚本方式直接调用
如果你不想在终端里交互使用,也可以通过 Python 脚本实现类似功能:
from openai import OpenAI
# 通过 OpenRouter 调用 Claude
client = OpenAI(
api_key="sk-or-v1-xxx",
base_url="https://openrouter.ai/api/v1"
)
def claude_code(prompt, system_prompt="你是一个专业的编程助手"):
response = client.chat.completions.create(
model="anthropic/claude-sonnet-4.5",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": prompt}
],
max_tokens=4096,
temperature=0.3 # 编程推荐低温度
)
return response.choices[0].message.content
# 使用示例
code = claude_code("写一个React Hook来处理表单验证")
print(code)
第七步:自动 Fallback(高级)
如果 Claude 模型达到 OpenRouter 的 200 RPD 限制,可以设置 fallback:
def claude_code_with_fallback(prompt):
# 先尝试 Claude
models = [
"anthropic/claude-sonnet-4.5",
"meta-llama/llama-3.3-70b-instruct:free",
"deepseek/deepseek-v4-flash"
]
for model in models:
try:
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}],
max_tokens=4096,
timeout=30
)
return response.choices[0].message.content
except Exception as e:
print(f"{model} failed: {e}")
continue
return "All models exhausted"
---
curl 快速测试
curl -X POST "https://openrouter.ai/api/v1/chat/completions" \
-H "Authorization: Bearer sk-or-v1-xxx" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-sonnet-4.5",
"messages": [
{"role": "user", "content": "写一个Python装饰器"}
]
}'
---
够干什么?实际场景建议
| 场景 | 评价 | 说明 |
|---|---|---|
| 日常编程辅助 | ✅ 完全够用 | 200 RPD 够日常使用 |
| 代码审查 | ✅ 推荐 | Claude 的代码理解能力强 |
| 批量代码生成 | ⚠️ 注意配额 | 200 RPD 可能不够 |
| 学习 AI 编程 | ✅ 最佳选择 | 零成本入门 |
| 替代 $200/月 Max | ✅ 省大钱 | 功能完全一样 |
节省对比:
- Claude Pro 订阅:$20/月 → 免费
- Claude Max 订阅:$200/月 → 免费
- 使用 OpenRouter 免费层:$0
唯一成本:如果超出 OpenRouter 免费层 200 RPD 配额,可以按需付费(极低费率)。
---
2026 年现状与变化
- Claude Code 开源:2026 年初 Anthropic 将 Claude Code 开源,让这种自定义配置成为可能
- OpenRouter 免费配额调整:2026 年从 500 RPD 降到 200 RPD
- Sonnet 4.5 模型更新:最新 Sonnet 在代码生成上有显著提升
- 社区配置脚本:GitHub 上出现一键配置脚本,简化设置过程
- Anthropic 官方态度:目前 Anthropic 未明确阻止这种用法,不保证长期可用
---
交叉引用
- OpenRouter 免费模型路由 — 核心依赖
- Anthropic Claude 免费 $5 额度 — 官方额度
- Claude 免费用的 12 种方法 — 更多入口
- 免费 API 的坑与避雷指南 — 注意稳定性
- 全部薅一遍:20 家免费 API 额度叠加 — 组合策略
---
数据来源
- YouTube "Thetips4you", "Run Claude Code for FREE with OpenRouter" (92K views)
- Reddit r/ClaudeAI, "Claude Code + OpenRouter setup guide"
- OpenRouter 官方文档
- Claude Code GitHub 仓库
- 个人实践,2026 年 5 月