Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 7.9 — Playbook: A Safe Tool Runner
LLM Engineering · Example 1 of 1

The safe tool runner and its loop

The timeout is simulated with numbers. Real code enforces the deadline while waiting (Topic 7.6). In Java the course's Json class stands in for Jackson.

One small class that does what this phase taught: a registry, schemas, argument validation, confirmation for writes, a timeout, errors fed back, an audit log, and a step-limited loop around it. Copy it, then plug the real model in.

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(audit) that takes audit lines like "c1 get_order ok" and returns a dict counting outcomes (the last word of each line).

02

Write specs_for(registry) where registry maps a name to a dict with description and schema, returning the tools list in the API shape, in registry order.

Explain it without notes

01

What order of checks should a tool runner apply, and why?

02

Why is the confirmation function a parameter of the runner and not part of each tool?

03

How do you move from the scripted model to a real one safely?

The safe tool runner and its loop
Sign in to run this example in your browser.

Expected output

tools sent to the model: get_order, refund_order, slow_report
run 1: a typo, then a refund the user approves
  step 1: get_order -> {"error":"order_id: expected string"}
  step 2: get_order -> {"order_id":"A17","status":"shipped","total":40}
  step 3: refund_order -> {"refunded":20,"order_id":"A17"}
  final: Refunded 20 on order A17.
run 2: the user declines
  step 1: refund_order -> {"error":"the user declined this action"}
  final: Understood, I did not refund order A18.
run 3: a model that never stops
  step 1: slow_report -> {"error":"timed out after 1000 ms"}
  step 2: slow_report -> {"error":"timed out after 1000 ms"}
  step 3: slow_report -> {"error":"timed out after 1000 ms"}
  final: stopped: step limit 3 reached
audit log: c1 get_order invalid; c2 get_order ok; c3 refund_order ok; c4 refund_order declined; c5 slow_report timeout; c6 slow_report timeout; c7 slow_report timeout