| name | openrouter-model-routing |
| description | 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'.
|
| allowed-tools | Read, Write, Edit, Grep, Bash(python3:*) |
| version | 1.20.0 |
| license | MIT |
| author | Jeremy Longshore <jeremy@intentsolutions.io> |
| tags | ["saas","openrouter","routing","cost-optimization","model-selection"] |
| compatibility | Designed for Claude Code, also compatible with Codex and OpenClaw |
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=,
api_key=os.environ[],
default_headers={: , : },
)
MODELS = {
: ,
: ,
: ,
:,
: ,
}
TASK_ROUTING = {
: ,
: ,
: ,
: ,
: ,
: ,
:,
: ,
: ,
: ,
}
() -> :
tier = TASK_ROUTING.get(task_type, )
model = MODELS[tier]
response = client.chat.completions.create(
model=model, messages=messages, **kwargs
)
{
: response.choices[].message.content,
: response.model,
: tier,
: response.usage.prompt_tokens + response.usage.completion_tokens,
}