LLM Routing & Failover: A Practical Guide
LLM routing — sending each request to the right model instead of one model for everything — is the highest-leverage cost and reliability decision most teams aren't making. Done well, cost routing cuts spend by more than 60% with no quality loss, and failover keeps your app up when a provider goes down. This guide covers the three routing patterns that matter (cost, quality, failover), tools like claude-code-router, and how to implement routing without building it yourself.
What LLM routing is
LLM routing means sending each request to the right model instead of defaulting everything to one. There are three patterns worth knowing, and they stack:
- Cost routing — cheap model first, escalate hard cases to a flagship.
- Quality routing — match model to task type (a coding model for code, a cheap model for classification).
- Failover — reroute to another provider/model on error or rate-limit.
All three require reaching multiple models through one interface — which is why routing lives at a gateway or router layer.
Why route
Three reasons, in order of impact: cost (most teams overpay by sending easy traffic to a flagship), reliability (a single provider's outage takes your app down without failover), and quality (the best model for code isn't the best for cheap classification). The first is the biggest and most measurable.
Cost routing (the big win)
The single highest-leverage change: route the easy majority of traffic to a cheap model and reserve a flagship for the hard minority. The savings are large and concrete. For a 100M-input / 20M-output monthly workload:
Two patterns implement this:
- Classify-then-escalate — a cheap model (or heuristic) decides "is this hard?" and sends only the hard cases to a flagship.
- Try-cheap-then-verify — run a cheap model, apply a check the task already needs (tests pass, valid JSON, tool call succeeds), and retry on a flagship only on failure. The simplest to start with.
Quality routing
Beyond cost, route by task type: a dedicated coding model (Qwen3 Coder, Grok Code Fast) for code, a cheap general model for classification and extraction, a multimodal model for documents, a flagship for hard reasoning. Quality routing ensures each request hits a model that's actually good at it — often cheaper and better than one general default.
Failover
Providers have outages and rate limits. Failover detects an error or timeout and automatically retries on a different provider or model, so a single provider's bad day doesn't become your downtime. It only works when the fallback targets are reachable through one interface — so failover is implemented at the gateway/router layer that pools providers behind an OpenAI-compatible API. A good failover policy also handles rate-limit (429) responses by rerouting rather than backing off.
Tools: claude-code-router & more
- claude-code-router — an open-source tool that routes Claude Code (and similar agents) across models/providers, letting you use cheaper or alternative models for parts of an agent's work.
- LiteLLM — a popular self-host router: run it yourself, pay no platform fee, keep traffic in your infrastructure.
- Hosted gateways (OpenRouter, DataLLM Lab, Portkey) — routing + failover + one bill across providers, no infrastructure to run. Compared in our OpenRouter alternatives guide.
Build vs buy
Build (self-host)
- LiteLLM or claude-code-router — full control, no platform fee, traffic stays in your VPC. You operate it.
Buy (gateway)
- One OpenAI-compatible key, built-in failover and price routing, one bill. DataLLM Lab is one option, with live cross-provider price comparison.
Either way the foundation is OpenAI-compatibility: because switching models is a config change, routing logic stays simple. Here's the minimal try-cheap-then-escalate pattern through a gateway:
from openai import OpenAI
client = OpenAI(base_url="https://www.datallmlab.com/v1", api_key="$DATALLMLAB_API_KEY")
def solve(prompt, check):
cheap = client.chat.completions.create(model="deepseek/deepseek-v3.2", messages=[{"role":"user","content":prompt}])
out = cheap.choices[0].message.content
if check(out): return out # cheap model passed — done
strong = client.chat.completions.create(model="anthropic/claude-opus-4.7", messages=[{"role":"user","content":prompt}])
return strong.choices[0].message.content # escalate only on failure
Routing & failover, built in
One OpenAI-compatible key for 300+ models with live price comparison and automatic failover — so cost routing is a rule, not a rebuild.
FAQ
What is LLM routing?
Directing each request to the most appropriate model instead of one model for everything. Patterns: cost routing (cheap-first), quality routing (model-to-task), and failover (reroute on error). The main lever for cutting cost without losing quality.
How much can LLM cost routing save?
Often 60%+. For a 100M/20M monthly workload, all-flagship ≈ $1,000 vs a 70/30 cheap-to-flagship split ≈ $320 — about 68% less.
What is claude-code-router?
An open-source tool that routes Claude Code (and similar agents) across models/providers, so cheaper or alternative models handle parts of an agent's work. A hosted gateway provides the same plus failover and cross-provider billing.
How does LLM failover work?
It detects an error, timeout, or rate-limit and retries on another provider/model automatically — implemented at a gateway/router layer that pools providers behind one OpenAI-compatible API.
Should I build routing or use a gateway?
Build with a self-host router (LiteLLM) for full control and no platform fee if you can run it; use a hosted gateway for routing + failover + one bill without operating it. Both rely on OpenAI-compatibility.
What's the simplest routing pattern?
Try-cheap-then-verify: run a cheap model, apply a check the task needs (tests/JSON/tool call), retry on a flagship only on failure.
DataLLM Lab