OpenRouter Setup & Routing Basics
7 examples to get you from zero to a working OpenRouter call - 5 basic and 2 intermediate.
Search across all documentation pages
7 examples to get you from zero to a working OpenRouter call - 5 basic and 2 intermediate.
You will create a key, point an OpenAI-compatible client at OpenRouter, send a chat completion, swap model slugs, and stream a response.
python -m venv .venv && source .venv/bin/activate
pip install openai
export OPENROUTER_API_KEY="sk-or-..." # never commit real keysRelated: Authenticating and Configuring the OpenRouter Base URL - full client configuration
import os
from openai import OpenAI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=os.environ["OPENROUTER_API_KEY"],
)
resp = client.chat.completions.create(
model="openai/gpt-4o-mini", # illustrative slug; verify at build
messages=[{"role": "user", "content": "Reply with exactly: openrouter-ok"}],
extra_headers={
"HTTP-Referer": "https://localhost", # optional app attribution
"X-OpenRouter-Title": "setup-basics",
},
)
print(resp.choices[0].message.content)
print("resolved model:", resp.model)provider/model form, not bare gpt-4o-mini.curl https://openrouter.ai/api/v1/chat/completions \
-H "Authorization: Bearer $OPENROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-4o-mini",
"messages": [{"role": "user", "content": "Say hi in five words"}]
}'MODELS = [
"openai/gpt-4o-mini",
"anthropic/claude-sonnet-4.5", # illustrative; verify at build
"openrouter/auto",
]
for model in MODELS:
r = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": "One-sentence definition of an agent."}],
)
print(model, "->", r.model, r.choices[0].message.content[:80])response.model.openrouter/auto lets OpenRouter pick a model per prompt (see auto-router page).stream = client.chat.completions.create(
model="openai/gpt-4o-mini", # verify at build
messages=[{"role": "user", "content": "Count from 1 to 5 slowly."}],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content or ""
print(delta, end="", flush=True)
print()resp = client.chat.completions.create(
model="anthropic/claude-sonnet-4.5", # primary; verify at build
messages=[{"role": "user", "content": "Name two agent stop conditions."}],
extra_body={
"models": ["openai/gpt-4o-mini", "google/gemini-2.5-flash"], # verify at build
},
)
print("used:", resp.model)
print(resp.choices[0].message.content)Related: Switching Models by Config, Not Code: A Routing Pattern
resp = client.chat.completions.create(
model="meta-llama/llama-3.3-70b-instruct:nitro", # verify slug at build
messages=[{"role": "user", "content": "Summarize provider routing in one line."}],
)
print(resp.choices[0].message.content):nitro is a shortcut for sorting providers by throughput.:floor is the cost-oriented counterpart.Stack versions: Pins from the category manifest (verify at build): OpenRouter (~315+ models, July 2026 pricing/fees); LangGraph 1.0+; CrewAI 1.14+; Microsoft Agent Framework 1.0; Vercel AI SDK 6; Pydantic AI (latest); LlamaIndex (latest); OpenAI Agents SDK (latest + MCP); MCP (Linux Foundation governance); A2A (HTTP+SSE+JSON-RPC 2.0); Solana
@solana/web3.js+@solana/spl-token.
Reviewed by Chris St. John·Last updated Jul 16, 2026