Free sample lesson ยท Claude Architect Certification Prep
Agentic Loops
Why does stop_reason beat every 'I'm done' check?
Lesson 1 of the Wondering course Claude Architect Certification Prep
Overview of Agentic Loops
Mastering stop reasons and tool-result continuation ensures your Claude agents execute reliable, deterministic lifecycles without stopping early.
The Core Agentic Loop
An agentic loop is the deterministic execution cycle behind every Claude agent. It is defined in code rather than prompts.
This cycle follows four steps. First, send a request to the Messages API. Second, receive the response. Third, execute tools if requested. Fourth, repeat until the task is complete.
- Send Request: Send the initial request to the Messages API.
- Receive Response: Receive Claude's response including any tool use requests.
- Execute Tools: Execute the requested tools in your code environment.
- Repeat Cycle: Continue the loop until the task is marked complete.
Getting this lifecycle right is essential for Domain 1 of the Architect exam. It ensures your agent does not stop halfway through a task.
Mastering the Stop Reason
The stop_reason field is the only authoritative signal for loop control. It tells your code whether to continue or finish.
If the reason is tool_use, the agent must execute the tool and continue the loop. If the reason is end_turn, the task is complete.
Do not rely on text parsing. Checking if the response contains phrases like task complete is not reliable. Claude might return text and a tool request in the same message.
Handling Tool Results
Executing a tool is not enough. You must append the tool_result to the conversation history.
Claude needs this new evidence to reason about the next step. If you do not send the result back, the agent will repeat work or ignore the data.
- 1. Execute Tool: Run the order lookup function with the provided ID.
- 2. Append Result: Format the tool output and append it into the conversation history.
- 3. Resume Request: Send the updated conversation history back to Claude.
Imagine a support agent looking up an order. The lookup result must be returned as content before Claude can draft the final customer answer.
Avoiding Common Exam Traps
Many developers mistakenly check if the response content type is text to end a loop. This is a mistake because Claude often sends text alongside tool blocks.
Another trap is setting arbitrary iteration caps. Stopping after exactly 10 loops might cut off useful work or waste resources.
Focus on the stop_reason and proper result continuation. These mechanisms ensure production safety and reliability in agentic architectures.
- Anti-Patterns
- Best Practices
Putting It All Together
Check yourself
In the Messages API, _____ is the authoritative signal used to determine whether an agent loop should continue or finish.
- stop_reason
- content_type
Answer: stop_reason
The stop_reason field is the authoritative signal for loop control (e.g., tool_use to continue, end_turn to finish). Parsing text content or checking content types is unreliable.
The four steps of an agentic execution cycle occur in order: first _____, second _____, third _____, and fourth _____.
Answer: The four steps of an agentic execution cycle occur in order: first Send Request, second Receive Response, third Execute Tools, and fourth Repeat Cycle.
Every agentic loop follows this exact 4-step execution order in code: sending the request, receiving the API response, running requested tools, and repeating until stop_reason indicates completion.
Why must tool execution results be formatted and appended back into the conversation history before sending the next API request?
- It provides the necessary evidence for Claude to reason about the next step.
- It automatically sets the stop_reason field to end_turn.
- It prevents Claude from sending text and tool blocks in the same message.
- It bypasses the requirement to monitor API response fields.
Answer: It provides the necessary evidence for Claude to reason about the next step.
Claude requires tool results in the conversation context as evidence to reason about subsequent steps. If tool outputs are not returned, the model cannot see the results and may repeat work or ignore data.
Keep going
This is lesson 1 of 30 in Claude Architect Certification Prep. Next up:
- Multi-Agent Orchestration
- Subagent Context Passing
- Workflow Gates and Handoff
- Agent SDK Hooks
- Task Decomposition Strategies
Wondering turns any topic into short lessons you can read, watch, or listen to, with practice questions and an AI tutor.