OPENROUTER API
ОДИН КЛЮЧ
400+ МОДЕЛЕЙ.

OpenRouter unified API gateway GPT Claude Gemini 400 LLM моделей

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
Endpointhttps://openrouter.ai/api/v1/chat/completions
PricingZero token markup; 5,5% на credit top-up; 25+ free models
Free tier50 req/day без покупки; 1000/day после $10 top-up
Latency overhead10–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 выбирает лучший доступный providerdeepseek/deepseek-chat
Provider routingОдна модель на нескольких хостах; routing по price/latency/uptimeDeepSeek via Fireworks vs DeepSeek direct
FallbackAuto-retry на backup model при 429/5xxPrimary Claude → fallback GPT-4o
Free models25+ models @ $0; rate-limited по account tiermeta-llama/llama-3.3-70b-instruct:free

2. OpenRouter vs Direct API: comparison table

DimensionOpenRouterDirect vendor API
API keysОдин key на всех провайдеровОтдельный key на вендора
SDK changesТолько base_urlVendor-specific SDKs/endpoints
Model switchingОдна строка model stringНовый account, billing, rate limits
FailoverBuilt-in fallback models/providersCustom retry logic
Token pricingProvider list price, zero markupProvider list price
Platform fee5,5% на credit purchasesNone
Latency+10–80 ms routing overheadLowest (direct to vendor)
Vendor featuresSubset (no Assistants API, limited caching)Full feature set
ComplianceThird-party US routingDirect vendor DPA/BAA
Free models25+ models, tiered daily limitsVendor-specific free tiers

3. Пять причин перейти на OpenRouter

  1. One key, 400+ models. Prototype на GPT-4o, ship на DeepSeek V4 Flash, escalate на Claude Opus — без re-auth и новых billing accounts.
  2. OpenAI SDK drop-in. Codebases с openai.chat.completions.create() работают после swap двух строк: base_url + model.
  3. Built-in failover. Agent loops постоянно ловят 429. Provider routing + model fallback сжимают custom retry с сотен строк до JSON config block.
  4. Real usage data. OpenRouter публикует weekly token/dollar rankings — production ground truth, не benchmark noise. См. июньский rankings breakdown.
  5. 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 → KeysCreate Key, name (напр. prod-agent), copy immediately. Store в env var — never commit to Git.

export OPENROUTER_API_KEY="sk-or-v1-xxxxxxxx"

Step 3 — First request

Verify key minimal curl call. Expected: JSON с choices[0].message.content.

6. Code examples

6.1 curl

curl https://openrouter.ai/api/v1/chat/completions \ -H "Authorization: Bearer $OPENROUTER_API_KEY" \ -H "Content-Type: application/json" \ -H "HTTP-Referer: https://macgpu.com" \ -H "X-Title: MACGPU Blog Demo" \ -d '{ "model": "anthropic/claude-sonnet-4", "messages": [{"role": "user", "content": "Explain OpenRouter in one sentence."}] }'

6.2 Python (requests)

import os, requests resp = requests.post( "https://openrouter.ai/api/v1/chat/completions", headers={ "Authorization": f"Bearer {os.environ['OPENROUTER_API_KEY']}", "Content-Type": "application/json", }, json={ "model": "google/gemini-2.5-flash", "messages": [{"role": "user", "content": "Hello from Python"}], }, ) print(resp.json()["choices"][0]["message"]["content"])

6.3 Python (OpenAI SDK drop-in)

from openai import OpenAI client = OpenAI( base_url="https://openrouter.ai/api/v1", api_key=os.environ["OPENROUTER_API_KEY"], ) completion = client.chat.completions.create( model="openai/gpt-4o", messages=[{"role": "user", "content": "Drop-in SDK test"}], ) print(completion.choices[0].message.content)

6.4 Node.js (OpenAI SDK)

import OpenAI from "openai"; const client = new OpenAI({ baseURL: "https://openrouter.ai/api/v1", apiKey: process.env.OPENROUTER_API_KEY, }); const res = await client.chat.completions.create({ model: "deepseek/deepseek-chat", messages: [{ role: "user", content: "Node.js via OpenRouter" }], }); console.log(res.choices[0].message.content);

6.5 Streaming

from openai import OpenAI client = OpenAI( base_url="https://openrouter.ai/api/v1", api_key=os.environ["OPENROUTER_API_KEY"], ) stream = client.chat.completions.create( model="anthropic/claude-sonnet-4", messages=[{"role": "user", "content": "Stream this response."}], stream=True, ) for chunk in stream: delta = chunk.choices[0].delta.content or "" print(delta, end="", flush=True)

6.6 Fallback configuration

{ "model": "anthropic/claude-sonnet-4", "models": [ "anthropic/claude-sonnet-4", "openai/gpt-4o", "google/gemini-2.5-flash" ], "messages": [{"role": "user", "content": "Agent task with failover"}] }

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

curl -s https://openrouter.ai/api/v1/models \ -H "Authorization: Bearer $OPENROUTER_API_KEY" \ | python3 -m json.tool | head -80

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

TierDetails
Free models25+ models @ $0/token; 50 req/day без purchase; 1000/day после $10 top-up
Paid modelsProvider list price, zero per-token markup
Platform fee5,5% на credit purchases (не на token usage)
BYOKBring 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.