OpenRouter教程:一个API Key用所有LLM

2026-08-01 约 5 分钟阅读

不用再为每个模型服务商维护单独的API Key。

OpenRouter Tutorial 2026: One API Key for Every LLM

OpenRouter is an API gateway that gives you access to hundreds of models - Anthropic, OpenAI, Google, DeepSeek, Qwen, Llama, and more - through a single OpenAI-compatible endpoint and one API key. In 2026 it is the standard way developers compare and route models without managing a dozen accounts.

Why Developers Use It

Getting Started

# 1. Sign up at openrouter.ai, create a key (sk-or-v1-...)

# 2. Standard OpenAI SDK
from openai import OpenAI

client = OpenAI(
    api_key="sk-or-v1-...",
    base_url="https://openrouter.ai/api/v1"
)

resp = client.chat.completions.create(
    model="anthropic/claude-sonnet-4",
    messages=[{"role": "user", "content": "Hello!"}]
)
print(resp.choices[0].message.content)

Model IDs You Should Know

Model OpenRouter ID Cost class
Claude Sonnet 4 anthropic/claude-sonnet-4 Mid
GPT-5 mini openai/gpt-5-mini Low
DeepSeek V3 deepseek/deepseek-chat Very low
Qwen 2.5 72B qwen/qwen-2.5-72b-instruct Low
Llama 3.3 70B meta-llama/llama-3.3-70b-instruct Low
Free models various (:-free suffix) $0

Advanced Features

Automatic routing:

"model": "openrouter/auto"

Routes each request to the best model for the prompt - useful for cost optimization.

Fallbacks:

"model": ["anthropic/claude-sonnet-4", "openai/gpt-5-mini"]

If the first fails, the second is used.

Cost tracking: The response includes usage.cost - log it to see what you spend per request.

FAQ

Is OpenRouter cheaper than direct APIs? Slightly more per token (small gateway fee) but often cheaper in practice because you can pick the cheapest adequate model per task.

Is my data private? OpenRouter is a pass-through; your data goes to the model provider. Enterprise tier offers zero-retention with some providers.

Free models on OpenRouter? Yes - several models are available free (rate-limited) for experimentation.

相关文章
2026-06-29
高收益主线龙头策略——不花钱的数据,也能抓到龙头
2026-06-29
你笔记本里,藏着一个AI
2026-07-14
免费AI编程助手 2026 配置指南:VS Code 5分钟装 Continue、Copilot、Windsurf

💬 评论 (0)

暂无评论,来说两句吧~

登录后评论