Openrouter Model Routing
Overview
This skill teaches you how to build intelligent routing logic that selects the best model based on task complexity, cost constraints, or latency requirements.
Prerequisites
- OpenRouter integration with multiple models tested
- Understanding of model capabilities and pricing differences
Instructions
- Classify request complexity: Analyze the incoming prompt to determine if it needs a simple, mid-tier, or advanced model (e.g., by token count, keyword patterns, or task type)
- Build a routing table: Map task categories to model IDs — e.g., simple Q&A to
google/gemma-2-9b-it:free, code generation toanthropic/claude-3.5-sonnet, reasoning toopenai/gpt-4-turbo - Implement the router function: Create a function that takes a prompt and returns the appropriate model ID based on your classification logic
- Add cost guardrails: Set per-request
max_tokensand budget caps to prevent expensive models from exceeding cost limits - Monitor and tune: Log which model handles each request and track quality metrics to adjust routing thresholds over time
Output
- A routing function that selects models based on task type and constraints
- Cost savings from using cheaper models for simple tasks
- Quality metrics per route to validate routing decisions
Error Handling
| Error | Cause | Fix |
|---|---|---|
| Wrong model selected | Classification logic too simple | Add more granular task categories; test with diverse prompts |
| Model unavailable | Selected model is temporarily down | Chain to fallback model (see openrouter-fallback-config) |
| Cost overrun | Complex tasks routed to expensive models | Set hard max_tokens limits and daily budget caps |
See ${CLAUDE_SKILL_DIR}/references/errors.md for full error reference.
Examples
See ${CLAUDE_SKILL_DIR}/references/examples.md for runnable code samples.