1.1 Design and implement agentic loops for autonomous task execution
Implement the request → inspect `stop_reason` → execute tools → append results → repeat loop, let the model decide the next action from context, and avoid text-parsing, iteration-cap and content-shape termination anti-patterns.
Key points
- 1
The agentic loop lifecycle: send the messages plus
toolsto Claude, inspectstop_reason, and if it is"tool_use"run everytool_useblock in the response, append the results, and call the model again. Whenstop_reasonis"end_turn"the model has finished and the loop returns the reply. - 2
Tool results go back as
tool_resultcontent blocks inside ausermessage, each carrying thetool_use_idof the request it answers. They must immediately follow the assistant'stool_usemessage and come first in that user message; any text goes after them. - 3
Keep the whole message history intact between iterations. The history is how the model incorporates new information into its reasoning about the next action; dropping or reordering it removes context and can produce API errors about unmatched
tool_useids. - 4
A response can contain a
textblock and atool_useblock together, and Claude may request several tools in one response. Never inspect only the first block or treat the presence of text as a completion signal. - 5
Return tool failures as
tool_resultblocks withis_error: trueand an instructive message (what went wrong, what to try next). Claude will adapt or retry; silently dropping the result or raising out of the loop prevents recovery. - 6
stop_reasonhas values beyond"tool_use"and"end_turn", notably"max_tokens"(output truncated) and"pause_turn"(long server-tool turns). Branch on each explicitly; a loop written as "continue unless end_turn" resends an unchanged request forever when the model stops for another reason. - 7
Model-driven decision-making means Claude chooses which tool to call next from the conversation and tool results. Pre-configured decision trees or fixed tool sequences work only when the path is known in advance; for high-ambiguity work (support requests, open-ended investigation) they waste calls and fail on unexpected cases.
- 8
Anti-patterns the exam targets: parsing natural-language signals ("Resolution complete", "DONE") to end the loop; using an arbitrary iteration cap as the primary stopping mechanism; checking for assistant text content as a completion indicator; and raising the cap instead of finding out why runs need so many steps.
- 9
Bounds are still required: keep a turn cap or budget (
maxTurns,maxBudgetUsdin the Agent SDK) as a guardrail against runaway loops, log runs that hit it, and treat those as diagnostics rather than normal exits. - 10
Reserve deterministic control for the few invariants that must always hold (for example verified identity before a refund) and enforce those with hooks or gates, leaving everything else to the model's judgment within the loop.
- 11
"Building effective agents": start simple, add agentic behaviour only when it measurably helps, ground each step in environment feedback (tool results), and include stopping conditions and checkpoints for human review.
Read the source
Test yourself on 1.1 Design and implement agentic loops for autonomous task execution
Ten questions, with the answer and explanation after each one.