Agentic loop
Definition
The core control loop where Claude requests tool calls, your code executes them and returns results, and the conversation is resent until Claude answers naturally — while stop_reason == 'tool_use': execute tools → append tool_result → resend.
Key points
- Only
stop_reason: "tool_use"continues the loop; every other stop reason exits it. See Stop reasons. - Default
tool_choiceisauto: each turn Claude decides to call a tool or respond directly. Loop ends when Claude answers naturally (stop_reason: "end_turn"). - On
tool_use: response contains one or moretool_useblocks; your code executes the operation and sends back atool_result(in a user message), then resends the whole conversation. - Steerable via system prompt (e.g. "Always call a tool first before responding.").
- Client SDK: you run the loop yourself. Agent SDK: Claude runs the gather→act→verify loop inside
query()— same tools, agent loop, and context management that power Claude Code. - Applies to Tool execution types (client vs server): only client tools require you to build the
tool_resultand continue the loop.
Why it matters for the exam
- D1 is 27% of scored content and the loop is its backbone; expect questions asking which
stop_reasoncontinues vs exits, and who runs the loop (Client SDK vs Agent SDK).
Common gotchas
- Every non-
tool_usestop reason ends the loop — includingpause_turn, which requires a special resend rather than tool execution (see pause_turn handling). - All parallel
tool_useblocks in one response must be answered with all theirtool_resultblocks in a single following user message.
See also
Sources
Referenced by
Practice questions optional · AI
Generate fresh practice questions about this concept with AI. These are not vault-verified.