Topic 3 of 4 · 4 min read
Routing requests by task and difficulty
Goal: Write routing rules that send each task type to a model that meets its quality bar at the lowest cost.
Routing sends each request to the cheapest model that still meets that task's quality bar. A short classification does not need the strongest model. A contract analysis may. Paying for the strong model on everything is the most common waste in LLM apps.
Check yourself · 1 of 3
What should exist before you move ticket tagging from a strong model to a small one?
Why it matters
Model prices differ by large factors between small and large tiers, and so does speed. If half your traffic is simple (tagging, extraction, short rewrites), moving it to a small model cuts cost and latency. The risk runs the other way: sending a hard task to a weak model gives wrong answers that look fine.
Three ways to route
- Static rules. You map task type to model in config. The support-ticket tagger uses a small model; the report writer uses a strong one. Predictable, easy to test, and what most teams should start with.
- A classifier. A small model, or a rule on input length and keywords, labels each request as easy or hard, then picks the model. It adapts to input, but the classifier itself costs money, adds latency and can be wrong.
- Automatic routers. Some services pick a model for you per prompt. OpenRouter offers model lists with fallbacks, and LiteLLM's router handles load balancing, retries and fallbacks across deployments. Treat "pick the best model for me" features as a black box you must still measure.
How to write rules that hold up
- Define the quality bar per task. For example "tags match a human on at least 95 of 100 labelled tickets". Without a bar, "cheap enough" has no meaning.
- Test each tier on a labelled set from real traffic, not on three samples.
- Pick the cheapest model that passes, then re-test when prices or models change.
- Add an escalation path. If the small model's output fails validation (bad JSON, low confidence), retry once on the stronger model.
- Log the route with each call so you can see the split and the failure rate per tier.
Before and after
// Before: everything uses the strong model
const model = "strong-model";
// After: static rules plus escalation
const ROUTES = {
tag_ticket: "small-model",
summarise_thread: "mid-model",
analyse_contract: "strong-model",
} as const;
async function run(task: keyof typeof ROUTES, input: string) {
const first = await call(ROUTES[task], input);
if (valid(first) || ROUTES[task] === "strong-model") return first;
return call("strong-model", input); // escalate once
}
What goes wrong
- Routing on price alone. A cheap model that fails often may cost more once humans fix the failures.
- No escalation. Hard inputs inside an "easy" task type fail silently.
- A classifier nobody measures. If it sends hard prompts to the small model, quality drops with no error.
- Stale rules. New models change the price-quality order; rules need a periodic re-test.
Examples
Three tasks, three tiers
A helpdesk tool tags tickets (small model), summarises long threads (mid model) and drafts refund-policy replies that quote contract terms (strong model). Each route is in config with the pass rate it achieved on a labelled set of real examples, so a future change can be compared against it.
Escalation on bad JSON
The small model must return JSON with category and priority. If parsing fails or a field is missing, the code retries once on the strong model and logs escalated: true. The team watches the escalation rate: a rise means the small model no longer fits the traffic.
Key points
- Route by task: pick the cheapest model that meets a written quality bar for that task.
- Start with static rules per task type; add a classifier only when inputs within one task vary a lot.
- Test each tier on a labelled set from real traffic, not on a few samples.
- Add an escalation path so a failed validation retries on a stronger model.
- Log the chosen route on every call and re-test when models or prices change.
Common mistakes
- Choosing models by price alone, without measuring failure rate on the task.
- Sending every request of a task type to the small model with no escalation for hard inputs.
- Adding an unmeasured classifier whose mistakes silently lower quality.
- Never re-testing routes after providers release new models or change prices.
Check yourself · 2 of 3
A small model sometimes returns malformed JSON on long tickets. Which design handles that best?
Check yourself · 3 of 3
Which approach to routing is the best default for a team's first version?
Practice items, not counted towards your mastery.
Start here
- LiteLLM documentation (opens in a new tab)DocsFree
One interface and proxy server for many providers, with keys, budgets and logging.
- OpenRouter quickstart (opens in a new tab)DocsFree
Calling many models through one OpenAI-compatible endpoint with one key.
- AI SDK: providers and models (opens in a new tab)DocsFree
How a provider-neutral SDK swaps models behind one function call.
More for this topic
- LiteLLM routing and load balancing (opens in a new tab)DocsFree
Router settings for retries, fallbacks, cooldowns and load balancing across deployments.
- OpenRouter model fallbacks (opens in a new tab)DocsFree
How a model list lets a request fall through to the next model on error.