Model Routing, Gateways and Fallbacks

Route each request to the right model, survive provider outages with fallbacks, and centralize keys, limits and spend in an AI gateway.

Advanced · about 4 hours · difficulty 3/5

Learn first: Rate Limits, Errors and Cost Awareness, Regression Testing and Evals in CI

Depending on one model from one provider is a single point of failure and often a waste of money. Production systems increasingly treat models as interchangeable backends behind a routing layer.

Routing

  • Static routing by task: a small model for classification and extraction, a mid-sized model for most chat, a frontier or reasoning model for hard analysis.
  • Dynamic routing: a cheap classifier estimates difficulty and picks a model per request. Validate with evals that the cheap path does not hurt quality.
  • Cascades: try a cheap model first and escalate when a check fails or confidence is low.

Fallbacks and resilience

  • Retry transient errors with backoff, then fail over to another model or provider.
  • Keep prompts portable; test your fallback model on your eval set so a failover does not silently degrade answers.
  • Use circuit breakers to stop hammering a failing provider.
  • Degrade gracefully: a shorter answer, a cached answer or a clear error beats a hanging spinner.

AI gateways

Gateways such as LiteLLM, Portkey, Cloudflare AI Gateway or cloud platform equivalents sit between your app and providers. They offer a unified API, key management, per-team budgets, rate limiting, logging, caching and fallbacks in one place.

Cost governance

Track spend per feature, team and customer. Set budgets and alerts, and review your most expensive prompts regularly.

Good enough to move on

Your app routes at least two task types to different models, fails over to a second provider in a simulated outage, and reports cost per feature.

Key concepts

  • Task-based model routing
  • Cascades and escalation
  • Provider failover
  • Circuit breakers
  • LiteLLM / Portkey gateways
  • Per-feature cost tracking
  • Graceful degradation

Mini project

Put a LiteLLM proxy (or your own router) in front of your app with a cheap and a strong model, route by task type, configure failover to a second provider, and show the cost difference on a day of traffic.

Check yourself

Your fallback model has never been run against your eval set. What could go wrong during the next provider outage?

Free resources