promptdojo_

stop_reason, tool_use, tool_result — the loop every agent runs — step 8 of 9

Build the actual agent loop. Write run_agent(question) that:

  • Starts with a messages list containing one user turn with question.
  • Loops up to 5 times. Each iteration calls fake_model(messages).
  • If response["stop_reason"] is "end_turn", return the text from response["content"][0]["text"].
  • If it's "tool_use", append the assistant turn AND a user turn containing a tool_result with content="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

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