Agentic loops and stop_reason control
How a client-tool agentic loop works on the Messages API and in the Agent SDK: continue on stop_reason tool_use, branch on every other value, append full assistant turns and all tool_result blocks, and keep turn and budget caps as backstops that report the run as incomplete.
Design and implement agentic loops for autonomous task execution
~8 min read · 11 practice questions ·
What the exam expects
- Write loop control that continues on
stop_reason"tool_use"and handles every other value it exits on. - Append each assistant response and its
tool_resultblocks to one runningmessageslist. - Answer parallel tool calls correctly: one
tool_resultpertool_useblock, all in the next user message, nothing after them. - Reject the named anti-patterns: parsing natural-language phrases, treating any text block as completion, or using an iteration cap as the primary stop.
- Choose model-driven tool selection over hard-coded sequences for open-ended work, and read Agent SDK result subtypes.
Key terms: agentic loop (send, inspect stop_reason, run tools, return results, repeat); stop_reason (the response field that says why Claude stopped generating); tool_result (the user-turn block that answers a tool_use block by tool_use_id); ResultMessage (the Agent SDK message that ends the loop, with a subtype); backstop (a turn or budget cap that only catches runaway runs).
Key facts
- Detail
stop_reason == "tool_use"; exit on any other value
stop_reason values- Detail
end_turn,max_tokens,stop_sequence,tool_use,pause_turn,refusal,model_context_window_exceeded
pause_turn- Detail
- Server-tool loop hit its limit (default 10 iterations); send the response back as-is
refusal- Detail
- Returned as a normal HTTP 200;
stop_detailsnames the category
- Detail
- One
tool_resultpertool_use, all in one user message, before any text
- Detail
- Return
tool_resultwithis_error: true; the loop keeps running
- Detail
max_turns(tool-use turns) andmax_budget_usd; no limit by default
result field- Detail
- Present only on subtype
success
Sources for this section (5)
- How tool use worksplatform.claude.com (opens in a new tab)
- Stop reasons and fallbackplatform.claude.com (opens in a new tab)
- Parallel tool useplatform.claude.com (opens in a new tab)
- Tutorial: Build a tool-using agentplatform.claude.com (opens in a new tab)
- How the agent loop workscode.claude.com (opens in a new tab)
- Why must the loop check
stop_reasonrather than whether the reply contains text? - When the response has
stop_reason: "pause_turn", what should the application send next? - How must the loop answer a response with three
tool_useblocks? - Why should code check the Agent SDK
ResultMessagesubtypebefore readingresult? - When does a model-driven agent fit better than a hard-coded tool sequence?
The questions are free to read. Answers with their sources and self-grading are included with the pack.
Keep reading with the Claude Certified Architect - Foundations pack
- Core concepts
- Exam traps
- Worked scenario
The pack includes:
- All 30 lessons of the Claude Certified Architect - Foundations guide, with reading progress
- Interactive decision walks and step-by-step sequence steppers
- Inline practice questions and one-click topic practice (11 questions for this lesson)
- An official source cited for every claim
- An AI tutor that answers from the guide
- Unlimited timed practice exams
Practice for the Claude Certified Architect - Foundations exam
Certifyforce is an independent study resource and is not affiliated with, endorsed by or sponsored by Anthropic. Claude is a trademark of Anthropic.