Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 4.8 — Playbook: Typed LLM Calls
LLM Engineering · Example 2 of 2

Recipe: typed_call with repair, fallback and counters

The model is a parameter: scripted here, a real client in production. max_attempts is 2 so the fallback case is visible.

The recipes of the whole phase in one place: parse a reply into a value or a list of problems, wrap it in one typed-call function that repairs and falls back, and count what happens. Copy these into any LLM feature.

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 summarise_stats(stats, total) that returns first_try=A repaired=B fallback=C followed by repair rate: B of total as two lines, given a stats dictionary with those keys.

02

Write safe_label(parse_result, allowed) that takes (value, problems) and returns value['label'] if the value exists and its label is in allowed, otherwise "unknown".

Explain it without notes

01

Describe the typed-call recipe step by step.

02

Which counters would you put on a typed call, and what does each tell you?

03

How would you adapt this phase's recipes to a model that does not offer JSON mode?

04

Where does validation end and evaluation (Phase 9) begin?

Recipe: typed_call with repair, fallback and counters
Sign in to run this example in your browser.

Expected output

request 1: label=bug
request 2: label=billing
request 3: label=feature
request 4: label=unknown (fallback)
request 5: label=bug
first_try=3 repaired=1 fallback=1 calls=7
extra calls caused by repairs and fallbacks: 2