OPENROUTER API
ОДИН КЛЮЧ
400+ МОДЕЛЕЙ.
TL;DR: Отдельные API keys для OpenAI, Anthropic, Google, DeepSeek = разные SDK, billing dashboards, rate limits и custom failover в пяти репозиториях. OpenRouter — unified LLM gateway: один OpenAI-compatible endpoint, 70+ провайдеров, 400+ моделей, один ключ. Это hardcore-руководство: routing layers, comparison table vs direct API, 5 причин миграции, anti-patterns, 3-step setup, production code (curl/Python/Node/OpenAI SDK), streaming + fallback config, pricing (5,5%, BYOK), FAQ.
Спеки за 30 секунд
| Определение | Unified LLM gateway: 70+ providers, 400+ models, OpenAI-compatible /v1 |
| Endpoint | https://openrouter.ai/api/v1/chat/completions |
| Pricing | Zero token markup; 5,5% на credit top-up; 25+ free models |
| Free tier | 50 req/day без покупки; 1000/day после $10 top-up |
| Latency overhead | 10–80 ms routing vs direct vendor API |
1. Что такое OpenRouter
OpenRouter — unified LLM API gateway. Вместо регистрации у OpenAI, Anthropic, Google, Meta, Mistral и DeepSeek по отдельности вы шлёте все запросы на один OpenAI-compatible endpoint. OpenRouter обрабатывает auth, provider selection, failover и billing.
Core specs:
- Endpoint:
https://openrouter.ai/api/v1— drop-in replacement для OpenAI base URL - Auth: один API key в header
Authorization: Bearer - Model naming: формат
provider/model-id— напр.openai/gpt-4o,anthropic/claude-sonnet-4,google/gemini-2.5-pro - Compatibility: curl, Python requests, Node fetch, official OpenAI SDK — меняете только
base_urlиmodel
Routing mechanism
| Layer | Функция | Пример |
|---|---|---|
| Model routing | Вы задаёте model ID; OpenRouter выбирает лучший доступный provider | deepseek/deepseek-chat |
| Provider routing | Одна модель на нескольких хостах; routing по price/latency/uptime | DeepSeek via Fireworks vs DeepSeek direct |
| Fallback | Auto-retry на backup model при 429/5xx | Primary Claude → fallback GPT-4o |
| Free models | 25+ models @ $0; rate-limited по account tier | meta-llama/llama-3.3-70b-instruct:free |
2. OpenRouter vs Direct API: comparison table
| Dimension | OpenRouter | Direct vendor API |
|---|---|---|
| API keys | Один key на всех провайдеров | Отдельный key на вендора |
| SDK changes | Только base_url | Vendor-specific SDKs/endpoints |
| Model switching | Одна строка model string | Новый account, billing, rate limits |
| Failover | Built-in fallback models/providers | Custom retry logic |
| Token pricing | Provider list price, zero markup | Provider list price |
| Platform fee | 5,5% на credit purchases | None |
| Latency | +10–80 ms routing overhead | Lowest (direct to vendor) |
| Vendor features | Subset (no Assistants API, limited caching) | Full feature set |
| Compliance | Third-party US routing | Direct vendor DPA/BAA |
| Free models | 25+ models, tiered daily limits | Vendor-specific free tiers |
3. Пять причин перейти на OpenRouter
- One key, 400+ models. Prototype на GPT-4o, ship на DeepSeek V4 Flash, escalate на Claude Opus — без re-auth и новых billing accounts.
- OpenAI SDK drop-in. Codebases с
openai.chat.completions.create()работают после swap двух строк:base_url+model. - Built-in failover. Agent loops постоянно ловят 429. Provider routing + model fallback сжимают custom retry с сотен строк до JSON config block.
- Real usage data. OpenRouter публикует weekly token/dollar rankings — production ground truth, не benchmark noise. См. июньский rankings breakdown.
- Free tier для prototyping. 25+ free models, 50 req/day (1000/day после $10 credit) — validate agent workflows до paid inference.
4. Когда НЕ использовать OpenRouter
Gateway — не silver bullet. Skip when:
- Latency-sensitive realtime: 10–80 ms overhead критичен для voice, live coding completions, TTFT <100 ms.
- Very high token volume: при миллионах tokens/day 5,5% credit fee material — direct contracts/reserved capacity дешевле.
- Strict compliance: HIPAA, data residency, запрет third-party routing — direct API relationships.
- Vendor-specific features: OpenAI Assistants, Anthropic prompt caching с guaranteed hit rates, Google Vertex grounding — native SDK, не gateway abstraction.
5. Setup: получить OpenRouter API key
Step 1 — Register
openrouter.ai — sign in через Google/GitHub/email. Credit card не нужен для free-tier models.
Step 2 — Create API key
Dashboard → Keys → Create Key, name (напр. prod-agent), copy immediately. Store в env var — never commit to Git.
Step 3 — First request
Verify key minimal curl call. Expected: JSON с choices[0].message.content.
6. Code examples
6.1 curl
6.2 Python (requests)
6.3 Python (OpenAI SDK drop-in)
6.4 Node.js (OpenAI SDK)
6.5 Streaming
6.6 Fallback configuration
Array models instructs OpenRouter try each model in order if previous returns error/rate limit. Simplest production-grade failover для agent loops.
6.7 List available models
7. Streaming, fallback, cost control
Streaming: "stream": true в request body. OpenRouter passthrough SSE chunks в OpenAI format. Use для chat UI и agent tool-call loops где TTFT critical.
Fallback: models array для model-level failover. Provider-level routing — OpenRouter auto-select cheapest/fastest unless pin via provider preferences в dashboard.
Cost control: spend limits в dashboard. Cheap models (DeepSeek V4 Flash, Gemini Flash) для drafts; Claude Opus/GPT-4o для escalation. Monitor weekly spend в Activity tab.
8. Pricing: free tier, 5,5% fee, BYOK
| Tier | Details |
|---|---|
| Free models | 25+ models @ $0/token; 50 req/day без purchase; 1000/day после $10 top-up |
| Paid models | Provider list price, zero per-token markup |
| Platform fee | 5,5% на credit purchases (не на token usage) |
| BYOK | Bring Your Own Key; 1M free routed requests/month, then small overage fee |
Example: $100 credit purchase = $105,50 total. $100 покупает tokens по provider rates. При 10M tokens/day 5,5% fee становится material — evaluate direct contracts.
9. FAQ
Q: OpenRouter бесплатный?
A: 25+ free models, 50 req/day без charge. После $10 credit: 1000/day. Paid models — provider list price.
Q: OpenRouter markup на tokens?
A: Zero per-token markup. 5,5% только на credit top-ups. BYOK: 1M free routed requests/month.
Q: OpenRouter safe для production?
A: Widely used в agent stacks. Keys server-side, spend caps, BYOK/direct API для compliance workloads.
Q: OpenRouter vs OpenAI API?
A: OpenRouter = multi-provider gateway, one endpoint, 400+ models. OpenAI API = только OpenAI models + native features.
Q: OpenAI Python SDK compatible?
A: Да. base_url="https://openrouter.ai/api/v1" + OpenRouter key. No other changes.
Q: Когда skip OpenRouter?
A: Sub-10ms latency, very high volume (5,5% fee), strict compliance, vendor features вроде Assistants API.
10. Заключение: OpenRouter dev на Mac — offload agent stress tests
OpenRouter integration работает на любой OS. Но Cursor + Claude Code + OpenClaw с multi-model fallback chains превращают primary Mac в bottleneck: Docker sandboxes, agent loops, long-context batch jobs конкурируют за unified memory и bandwidth GPU.
Practical split: Cursor review + light API calls на laptop; offload agent stress tests, OpenRouter routing probes и 24/7 Gateway cron на MACGPU remote Mac mini M4 node. SSH isolation, on-demand rental, параллельная Metal-native MLX validation с полным GPU throughput без throttling на вашем основном Mac.