Study notes · 3.9% of the exam

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. 1

    The agentic loop lifecycle: send the messages plus tools to Claude, inspect stop_reason, and if it is "tool_use" run every tool_use block in the response, append the results, and call the model again. When stop_reason is "end_turn" the model has finished and the loop returns the reply.

  2. 2

    Tool results go back as tool_result content blocks inside a user message, each carrying the tool_use_id of the request it answers. They must immediately follow the assistant's tool_use message and come first in that user message; any text goes after them.

  3. 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_use ids.

  4. 4

    A response can contain a text block and a tool_use block 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. 5

    Return tool failures as tool_result blocks with is_error: true and 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. 6

    stop_reason has 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. 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. 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. 9

    Bounds are still required: keep a turn cap or budget (maxTurns, maxBudgetUsd in the Agent SDK) as a guardrail against runaway loops, log runs that hit it, and treat those as diagnostics rather than normal exits.

  10. 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. 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.

Test yourself on 1.1 Design and implement agentic loops for autonomous task execution

Ten questions, with the answer and explanation after each one.