`with_fallbacks` is ordered failover: on exception, advance to the next backup Runnable; never retry the same one, never run concurrently, never silently swallow.
Imagine you have three friends to ask for a ride home. You call the first; if she does not answer, you call the second; if he does not answer, you call the third. You do not redial the first friend over and over, you do not call all three at once, and if all three fail to answer you give up and figure out a Plan D. That is exactly how `with_fallbacks` works: a list of friends, called in order, with no redialing.
Detailed answer & concept explanation~8 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
5 min: failover semantics in detail + contrast with with_retry + the exception filter trap + production composition pattern + observability on fallback rate + when to skip fallbacks.
Real products, models, and research that use this idea.
- Provider failover patterns: primary `ChatOpenAI` with `gpt-5.5`, fallback to `ChatAnthropic` with `Claude Opus 4.7`, the canonical use case.
- Cost-tier fallback: premium model as primary, cheaper model as backup; if the premium is rate-limited, the cheaper model serves the request.
- Region failover for compliance: EU-region OpenAI as primary, US-region as backup; in normal operation only the EU serves traffic.
- Local-model backup: hosted provider as primary, local Ollama-served model as the last-resort backup for hard-availability requirements.
- Klarna's customer-support stack reportedly uses provider-fallback patterns to maintain availability during OpenAI rate-limit windows.
What an interviewer would ask next. Try answering before peeking at the approach.
QWalk through the composition pattern for retry plus fallback in detail.
QHow would you build race-style concurrent fallback if you wanted it?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Confusing failover (next Runnable on failure) with retry (same Runnable again). `with_fallbacks` is the first; `with_retry` is the second. Mixing them up causes thundering retries on the wrong call.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.