agent loops — tool_use and the request/tool/respond cycle9 / 9
multi-step tools — when one tool isn't enough
Checkpoint
One last thing before we move on. Same surface as a write step — but the lesson doesn't complete until this passes.
Final drill. Build a research agent with three tools and parallel
tool calls. Write run_research_agent(question, max_iterations)
that:
- Maintains a
messageslist, starting with one user turn. - Loops up to
max_iterationstimes, callingfake_model(messages). - On
end_turn: return{"ok": True, "answer": <text>, "rounds": <iterations used>, "tools_called": <total tool runs across all turns>}. - On
tool_use: handle ONE OR MORE tool_use blocks per turn. For each, dispatch throughTOOLS. If the tool name isn't in the registry, the tool result content should be"unknown tool: <name>"(don't crash). Each tool you successfully run counts towardtools_called. Append the assistant turn + one user turn with all tool_result blocks. - If
max_iterationsis exhausted withoutend_turn: return{"ok": False, "answer": "capped", "rounds": max_iterations, "tools_called": <count so far>}.
Two cases run for you. Expected output:
ok=True rounds=3 tools=3 answer=found 2 sources on ramen
ok=False rounds=2 tools=2 answer=capped
⌘↵ runs the editor.
Booting Python…
Output
[promptdojo:~]$ _