Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 6.9 — Fallbacks, Deadlines and Circuit Breakers
LLM Engineering · Example 2 of 2

A circuit breaker by hand

The provider is down from t=2000 to t=10000. One call is attempted per simulated second.

When your main model is slow or down, a fallback chain tries the next model within a deadline, a circuit breaker stops you calling a provider that keeps failing, and graceful degradation gives users something useful instead of an error.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write first_working(models, call) that tries each model name in order using call(name) (which returns a string or raises RuntimeError) and returns (name, reply) for the first that works, or None. Test it with a call that fails for "a" and works for "b".

02

Write a Breaker class with failures and is_open(): it opens once failures reaches 2, and success() resets the count. Print is_open() after 1 failure, 2 failures, and after success().

Explain it without notes

01

How does a fallback chain work, and which failures should trigger the next model?

02

Why do you need a deadline as well as a per-attempt timeout?

03

Describe the three states of a circuit breaker and when it moves between them.

04

What is graceful degradation and what are two examples for an LLM feature?

A circuit breaker by hand
Sign in to run this example in your browser.

Expected output

t=0: ok, state closed
t=1000: ok, state closed
t=2000: failed, state closed
t=3000: failed, state closed
t=4000: failed, state open
t=5000: skipped (no call made), state open
t=6000: skipped (no call made), state open
t=7000: skipped (no call made), state open
t=8000: skipped (no call made), state open
t=9000: failed, state open
t=10000: skipped (no call made), state open
t=11000: skipped (no call made), state open
t=12000: skipped (no call made), state open
t=13000: skipped (no call made), state open
t=14000: ok, state closed
t=15000: ok, state closed
t=16000: ok, state closed