agent loops — tool_use and the request/tool/respond cycle8 / 9
stop_reason, tool_use, tool_result — the loop every agent runs
Build the actual agent loop. Write run_agent(question) that:
- Starts with a
messageslist containing one user turn withquestion. - Loops up to 5 times. Each iteration calls
fake_model(messages). - If
response["stop_reason"]is"end_turn", return the text fromresponse["content"][0]["text"]. - If it's
"tool_use", append the assistant turn AND a user turn containing atool_resultwithcontent="42"(we'll pretend every tool returns 42). Then loop again. - If the cap is hit without
end_turn, return the string"capped".
Then call it once and print the result.
Expected output:
the answer is 42
⌘↵ runs the editor.
Booting Python…
Output
[promptdojo:~]$ _