Command Palette

Search for a command to run...

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

What repairs cost, with example prices

Prices are example numbers. Read your provider's price page and keep prices in configuration.

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?

What repairs cost, with example prices
Sign in to run this example in your browser.

Expected output

1 attempt(s): 0.000240 dollars
2 attempt(s): 0.000545 dollars
3 attempt(s): 0.000915 dollars
1,000 requests with repairs: 0.2620 dollars
1,000 requests if all were fine: 0.2400 dollars
repairs add 9 percent