本文作者:程序员晚枫 | AI编程布道者 | 专注AI工具测评与教学

全网40万+粉丝,6年Python开发经验,开源项目python-office作者

💡 想系统了解各大厂商 Coding Plan? 👉 点击查看 Coding Plan 对比汇总

大家好,这里是程序员晚枫。

今天带来 OpenRouter Coding Plan 的实战教程,手把手教你用统一接口访问全球所有主流大模型。

一、基础配置

第一步:注册并获取 API Key

  1. 访问 👉 OpenRouter Coding Plan 详情
  2. 注册 OpenRouter 账号
  3. 充值(按量付费)
  4. 获取 API Key

第二步:环境准备

1
pip install openai

第三步:基础调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from openai import OpenAI

client = OpenAI(
api_key="你的OpenRouter API Key",
base_url="https://openrouter.ai/api/v1"
)

# 调用任意模型
response = client.chat.completions.create(
model="anthropic/claude-3-opus", # 改这里就能换模型
messages=[{"role": "user", "content": "帮我写一个Python快速排序"}]
)

print(response.choices[0].message.content)

二、模型切换实战

OpenRouter 的强大之处在于可以轻松切换模型。

示例:同一个任务,不同模型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
models = [
"openai/gpt-4-turbo",
"anthropic/claude-3-opus",
"deepseek/deepseek-chat"
]

for model in models:
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": "帮我写一个Python快速排序"}]
)
print(f"Model: {model}")
print(f"Response: {response.choices[0].message.content}")
print("---")

三、费用控制

查看可用模型和价格

1
2
3
4
# 查看所有可用模型
response = client.models.list()
for model in response.data:
print(model.id)

查看实时价格

OpenRouter 按 token 计费,不同模型价格不同:

  • GPT-4o:较贵
  • Claude 3.5:较贵
  • DeepSeek:便宜
  • Llama 3:免费

四、进阶技巧

1. 自动选择最优模型

1
2
3
4
5
6
7
8
9
10
11
12
def call_model(task_type):
if task_type == "code":
return "anthropic/claude-3-opus"
elif task_type == "creative":
return "openai/gpt-4-turbo"
else:
return "deepseek/deepseek-chat"

response = client.chat.completions.create(
model=call_model("code"),
messages=[{"role": "user", "content": "帮我写代码"}]
)

2. 错误重试

1
2
3
4
5
6
7
8
from tenacity import retry, stop_after_attempt

@retry(stop=stop_after_attempt(3))
def call_with_retry(model, messages):
return client.chat.completions.create(
model=model,
messages=messages
)

五、常见问题

Q1:需要科学上网吗?

OpenRouter 是国外服务,需要稳定的环境才能访问。

Q2:价格贵吗?

按量付费,不同模型价格不同。DeepSeek 和 Llama 比较便宜,GPT-4 和 Claude 比较贵。

Q3:稳定性如何?

OpenRouter 本身稳定性不错,但依赖底层服务商(如 OpenAI、Anthropic)的稳定性。


相关阅读


📢 更多 Coding Plan 对比:👉 点击查看所有厂商的 Coding Plan


作者:程序员晚枫,全网同名,专注 AI 工具测评与 Python 自动化办公教学。


🎓 AI Programming Course

Want to learn AI programming systematically? Check out CoderWanFeng's AI Programming Course!


🤖 Developer Productivity Tools

👉 Want to try MiniMax Token Plan? Click here for 10% off

💡 Pay-per-use pricing — super cost-effective! Think of it like a farmers market: buy a ticket, and all the veggies are free. Pay based on actual usage, no limits, no monthly fees. Perfect for developers!