promptdojo_promptdojo_promptdojo_promptdojo_promptdojo_promptdojo_promptdojo_promptdojo_promptdojo_promptdojo_promptdojo_

Validate tool inputs — when the model invents arguments — step 9 of 9

Checkpoint

One last thing before we move on. pass this to mark the lesson done, or skip and keep moving. hop to the next when you're ready.

Final drill. Build the agent loop with full validation. Write run_validated_agent(question, max_iters) that:

  • Maintains messages = [{"role": "user", "content": question}].
  • Loops up to max_iters times calling fake_create(messages) → a MockMessage with .stop_reason and .content (a list of MockTextBlock and/or MockToolUseBlock).
  • On end_turn: collect text from text blocks, return {"ok": True, "answer": <text joined with " ">, "validation_errors": <count>, "iters": <iter num>}.
  • On tool_use: for each tool_use block, call dispatch_with_validation(block.name, block.input). Track how many results came back as VALIDATION_ERROR: or UNKNOWN_TOOL: — increment a validation_errors counter per such result. Append the assistant turn + a user turn with tool_result blocks (each with the right tool_use_id).
  • On cap: return {"ok": False, "error": "capped", "iters": max_iters}.

Expected output:

ok=True iters=3 validation_errors=1 answer=Found a 2026 ramen guide.

full-screen editor opens — close anytime to keep reading.