OpenRouter Model Routing
Overview
OpenRouter gives you access to 100+ models through one API. The key to cost efficiency is routing each request to the right model based on task complexity, required capabilities, cost budget, and latency requirements. This skill covers task-based routing, complexity classification, cost-aware selection, and OpenRouter's native routing features.
Prerequisites
- An OpenRouter API key exported as
OPENROUTER_API_KEY — see the openrouter-install-auth skill for setup
- Python 3.8+ with the OpenAI SDK and
requests (pip install openai requests)
- A rough inventory of your task mix (classification, summarization, code generation, deep reasoning, ...) to seed the
TASK_ROUTING table
- Credits sized for the tiers you route to — the premium tier (
openai/o1) runs $15/$60 per 1M tokens, 250x the budget tier
Instructions
- Define your tiers per Task-Based Router: the
MODELS dict (free → budget → mid → standard → premium) and the TASK_ROUTING map, then send requests through route_request(), which returns content, the serving model, tier, and token count.
- When callers can't label tasks, switch to the Complexity-Based Auto-Router —
classify_complexity() scores word count, code, reasoning, and math markers to pick a tier inside auto_route().
- Add resilience per OpenRouter Native Routing:
extra_body={"models": [...], "route": "fallback"} tries models in order, provider.order controls which provider serves, and the :floor variant picks the cheapest provider automatically.
- Keep pricing current per Cost-Aware Router —
get_model_pricing() pulls live per-1M rates from GET /api/v1/models, and cheapest_model_for_task() selects under context/tooling constraints.
- Log every routing decision (task type, tier, model, cost) and tune per Error Handling and Enterprise Considerations — escalate the tier on quality regressions and cap per-request cost with
max_tokens.
Task-Based Router
import os, re
from openai import OpenAI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=os.environ["OPENROUTER_API_KEY"],
default_headers={"HTTP-Referer": "https://my-app.com", "X-Title": "my-app"},
)
# Model tiers by cost and capability
MODELS = {
"free": "google/gemma-2-9b-it:free", # $0/0 — testing only
"budget": "meta-llama/llama-3.1-8b-instruct", # $0.06/$0.06 per 1M
"mid": "openai/gpt-4o-mini", # $0.15/$0.60 per 1M
"standard":"anthropic/claude-3.5-sonnet", # $3/$15 per 1M
"premium": "openai/o1", # $15/$60 per 1M
}
TASK_ROUTING = {
"classification": "budget", # Simple label assignment
"translation": "mid", # Moderate quality needed
"summarization": "mid", # Good quality, cost-effective
"code_generation": "standard", # Needs high accuracy
"code_review": "standard", # Needs reasoning
"analysis": "standard", # Complex reasoning
"creative_writing":"standard", # Quality matters
"deep_reasoning": "premium", # Multi-step logic
"simple_qa": "budget", # Basic questions
"chat": "mid", # General conversation
}
def route_request(task_type: str, messages: list[dict], **kwargs) -> dict:
"""Route to appropriate model based on task type."""
tier = TASK_ROUTING.get(task_type, "mid")
model = MODELS[tier]
response = client.chat.completions.create(
model=model, messages=messages, **kwargs
)
return {
"content": response.choices[0].message.content,
"model": response.model,
"tier": tier,
"tokens": response.usage.prompt_tokens + response.usage.completion_tokens,
}
Complexity-Based Auto-Router
def classify_complexity(prompt: str) -> str:
"""Classify prompt complexity to select model tier.
Simple heuristics -- replace with a trained classifier for production.
"""
word_count = len(prompt.split())
has_code = bool(re.search(r'```|def |function |class |import ', prompt))
has_reasoning = bool(re.search(r'explain|analyze|compare|why|how does|trade.?off', prompt, re.I))
has_math = bool(re.search(r'calculate|equation|formula|derive|proof', prompt, re.I))
if has_math or (has_reasoning and has_code):
return "premium"
if has_code or has_reasoning or word_count > 500:
return "standard"
if word_count > 100:
return "mid"
return "budget"
def auto_route(messages: list[dict], **kwargs):
"""Automatically select model based on prompt complexity."""
user_msg = next((m["content"] for m in reversed(messages) if m["role"] == "user"), "")
tier = classify_complexity(user_msg)
model = MODELS[tier]
response = client.chat.completions.create(model=model, messages=messages, **kwargs)
return response
OpenRouter Native Routing
# Route: "fallback" — try models in order until one succeeds
response = client.chat.completions.create(
model="anthropic/claude-3.5-sonnet",
messages=[{"role": "user", "content": "Hello"}],
max_tokens=200,
extra_body={
"models": [
"anthropic/claude-3.5-sonnet",
"openai/gpt-4o",
"openai/gpt-4o-mini",
],
"route": "fallback",
},
)
# Provider routing — control which provider serves a model
response = client.chat.completions.create(
model="anthropic/claude-3.5-sonnet",
messages=[{"role": "user", "content": "Hello"}],
max_tokens=200,
extra_body={
"provider": {
"order": ["Anthropic", "AWS Bedrock"],
"allow_fallbacks": True,
},
},
)
# Model variant: ":floor" picks cheapest provider
response = client.chat.completions.create(
model="anthropic/claude-3.5-sonnet:floor",
messages=[{"role": "user", "content": "Hello"}],
max_tokens=200,
)
Cost-Aware Router
import requests
def get_model_pricing() -> dict:
"""Fetch current pricing for cost-aware routing."""
models = requests.get("https://openrouter.ai/api/v1/models").json()["data"]
return {
m["id"]: {
"prompt": float(m["pricing"]["prompt"]) * 1_000_000,
"completion": float(m["pricing"]["completion"]) * 1_000_000,
"context": m["context_length"],
}
for m in models
}
def cheapest_model_for_task(pricing: dict, min_context: int = 4096,
needs_tools: bool = False) -> str:
"""Find the cheapest model that meets requirements."""
candidates = [
(mid, p) for mid, p in pricing.items()
if p["context"] >= min_context and p["prompt"] > 0 # Exclude free (unreliable)
]
candidates.sort(key=lambda x: x[1]["prompt"] + x[1]["completion"])
return candidates[0][0] if candidates else "openai/gpt-4o-mini"
Output
- Routed completion dicts from
route_request(): the reply content, the actual model that served, the tier chosen, and total tokens consumed
- Router decision traces per request, e.g.
[Router] Task=code -> Model=anthropic/claude-3.5-sonnet, giving you an audit trail to tune the routing table against
- A live pricing map from
get_model_pricing() keyed by model ID: per-1M prompt/completion cost plus context length for cost-aware selection
Examples
The same router sends trivial and demanding prompts to opposite ends of the cost spectrum:
print(routed_completion("What is 2+2?"))
# [Router] Task=simple -> Model=google/gemma-2-9b-it:free
print(routed_completion("Write a Python function to merge two sorted lists."))
# [Router] Task=code -> Model=anthropic/claude-3.5-sonnet
The 4-word arithmetic prompt lands on the free tier while the code request escalates to Claude 3.5 Sonnet — the spread between those two decisions is where the cost savings live. More worked examples: references/examples.md.
Error Handling
| Error |
Cause |
Fix |
| Wrong model selected |
Classification too coarse |
Add more task categories; test with diverse prompts |
| Model unavailable |
Selected model temporarily down |
Add fallback chain per tier |
| Cost overrun |
Complex tasks routed to premium models |
Set max_tokens and daily budget caps |
| Quality regression |
Budget model can't handle task |
Monitor output quality; escalate tier on poor results |
Enterprise Considerations
- Start with manual task-type routing (explicit labels), then graduate to auto-classification
- Log every routing decision (task type, tier, model, cost) to tune the router over time
- Use OpenRouter's
:floor variant to automatically get the cheapest provider for any model
- Set
max_tokens on every request to cap per-request cost regardless of model tier
- A/B test routing rules: send 10% of traffic to a different tier and compare quality metrics
- Combine with fallback chains so each tier has backup models
References
Source: jeremylongshore/claude-code-plugins-plus-skills → skills/.curated/openrouter-model-routing/SKILL.md
Also appears in: jeremylongshore/claude-code-plugins-plus-skills/plugins/saas-packs/openrouter-pack/skills/openrouter-model-routing/SKILL.md
1---2name: openrouter-model-routing3description: "'Implement intelligent model routing to optimize cost, quality, and latency on OpenRouter. Use when building multi-model systems or optimizing spend across task types. Triggers: ''openrouter routing'', ''model routing'', ''route to model'', ''model selection openrouter''. '"4allowed-tools: "Read, Write, Edit, Grep, Bash(python3:*)"5category: general-purpose6source_repo: jeremylongshore/claude-code-plugins-plus-skills7source_path: "skills/.curated/openrouter-model-routing/SKILL.md"8source_url: https://github.com/jeremylongshore/claude-code-plugins-plus-skills/blob/HEAD/skills/.curated/openrouter-model-routing/SKILL.md9---1011# OpenRouter Model Routing1213## Overview1415OpenRouter gives you access to 100+ models through one API. The key to cost efficiency is routing each request to the right model based on task complexity, required capabilities, cost budget, and latency requirements. This skill covers task-based routing, complexity classification, cost-aware selection, and OpenRouter's native routing features.1617## Prerequisites1819- An OpenRouter API key exported as `OPENROUTER_API_KEY` — see the `openrouter-install-auth` skill for setup20- Python 3.8+ with the OpenAI SDK and `requests` (`pip install openai requests`)21- A rough inventory of your task mix (classification, summarization, code generation, deep reasoning, ...) to seed the `TASK_ROUTING` table22- Credits sized for the tiers you route to — the premium tier (`openai/o1`) runs $15/$60 per 1M tokens, 250x the budget tier2324## Instructions25261. Define your tiers per Task-Based Router: the `MODELS` dict (free → budget → mid → standard → premium) and the `TASK_ROUTING` map, then send requests through `route_request()`, which returns `content`, the serving `model`, `tier`, and token count.272. When callers can't label tasks, switch to the Complexity-Based Auto-Router — `classify_complexity()` scores word count, code, reasoning, and math markers to pick a tier inside `auto_route()`.283. Add resilience per OpenRouter Native Routing: `extra_body={"models": [...], "route": "fallback"}` tries models in order, `provider.order` controls which provider serves, and the `:floor` variant picks the cheapest provider automatically.294. Keep pricing current per Cost-Aware Router — `get_model_pricing()` pulls live per-1M rates from `GET /api/v1/models`, and `cheapest_model_for_task()` selects under context/tooling constraints.305. Log every routing decision (task type, tier, model, cost) and tune per Error Handling and Enterprise Considerations — escalate the tier on quality regressions and cap per-request cost with `max_tokens`.3132## Task-Based Router3334```python35import os, re36from openai import OpenAI3738client = OpenAI(39 base_url="https://openrouter.ai/api/v1",40 api_key=os.environ["OPENROUTER_API_KEY"],41 default_headers={"HTTP-Referer": "https://my-app.com", "X-Title": "my-app"},42)4344# Model tiers by cost and capability45MODELS = {46 "free": "google/gemma-2-9b-it:free", # $0/0 — testing only47 "budget": "meta-llama/llama-3.1-8b-instruct", # $0.06/$0.06 per 1M48 "mid": "openai/gpt-4o-mini", # $0.15/$0.60 per 1M49 "standard":"anthropic/claude-3.5-sonnet", # $3/$15 per 1M50 "premium": "openai/o1", # $15/$60 per 1M51}5253TASK_ROUTING = {54 "classification": "budget", # Simple label assignment55 "translation": "mid", # Moderate quality needed56 "summarization": "mid", # Good quality, cost-effective57 "code_generation": "standard", # Needs high accuracy58 "code_review": "standard", # Needs reasoning59 "analysis": "standard", # Complex reasoning60 "creative_writing":"standard", # Quality matters61 "deep_reasoning": "premium", # Multi-step logic62 "simple_qa": "budget", # Basic questions63 "chat": "mid", # General conversation64}6566def route_request(task_type: str, messages: list[dict], **kwargs) -> dict:67 """Route to appropriate model based on task type."""68 tier = TASK_ROUTING.get(task_type, "mid")69 model = MODELS[tier]7071 response = client.chat.completions.create(72 model=model, messages=messages, **kwargs73 )74 return {75 "content": response.choices[0].message.content,76 "model": response.model,77 "tier": tier,78 "tokens": response.usage.prompt_tokens + response.usage.completion_tokens,79 }80```8182## Complexity-Based Auto-Router8384```python85def classify_complexity(prompt: str) -> str:86 """Classify prompt complexity to select model tier.8788 Simple heuristics -- replace with a trained classifier for production.89 """90 word_count = len(prompt.split())91 has_code = bool(re.search(r'```|def |function |class |import ', prompt))92 has_reasoning = bool(re.search(r'explain|analyze|compare|why|how does|trade.?off', prompt, re.I))93 has_math = bool(re.search(r'calculate|equation|formula|derive|proof', prompt, re.I))9495 if has_math or (has_reasoning and has_code):96 return "premium"97 if has_code or has_reasoning or word_count > 500:98 return "standard"99 if word_count > 100:100 return "mid"101 return "budget"102103def auto_route(messages: list[dict], **kwargs):104 """Automatically select model based on prompt complexity."""105 user_msg = next((m["content"] for m in reversed(messages) if m["role"] == "user"), "")106 tier = classify_complexity(user_msg)107 model = MODELS[tier]108109 response = client.chat.completions.create(model=model, messages=messages, **kwargs)110 return response111```112113## OpenRouter Native Routing114115```python116# Route: "fallback" — try models in order until one succeeds117response = client.chat.completions.create(118 model="anthropic/claude-3.5-sonnet",119 messages=[{"role": "user", "content": "Hello"}],120 max_tokens=200,121 extra_body={122 "models": [123 "anthropic/claude-3.5-sonnet",124 "openai/gpt-4o",125 "openai/gpt-4o-mini",126 ],127 "route": "fallback",128 },129)130131# Provider routing — control which provider serves a model132response = client.chat.completions.create(133 model="anthropic/claude-3.5-sonnet",134 messages=[{"role": "user", "content": "Hello"}],135 max_tokens=200,136 extra_body={137 "provider": {138 "order": ["Anthropic", "AWS Bedrock"],139 "allow_fallbacks": True,140 },141 },142)143144# Model variant: ":floor" picks cheapest provider145response = client.chat.completions.create(146 model="anthropic/claude-3.5-sonnet:floor",147 messages=[{"role": "user", "content": "Hello"}],148 max_tokens=200,149)150```151152## Cost-Aware Router153154```python155import requests156157def get_model_pricing() -> dict:158 """Fetch current pricing for cost-aware routing."""159 models = requests.get("https://openrouter.ai/api/v1/models").json()["data"]160 return {161 m["id"]: {162 "prompt": float(m["pricing"]["prompt"]) * 1_000_000,163 "completion": float(m["pricing"]["completion"]) * 1_000_000,164 "context": m["context_length"],165 }166 for m in models167 }168169def cheapest_model_for_task(pricing: dict, min_context: int = 4096,170 needs_tools: bool = False) -> str:171 """Find the cheapest model that meets requirements."""172 candidates = [173 (mid, p) for mid, p in pricing.items()174 if p["context"] >= min_context and p["prompt"] > 0 # Exclude free (unreliable)175 ]176 candidates.sort(key=lambda x: x[1]["prompt"] + x[1]["completion"])177 return candidates[0][0] if candidates else "openai/gpt-4o-mini"178```179180## Output181182- Routed completion dicts from `route_request()`: the reply `content`, the actual `model` that served, the `tier` chosen, and total `tokens` consumed183- Router decision traces per request, e.g. `[Router] Task=code -> Model=anthropic/claude-3.5-sonnet`, giving you an audit trail to tune the routing table against184- A live pricing map from `get_model_pricing()` keyed by model ID: per-1M `prompt`/`completion` cost plus `context` length for cost-aware selection185186## Examples187188The same router sends trivial and demanding prompts to opposite ends of the cost spectrum:189190```python191print(routed_completion("What is 2+2?"))192# [Router] Task=simple -> Model=google/gemma-2-9b-it:free193194print(routed_completion("Write a Python function to merge two sorted lists."))195# [Router] Task=code -> Model=anthropic/claude-3.5-sonnet196```197198The 4-word arithmetic prompt lands on the free tier while the code request escalates to Claude 3.5 Sonnet — the spread between those two decisions is where the cost savings live. More worked examples: `references/examples.md`.199200## Error Handling201202| Error | Cause | Fix |203|-------|-------|-----|204| Wrong model selected | Classification too coarse | Add more task categories; test with diverse prompts |205| Model unavailable | Selected model temporarily down | Add fallback chain per tier |206| Cost overrun | Complex tasks routed to premium models | Set `max_tokens` and daily budget caps |207| Quality regression | Budget model can't handle task | Monitor output quality; escalate tier on poor results |208209## Enterprise Considerations210211- Start with manual task-type routing (explicit labels), then graduate to auto-classification212- Log every routing decision (task type, tier, model, cost) to tune the router over time213- Use OpenRouter's `:floor` variant to automatically get the cheapest provider for any model214- Set `max_tokens` on every request to cap per-request cost regardless of model tier215- A/B test routing rules: send 10% of traffic to a different tier and compare quality metrics216- Combine with fallback chains so each tier has backup models217218## References219220- Examples | Errors221- [Model Routing](https://openrouter.ai/docs/features/model-routing) | [Provider Routing](https://openrouter.ai/docs/features/provider-routing)222223---224225**Source:** [`jeremylongshore/claude-code-plugins-plus-skills`](https://github.com/jeremylongshore/claude-code-plugins-plus-skills) → `skills/.curated/openrouter-model-routing/SKILL.md`226227**Also appears in:** `jeremylongshore/claude-code-plugins-plus-skills/plugins/saas-packs/openrouter-pack/skills/openrouter-model-routing/SKILL.md`