Lesson 63.3 · 3 of 4 · 4 min read · Reviewed 24 Nov 2026
Graceful fallback
By the end, you can
- Design fallbacks for when the main path fails.
- Degrade gracefully instead of failing completely.
Something will fail: a rate limitRate limit: A cap on how many requests or tokens you can use in a period, such as per minute., an outage, a slow tool, a malformed answer. Decide in advance what happens.
Fallback options
| Failure | Fallback |
|---|---|
| Primary model overloaded | Retry, then a different model or region |
| Tool down | Explain, offer a follow-up or a human |
| Output fails validation after retries | Review queue (14.4) |
| Budget or turn limit reached | Stop, report progress so far |
| Low confidence | Ask a clarifying question, or escalate (45.3) |
Degrade, don’t break
A partial answer with a clear note beats an error page: “I couldn’t check live stock right now. Based on this morning’s data it was in stock. Want me to message you when I can confirm?”
Never fill gaps with guesses
The worst fallback is a confident made-up answer. Make “I don’t know” and “I couldn’t check” acceptable outcomes in your prompts and evalsEval: A repeatable test that measures how well an AI system performs on a set of cases with defined success criteria..
Test the fallbacks
Fallback paths run rarely, so they break quietly. Use fault injection, like the Playground’s failure lab, to exercise them.
Key points
- Plan what happens when the model, a tool or a dependency is slow or down.
- Fallbacks include a different model, a cached answer, a simpler path, or a hand-off to a person.
- Tell users honestly what happened; don't make up an answer.
Add a fallback chain to an answer
Answer a stock question when the live tool is down, using a cache or a hand-off instead of a guess.
You will have: An answer_stock() function with a live, cached and hand-off path, plus a log of fallback events.
Steps
- Run the starter: it answers 'in stock' with source 'guess', which is the worst fallback.
- Try the live tool first and catch its failure.
- Use the cached value with its timestamp and an honest note when one exists.
- Hand off to a person when there is nothing to fall back on, and log every fallback.
Done when
- SKU-7 is answered from the cache with 'as of 08:00 today'.
- SKU-9 is answered with a hand-off, not a stock level.
- Two fallback events are logged.
Open in the Playground No API key needed.
Starter code (Python)
# Degrade gracefully: try the main path, then fallbacks, and say honestly what happened.
class Overloaded(Exception): pass
class ToolDown(Exception): pass
def primary_model(q): raise Overloaded("529 overloaded")
def backup_model(q): raise Overloaded("region busy")
def live_stock(sku): raise ToolDown("stock API down")
CACHE = {"SKU-7": {"in_stock": True, "as_of": "08:00 today"}}
def answer_stock(sku):
# TODO: try live_stock; on ToolDown use CACHE with an honest note; if no cache, say you could not check
# TODO: never invent a stock level
return {"text": "It is in stock.", "source": "guess"}
for sku in ["SKU-7", "SKU-9"]:
r = answer_stock(sku)
print(sku, "| source:", r["source"], "|", r["text"])
Show a solution
class Overloaded(Exception): pass
class ToolDown(Exception): pass
def primary_model(q): raise Overloaded("529 overloaded")
def backup_model(q): raise Overloaded("region busy")
def live_stock(sku): raise ToolDown("stock API down")
CACHE = {"SKU-7": {"in_stock": True, "as_of": "08:00 today"}}
EVENTS = []
def answer_stock(sku):
try:
s = live_stock(sku)
return {"text": "In stock now." if s["in_stock"] else "Out of stock now.", "source": "live"}
except ToolDown as e:
EVENTS.append(("fallback", sku, str(e)))
if sku in CACHE:
c = CACHE[sku]
state = "in stock" if c["in_stock"] else "out of stock"
return {"text": f"I couldn't check live stock right now. As of {c['as_of']} it was {state}. Want a message when I can confirm?", "source": "cache"}
return {"text": "I couldn't check stock right now. I can ask a colleague to confirm and get back to you.", "source": "handoff"}
for sku in ["SKU-7", "SKU-9"]:
r = answer_stock(sku)
print(sku, "| source:", r["source"], "|", r["text"])
print("fallback events logged:", len(EVENTS))