News / Learn / Production & LLMOps
How do model fallbacks and routing work?
You'll be able to design a fallback chain that keeps your AI feature working when a model fails or costs too much.
In plain English
A model fallback is a backup plan. If the first model call fails, times out, or hits a rate limit, your app tries another model. Model routing is the decision step that picks which model should handle each request in the first place.
Why it matters
Model providers have outages, rate limits, and slow periods. A single hard dependency means your product goes down with them. Routing also lets you send easy requests to cheaper models and hard ones to stronger models, which controls cost and speed.
How it works
Start with a primary model and an ordered list of backups. Wrap each call so that errors such as timeouts, server errors, and rate limit responses trigger the next option. Only retry errors that are safe to retry, and cap total time so the user is not left waiting.
Routing decides the starting point. A router can use simple rules, such as request length, feature type, or user tier. It can also use a small classifier that estimates how hard a request is. Keep rules explicit at first. They are easier to test and explain than a clever router you cannot inspect.
Backups are not identical copies. Different models follow instructions, format output, and handle long inputs differently. Each fallback needs its own prompt tuning, output checks, and tests. A fallback that returns broken JSON is still a failure.
Log which model answered each request, why it was chosen, and how long it took. Without this, you cannot tell whether your fallbacks fire often or whether a provider is quietly degrading.
A concrete example
A simple fallback loop. It tries each model in order and stops at the first success. Call the provider function with your own client.
import time
MODELS = ["primary-model", "backup-model", "last-resort-model"]
RETRYABLE = (TimeoutError, ConnectionError)
def call_with_fallback(prompt, call_model, deadline_s=20):
start = time.monotonic()
last_error = None
for name in MODELS:
if time.monotonic() - start > deadline_s:
break
try:
return name, call_model(name, prompt)
except RETRYABLE as err:
last_error = err
continue
raise RuntimeError("All models failed") from last_errorA short sketch to show the idea. Check the official docs of the tool you use.
What goes wrong
- Falling back to a model that behaves differently Test every fallback with your real prompts and output parser. Treat each model as its own integration.
- Retrying errors that will never succeed Retry timeouts and server errors. Do not retry invalid requests or content refusals, since the next model may refuse too.
- No total time limit Set an overall deadline across all attempts so a chain of slow failures does not stall the user.
- Silent fallbacks Record which model served each request. Otherwise you will not notice when your primary model is failing often.
Going deeper
Learn about circuit breakers, which stop sending traffic to a failing provider for a period. Also study cost-aware routing and how to evaluate each route with the same eval set before you ship it.
FAQ
Is a fallback the same as retrying?
No. A retry calls the same model again. A fallback calls a different model. Good systems often do a short retry first, then move to a fallback.
How many fallbacks do I need?
Usually one or two are enough. Each extra option adds testing work and more differences in output. Start small and add more only when you see real failures.
Should the router use an AI model to pick the model?
You can, but start with simple rules. A rule-based router is cheaper, faster, and easier to debug. Add a classifier only if the rules clearly fall short.
Do fallbacks guarantee uptime?
No. They reduce risk but cannot fix a shared dependency, such as your own network or a billing problem. Plan for a graceful message when every option fails.
Written with AI by CapyLaunch. Spotted a mistake? Email hello@capylaunch.com.