agent loops — tool_use and the request/tool/respond cycle8 / 9
multi-step tools — when one tool isn't enough
Build the multi-tool agent. Write run_agent(question) that:
-
Starts with a
messageslist containing one user turn withquestion. -
Loops up to 5 times, calling
fake_model(messages)each iteration. -
On
stop_reason == "end_turn": returnresponse["content"][0]["text"]. -
On
stop_reason == "tool_use": there may be ONE OR MOREtool_useblocks inresponse["content"]. For each one, look up the tool in theTOOLSregistry and call it with**block["input"]. Build a list oftool_resultblocks (each with the matchingtool_use_id) and append both turns:- The full assistant turn (
{"role": "assistant", "content": response["content"]}) - One user turn whose
contentis the list of tool_result blocks.
- The full assistant turn (
-
If the cap is hit without
end_turn, return"capped".
Then call it once and print the result.
Expected output:
found: Tokyo's best ramen shops, 2026 guide.
⌘↵ runs the editor.
Booting Python…
Output
[promptdojo:~]$ _