Prompt
LLM request routing cost latency fallback
Latest observation
Nov 21, 2026
OpenAI APIWeb search: off
Here’s a practical framework for LLM request routing with an emphasis on cost, latency, and fallback.
Core idea
Route each request to the “best” model/provider based on:
- Cost: cheaper models for routine requests
- Latency: faster models for interactive/low-latency needs
- Quality: stronger models for complex tasks
- Fallback: automatic retry/escalation when a model fails or returns low-confidence output
Common routing strategy
1) Classify the request
Before sending to an LLM, score the request on:
- Complexity: simple Q&A vs reasoning/coding/synthesis
- Latency sensitivity: interactive chat vs batch job
- Quality sensitivity: can a smaller model be tolerated?
- Risk/compliance: requires stronger model, audit trail, or special handling
- Context size: long context may force a larger/more expensive model
This can be done with:
- Rules/heuristics
- A lightweight classifier model
- User/app metadata
2) Maintain a model tier list
Example:
- Tier 0: ultra-cheap, very fast
- Tier 1: balanced default
- Tier 2: high-quality
- Tier 3: premium / fallback only
Route by default to the lowest tier that meets the request requirements.
Routing policy examples
Low latency, high throughput
- Prefer cached response if available
- Use fastest acceptable model
- Set strict timeout
- If timeout/failure, fallback to next model in tier order
Cost optimized
- Try cheapest model first
- If output fails validation or confidence threshold, escalate
- Use structured prompts and shorter context to reduce cost
Quality optimized
- Use best model immediately for hard tasks
- Fallback only if the provider fails
- Optionally run cheap verifier afterward
Fallback design
Fallback triggers
- Timeout
- Rate limit
- Provider error
- Empty/invalid response
- Output fails schema validation
- Confidence score below threshold
- Safety filter rejection
Fallback chain
Example:
- Cheap model
- Medium model
- Strong model
- Human review or canned response
Important:
- Preserve request idempotency
- Track retries to avoid duplicate charges/actions
- Use exponential backoff only when appropriate
Practical routing signals
Useful features for decisioning:
- Prompt length
- Expected output length
- User tier / SLA
- Historical success rate per model
- Average latency per model
- Current model load / queue depth
- Cost per token
- Task type: extraction, summarization, code, chat, reasoning
- Risk level: financial, legal, medical, internal, public
Simple decision matrix
| Request type | Latency need | Quality need | Suggested route |
|---|---|---|---|
| FAQ / short chat | High | Low | Cheap fast model |
| Summarization | Medium | Medium | Balanced model |
| Coding / reasoning | Medium | High | Strong model |
| Regulated / sensitive | Medium | High | Premium + validation |
| Batch processing | Low | Medium | Cheapest acceptable |
| User-facing critical flow | High | High | Fast premium or fast balanced |
Reliability patterns
Circuit breaker
If a provider/model is failing frequently:
- Temporarily stop routing to it
- Route elsewhere until recovery
Shadow testing
Send a copy of some requests to a new model without affecting the user response:
- Compare quality/cost/latency
- Use for evaluation before switching traffic
Hedged requests
For very latency-sensitive flows:
- Send to two models/providers after a short delay
- Take the first good result
- More expensive, but reduces tail latency
A robust routing flow
- Parse request metadata
- Estimate complexity, latency sensitivity, and risk
- Pick primary model using policy
- Execute with timeout and validation
- If failure, fallback to next model
- Log outcome, cost, latency, and quality signals
- Update routing stats
Pseudocode
def route_request(request):
features = classify(request)
candidates = choose_models(features)
for model in candidates:
try:
response = call_model(model, request, timeout=features.timeout)
if validate(response, request):
return response
except Exception as e:
log_failure(model, e)
continue
return fallback_response(request)
Metrics to track
- Success rate
- p50/p95 latency
- Cost per successful request
- Retry rate
- Fallback rate
- Validation failure rate
- User satisfaction / task success
Best practice
Start with a simple policy:
- Route most traffic to a balanced default
- Use cheap model for easy tasks
- Use strong model for hard or high-risk tasks
- Add validation and fallback
- Continuously measure and optimize
If you want, I can also provide:
- a production-ready routing architecture,
- a decision algorithm, or
- example code in Python/TypeScript.