Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 4.6 — Repair and Retry Loops
LLM Engineering · Example 1 of 2

A repair loop with a maximum and a fallback

The fake model is scripted, so each scenario is repeatable. In production the model call is a real client behind the same function.

When a reply fails validation, tell the model what was wrong and ask again, up to a fixed number of attempts, then fall back to a safe value. This fixes most failures at the cost of extra calls, so count the attempts and the money.

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 retry_until_valid(model, check, max_attempts) where model() returns text and check(text) returns an error string or None. Return the first valid text, or None after max_attempts calls.

02

Write feedback(problems) that returns the message to send to the model: That reply was rejected: p1; p2. Reply again with only the corrected JSON.

Explain it without notes

01

Why does sending the validation error back to the model help?

02

How do you choose max_attempts?

03

What should a fallback look like, and why must it be flagged?

04

What does a repair loop cost, and how would you reduce it?

A repair loop with a maximum and a fallback
Sign in to run this example in your browser.

Expected output

fine first time        attempts=1 plan=pro chars_sent=67 fallback=no
fixed after feedback   attempts=2 plan=pro chars_sent=273 fallback=no
  rejected: "plan" must be one of free, pro, got "gold"
never fixed            attempts=3 plan=free chars_sent=537 fallback=yes
  rejected: the reply is not valid JSON
  rejected: the reply is not valid JSON
  rejected: the reply is not valid JSON