Module 4 · 5 lessons
Reliability: fallbacks, timeouts and guardrails
What to do when the model is slow, down or wrong: fallbacks, timeouts, safe retries and output checks.
Before you start
From this track: Modules 0 to 3. Each module uses only ideas from the ones before it.
Builds on: Timeouts, retries and idempotency, Circuit breakers and bulkheads
Already know these? Start with the first lesson below.
1. A timeout on every model call
The picture
Stop waiting, start plan B
A first-token timeout cancels a call that has sent nothing, so the user gets a fallback instead of a spinner.
Scene 1 of 4 · A healthy call
A healthy call sends its first words after 0.6 s, so the user sees text almost at once.
Healthy call
- Request sent at 0 s
- Words reach the user at 0.6 s, first words
- First-token limit
- not shown yet
- Whole-answer limit
- not shown yet
- A healthy call. A healthy call sends its first words after 0.6 s, so the user sees text almost at once.
- An overloaded provider. When the provider is overloaded, nothing comes back; with no timeout, the user could wait 45 s.
- Cancel at 3 s. With a 3 s first-token limit, the stuck call is cancelled and the small model answers by 3.4 s.
- A longer whole limit. The whole answer gets a longer limit, 20 s, since a stream that started well can still stall.
- Now you try. Set the first-token limit, and see what happens to an overloaded call and to a slow but healthy one. For example: The overloaded call's user gets the fallback at 1.4 s; the slowest healthy call (2.5 s) is cancelled too. Too short a limit cancels calls that were working, wasting them and adding load from fallbacks.
Scene 1 of 4: A healthy call. A healthy call sends its first words after 0.6 s, so the user sees text almost at once.
Key points
- A healthy call usually starts sending words within a second or two.
- With no timeout, an overloaded provider can keep the user waiting a long time.
- A short first-token limit catches that early and hands over to plan B.
- A longer limit on the whole answer catches a stream that stalls.
A model call can take seconds even when all is well, and much longer when a provider is overloaded. Without a (The time a caller waits before giving up on a call and treating it as failed.), users wait as long as the slowest call.
Pick a timeout from what users will put up with, such as a few seconds for a chat reply. A call that runs past it counts as failed.
Streaming helps: set a short limit for the first (A piece of text that a language model reads or writes, often part of a word. Prices and limits are counted in tokens.), since a fast start usually means the call is healthy. Keep a longer limit for the whole answer, since a stream can stall.
In system design: Every model call needs a timeout, chosen from the user's patience, not from how long the model might need.
Where it goes wrong
- One timeout for every call is too short for long answers and too long for short ones. Set limits for each feature.
- A cancelled call may still be billed for the tokens it already used, so a retry can pay twice.
In an interview
- Give two limits, a short one for the first token and a longer one for the whole answer, and say what happens at each.
Check yourself
Two harder questions
The first uses the idea on a new case. The second asks you to judge a design.
Apply it
Judge a design
Sources and further reading (2)
First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Engineering guideTimeouts, retries, and backoff with jitterAmazon Builders’ LibraryHow Amazon picks a timeout from measured latency, so slow calls fail instead of piling up.
- Official docsErrorsAnthropic docs (one provider)The errors one provider returns, such as overloaded and rate limited, and what each means.
2. Retrying safely
The picture
One retry, then plan B
Retry a brief error once after a short, jittered wait, then fall back; do not repeat a step that changes something without checking first.
Scene 1 of 4 · A brief error
The call fails after 0.3 s with "busy, try again": a brief error that a retry often fixes.
One request
- Call or retry at 0 s, call
- "Busy" error at 0.3 s, busy
- A brief error. The call fails after 0.3 s with "busy, try again": a brief error that a retry often fixes.
- Wait, then retry once. The app waits about 1 s, retries once, and gets "busy" again.
- Then fall back. It stops retrying and moves on to its fallback, instead of keeping the user waiting longer.
- Spread the retries. Without jitter, every client retries at the same moment; with jitter, the retries spread out.
- Now you try. A call failed. Pick what went wrong, and see whether to retry. For example: Do not retry blindly. The email may already have gone, so a blind retry could send it twice; check first.
Scene 1 of 4: A brief error. The call fails after 0.3 s with "busy, try again": a brief error that a retry often fixes.
Key points
- A "busy" error is often gone a second later.
- Retry it once or twice, with backoff, then fall back.
- Jitter spreads many clients' retries apart.
- A step that sends or books something must never be retried blindly.
Many model errors are brief, such as a provider saying it is busy, so one retry with (Waiting longer after each failed attempt before trying again.) often works. The Retries lab shows backoff and (Randomness in how long each client waits before a retry, up to the backoff wait, so clients do not all retry at the same moment.) at work.
But a retry after a timeout may pay for the tokens again, and retrying an action, such as sending an email, may do it twice. Retry only what is safe to repeat.
Cap the retries, and spread them with jitter, so many clients do not hit a struggling provider at the same moment.
Compare retry policies during an outage and a traffic spike, and measure the extra load each one creates.
Open the lab →In system design: Retry brief errors once or twice with backoff and jitter, and never blindly retry a step that changes something.
Where it goes wrong
- Many clients retrying at once can keep a busy provider busy. Use growing waits, a random spread and a cap.
- Retrying a call that changed something, such as sending an email, can do it twice. Send an id the receiver can use to spot repeats, or check first.
In an interview
- Say which errors you retry, how many times and how far apart, and which calls are never retried.
Check yourself
Two harder questions
The first uses the idea on a new case. The second asks you to judge a design.
Apply it
Judge a design
Sources and further reading (2)
First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Engineering guideTimeouts, retries, and backoff with jitterAmazon Builders’ LibraryCapped retries with backoff and random jitter, so clients do not all retry at once.
- Engineering guideMaking retries safe with idempotent APIsAmazon Builders’ LibraryWhy a retried action must not run twice, and how a request token prevents it.
3. Provider rate limits and batch jobs
The picture
- Nightly job — 600 tickets — healthy
- Model API — 60,000 tokens a minute — healthy — NOW — 600 arrive at once
- Requests
- Refused: 429
Step 1 of 3 · Model API
Done: The job sends all 600 requests at once
- What happens
- Each request asks for one ticket summary, and all of them start together.
- Why
- 60,000 tokens a minute ÷ 2,000 tokens a request = 30 requests a minute.
- What changes
- Only 30 fit in this minute.
- Next
- 570 come back as "429 Too Many Requests"
Event log
2 more to go. Press a line to go back to that step.
Each minute: answered, and refused with 429
No minute has run yet.
Step 1 of 3: Model API. The job sends all 600 requests at once. Each request asks for one ticket summary, and all of them start together.
What this simplifies. The limit, the request size and the price ($3 per million tokens) are examples, and so is the half price for batch work. Real limits are often set per minute for both requests and tokens, and they differ by provider and account.
Model providers limit how much each account may send, usually per minute: a number of requests and a number of tokens. A call over the limit is refused with (The rules an app or browser uses to ask a server for something and get an answer, such as GET /profile/42 and then 200 OK.) status 429, "Too Many Requests", often with a Retry-After header that says how long to wait.
Retrying refused calls straight away only adds more refused calls. Instead, count your own tokens and pace the calls a little under the limit, with one limiter that every caller of the key shares. Then a 429 becomes rare, and each one waits as told.
Work nobody is waiting for, such as nightly summaries, can go to a batch API. You send one file of many requests, and the results come back within a set time, often up to 24 hours. Some providers charge much less for it.
Never batch a request a person is waiting on. Keep live chat on the normal API. Move bulk jobs out of its way, so they do not use up the limit that live users need.
In system design: Pace calls under the provider limit with one shared limiter, honour Retry-After, and send bulk work that nobody waits for to a batch API.
Where it goes wrong
- If each server paces itself alone, the total can still go over the account limit. Share one limiter for each key.
- Retrying at once and ignoring Retry-After turns a short limit into a long outage.
In an interview
- Work out how many requests a minute fit under a token limit, and how you keep room for live users.
Check yourself
Two harder questions
The first uses the idea on a new case. The second asks you to judge a design.
Apply it
Judge a design
Sources and further reading (3)
First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- StandardRFC 6585: Additional HTTP Status CodesIETFDefines 429 Too Many Requests and the Retry-After header.
- Official docsRate limitsAnthropic docs (one provider)Real limits on requests and tokens per minute, and the headers that report what is left.
- Official docsBatch processingAnthropic docs (one provider)Sending many requests as one job that finishes later, at a lower price.
4. Falling back when the model fails
The picture
Predict first: which model or saved answer replies, and how long does the user wait? Step through to check.
- User — healthy — NOW
- Application — timer and fallbacks — healthy
- Large model — best answers — healthy
- Small model — faster and simpler — healthy
- Answer cache — answers saved earlier — healthy
- Request
- Error or timeout
- Fallback
- Streamed answer
Step 1 of 8 · User
Done: The user sends a chat message
- What happens
- A user sends a message and waits for the reply.
- Why
- Every second of waiting counts from here, whatever happens behind the application.
- What changes
- The application has the message. The clock for this reply starts at 0 s.
- Next
- Build the request and start a 3 s timer
Event log
7 more to go. Press a line to go back to that step.
Step 1 of 8: User. The user sends a chat message. A user sends a message and waits for the reply.
The whole chain at a glance
- Large model timed out, 3 s
- Small model answered, 0.4 s
- Cached answer not needed
- Try-again message not needed
The small model answers after about 3.4 s in all, so the user gets a shorter, simpler answer.
The large model gets a 3 s timeout. What this simplifies. The timings are made up to show the idea. Real timeouts depend on what users will wait for. A circuit breaker would skip a provider that keeps failing, so later users reach the fallback faster. On a "too many requests" error this app moves on at once. Another app might wait and try the same model again.
A fallback is a plan B: when the main model times out or fails, the app answers another way instead of showing an error.
A common chain is a smaller model, then a cached answer from earlier, then an honest message asking the user to try again.
When a provider fails again and again, a (A switch that stops calls to a failing service for a while, so callers fail fast instead of waiting.) stops calling it for a while, so users go straight to the fallback. The Circuit breaker lab shows this.
Break a dependency and watch the breaker move through closed, open and half-open while protecting both services.
Open the lab →In system design: Design the fallback chain on purpose, and tell users when they get a simpler or older answer.
Where it goes wrong
- A fallback that is never tried fails on the day it is needed. Test the whole chain on purpose.
- A smaller fallback model can answer worse with nobody told. Mark answers that came from a fallback, and measure them.
In an interview
- Lay out the chain in order, and say what the user sees at each step.
Check yourself
Two harder questions
The first uses the idea on a new case. The second asks you to judge a design.
Apply it
Judge a design
Sources and further reading (2)
First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Engineering guideAvoiding fallback in distributed systemsAmazon Builders’ LibraryAmazon’s case that a fallback path can fail when it is needed, and how to test it.
- Official docsErrorsAnthropic docs (one provider)Which failures a fallback must handle: overload, rate limits and server errors.
5. Checking the answer before it is shown
The picture
Check before you show
Plain code checks every answer before the user sees it, and a failed check has a planned next step.
Scene 1 of 4 · The model answers
The page needs the answer in JSON, a strict data format; this time the model sends broken JSON.
- Language model — healthy
- Answer checks — healthy — checking
- User — healthy
| Code check |
|---|
| Valid JSON |
| No phone number but the support line |
| Under 200 words |
- The model answers. The page needs the answer in JSON, a strict data format; this time the model sends broken JSON.
- Code checks it. Plain code runs 3 checks: valid JSON, no phone number but the support line, and under 200 words.
- Ask once more. The JSON check failed, so the app sends the request to the model again rather than show a broken page.
- Then show it. The second answer passes every check, and only then does the user see it.
- Now you try. Pick an answer the model sent, and see which check catches it. For example: "No phone number but the support line" fails, so the app will block the answer and show a safe message. The response to each failure is decided in advance, so an answer that fails a check does not reach the page.
Scene 1 of 4: The model answers. The page needs the answer in JSON, a strict data format; this time the model sends broken JSON.
Key points
- Checks run between the model and the user.
- Code checks are fast and cheap: format, private data, length.
- Each failed check has a planned response.
- A model-based check can add more, but keep the code checks too.
A model can write something false, unsafe or in the wrong format. (A check on what goes into a model or on its answer, such as a format check or a search for private data.) are checks that run on the answer before the user sees it.
Some are plain code: is the answer valid (A plain-text format for data, such as {"id": 42, "name": "Asha"}. Most web APIs send and receive it.), does it include a phone number it should not, is it under the length limit?
Others use a second model to look for problems. That is slower and can itself be wrong, so keep the code checks too. A check catches only what it was written for, so some bad answers still get through.
In system design: Put cheap code checks on every answer, and decide in advance what happens when a check fails.
Where it goes wrong
- A check that blocks too much frustrates users and hides good answers. Measure how often each check fires, and why.
- A check run by a model can be fooled by the same tricks as the main model, so it should never be the only guard.
In an interview
- Say which checks run on every answer, which use code and which use a model, and what happens when one fails.
Check yourself
Two harder questions
The first uses the idea on a new case. The second asks you to judge a design.
Apply it
Judge a design
Sources and further reading (2)
First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Engineering guideOWASP Top 10 for LLM Applications: LLM05 Improper Output HandlingOWASP GenAI Security ProjectWhy model output must be checked like any untrusted input before your code uses it.
- Official docsReduce hallucinationsAnthropic docs (one provider)Checks that run on an answer before it is shown, such as matching it to quotes.