Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 82/ 160
05. AI System Design

Model Fallbacks

Graceful degradation across providers when primary models time out or error.

IntermediateFailover · Timeouts3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidHands-onQuiz (3)Practice

In plain words

If the main AI is slow or broken, the app quickly switches to a backup AI, or a simple message, so the user is never stuck.

Think of it like…

A hospital's backup generator and a second power feed: the main supply is trusted, but there is a tested automatic switch, and the staff regularly run drills so the lights stay on when the grid fails.

Where you’ve seen it

A support assistant calls its primary provider with a 10-second timeout (the normal p95 is 6.8 s). On a 429 or 5xx it retries up to twice with exponential backoff and jitter (0.5 s, then 1 s, randomised), but a retry budget limits retries to 10% of traffic so a brownout doesn't triple the load. After 20 failures in 30 seconds the circuit breaker opens for 60 seconds, then lets a few probe requests through (half-open). The fallback chain is: secondary provider with a pre-tested prompt variant, then a small self-hosted model with a reduced feature set (no tool use), then a canned message with the support phone number and a ticket form. With a primary at 99.5% availability and an independent secondary at 99.5%, the combined availability is 1 − 0.005² = 99.9975%, about 13 minutes of downtime a year instead of 43.8 hours. In practice correlated failures (shared cloud region, shared network) reduce that, which is why the chain ends with a non-model answer.

Words you’ll hear

Circuit breaker
A guard that stops calling a failing dependency and retries it after a cool-down.
Backoff with jitter
Increasing, randomised delays between retries to avoid synchronised retry storms.
Fallback chain
An ordered list of alternatives tried when the primary fails.
Graceful degradation
Reduced capability or quality instead of total failure.

Visual diagram

Loading interactive diagram

Theory & deep dive

Timeouts, retries and circuit breakers

Set timeouts deliberately: a connect timeout (1 to 2 s), a time-to-first-token timeout for streaming, and an overall deadline tied to the user experience; set them above normal p95 (for example 10 s against a 6.8 s p95) but low enough to leave room for a fallback. Retry only errors that may succeed on retry (429, 500, 502, 503, timeouts), never 4xx validation errors or content-policy refusals; honour Retry-After headers; use exponential backoff with full jitter (delay = random(0, base × 2ⁿ)) and cap attempts at 2 or 3. A global retry budget (for example retries ≤ 10% of requests) prevents retry storms from amplifying an outage.

A circuit breaker tracks recent failures per dependency: closed (normal), open (fail fast, skip the dependency for a cool-down such as 60 s) and half-open (let a few probe requests test recovery). Failing fast saves users from waiting out timeouts and gives the provider room to recover. Combine with bulkheads (separate concurrency pools per provider or tenant so one slow dependency can't exhaust all workers) and load shedding (reject or degrade low-priority requests when saturated).

Full-jitter backoff

Base 0.5 s: attempt 1 waits random(0, 0.5 s), attempt 2 waits random(0, 1 s), attempt 3 random(0, 2 s). Thousands of clients retrying after an incident spread out instead of hitting the recovering service in synchronised waves.

Key takeaway: Bound waiting with timeouts, retry only transient errors with jitter and budgets, and fail fast with circuit breakers.

Designing the fallback chain

Order fallbacks by quality retained and independence from the primary: first the same model family through another region or the provider's secondary endpoint, then an equivalent model from a different provider, then a smaller or self-hosted model, then a cached answer (semantic or exact cache) and finally a static response with next steps (a contact option or ticket form). Be honest about degradation ("I can't look up your order right now") rather than guessing. Decide which features survive in each tier: tool calling, structured output and long context often differ across models, so a fallback may need a reduced mode.

Test the fallbacks as seriously as the primary: keep per-model prompt variants and tool schemas, run the golden eval set against every model in the chain (record quality per route), and rehearse failures with chaos tests that block the primary in staging. Monitor fallback rate, per-hop latency and error codes, and alert when fallbacks exceed a threshold, since a silent fallback can hide a quality drop or a cost spike. Check non-technical constraints too: data-processing agreements, regions and privacy rules for each provider, and consistent safety behaviour. Availability maths assumes independence; shared dependencies (cloud region, DNS, authentication, your own code) make real availability lower, so keep the final tier independent of any model.

Availability maths

Primary 99.5% (0.5% downtime = 43.8 hours a year). A truly independent secondary also at 99.5%: both down together 0.005 × 0.005 = 0.0025%, so combined availability 99.9975%, about 13 minutes a year. A shared cloud outage that takes out both breaks the independence assumption, hence the model-free final tier.

Key takeaway: Order fallbacks by retained quality and independence, test every tier and alert on fallback rate.

How it works, step by step

Step 1 of 4
Engineering

1. Set timeouts and retry policy

Define connect, first-token and overall timeouts; retry only transient errors with jittered backoff and a retry budget.

10 s deadline · max 2 retries · budget 10%
Key numbers at this step
Honour
Retry-After

When to use it & trade-offs

What it is

Model fallbacks keep an AI feature working when the primary model is slow, rate-limited, erroring or down. A robust design combines timeouts (so a hung call doesn't hang the user), retries with exponential backoff and jitter (for transient errors), a circuit breaker (stop calling a failing dependency and probe it periodically) and an ordered fallback chain: the same model in another region, an equivalent model from a different provider, a smaller or self-hosted model, a cached answer, and finally a graceful static response. The result is graceful degradation instead of a hard failure.

Why it matters: LLM providers have outages, capacity limits and latency spikes, and an AI feature that depends on one endpoint inherits its availability. A single provider at 99.5% availability is down about 44 hours a year; users experience that as the product being broken. Fallbacks turn provider incidents into small quality or latency dips, but only if the fallbacks are tested, because an untested fallback is usually broken precisely when needed.

When to reach for it

  • Customer-facing AI features with availability targets
  • Systems depending on one external provider
  • Handling rate limits and latency spikes

When to avoid or be careful

  • Retrying non-transient errors
  • Unbounded retries that amplify outages
  • Fallbacks that were never tested

Fallback options

Secondary provider or region
Best for: Preserving full features during provider incidents

Strengths: High quality retained; independent failure domain.

Watch out: Prompt and tool differences; extra contracts and compliance.

Smaller or self-hosted model
Best for: Independence from external APIs

Strengths: Control; works when providers fail.

Watch out: Lower quality; you run the infrastructure.

Cache or static response
Best for: Last resort availability

Strengths: Always available; no model dependency.

Watch out: Limited usefulness; staleness risk.

Interview signals & numbers to know

When this topic comes up

  • “The provider had an incident and our feature was down”
  • “429 rate limit errors”
  • Circuit breaker
  • Graceful degradation
  • Multi-provider failover

Benchmarks & rules of thumb

99.5% → 43.8 h -
yearly downtime of a 99.5% dependency
99.9975% -
combined availability with an independent backup
10% -
example retry budget as a share of traffic

Common mistakes & follow-ups

Common interview mistakes

  • Retrying immediately without jitter and creating a retry storm.
  • Assuming two providers on the same cloud are independent.
  • Letting fallbacks silently serve a worse model with no alerting.

Likely interviewer follow-ups

  • How would you design failover between two LLM providers?
  • What does a circuit breaker do?
  • How do you test fallback paths?
Interview tip: End your chain with a non-model response and say why. It shows you understand correlated failures, not just redundancy.
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Model Fallbacks with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
Model Routing
Next topic
Semantic Caching