Agentic Loops
The loop
- Send messages and tool definitions to Claude.
- Read
stop_reason. - If
tool_use: run every requested tool, append the assistant turn, then append one user message containing only thetool_resultblocks. - Call Claude again. Repeat.
- If
end_turn: the model finished. Use the answer.
The control signal is the structured stop_reason field, not anything in the text.
What each stop_reason means for the loop
stop_reason | Meaning | Loop action |
|---|---|---|
tool_use | Claude is calling tools | Run them, return a tool_result for every tool_use block |
end_turn | Finished naturally | Stop and use the response |
max_tokens | Hit your max_tokens | Raise the limit and retry, or continue; an incomplete tool_use block means retry with a higher limit |
stop_sequence | One of your stop sequences fired | Read stop_sequence to see which |
pause_turn | A server-tool loop hit its iteration limit (default 10) | Send the assistant content back unchanged to continue |
refusal | Claude declined; HTTP 200, not an error | Read stop_details; retry on a fallback model |
model_context_window_exceeded | Response filled the context window | Treat as truncated |
Message rules that cause real bugs
- Each
tool_useneeds a matchingtool_resultin the next user message, and that message should contain nothing else. - Keep the same
toolsarray on the follow-up request. - Adding text after tool results can teach Claude to end its turn: you get
end_turnwith an empty response (2 to 3 tokens). Do not resend the empty turn. If it persists, add a continuation prompt in a new user message. - HTTP errors (4xx/5xx) are separate from
stop_reason; handle them around the request.
Always bound the loop (design guidance)
Add a maximum step count, a cost budget and timeouts. The Agent SDK exposes max_turns and a spend cap (max_budget_usd / maxBudgetUsd); hitting them ends the query with an error result subtype (for example error_max_turns, error_max_budget_usd), and you can resume the session with a higher limit.
Exam traps
- Parsing the assistant's text ("I'm done", a keyword) to decide whether to stop. Wrong: use
stop_reason. - Treating
max_tokensas success. The output is cut off. - Treating
refusalas an exception. It arrives as a normal 200 response. - Unbounded loops. A loop with no step limit is a defect, not a design.
- Skipping a
tool_resultfor one of several paralleltool_useblocks.
References
- Handling stop reasons — checked Oct 16, 2026
- Subagents in the SDK — checked Oct 16, 2026
Check your understanding
A response has
stop_reason: "tool_use"with threetool_useblocks. What do you send next?Show answerHide answerAnswer
One user message with threetool_resultblocks and nothing else.The model returns
end_turnwith empty content right after you sent tool results plus a text hint. Why, and what now?Show answerHide answerAnswer
Text after tool results can teach it to end the turn. Don't resend the empty turn; add a continuation prompt in a new user message.Which signal ends the loop: the word "finished" in the text, or
stop_reason?Show answerHide answerAnswer
stop_reason.