智能体循环
An agent is not a single prompt — it is a loop. The model is given a goal and a set of tools, then repeats: gather context → take action → verify work → repeat until the task is done. The Claude Agent SDK runs the same loop that powers Claude Code.
The mechanics
Each iteration is one round-trip to the Messages API:
- You send the conversation (system prompt, user goal, prior
tool_use/tool_resultblocks). - Claude responds. If it wants to act, it emits one or more
tool_useblocks and stops withstop_reason: "tool_use". - Your code executes the tool and appends the result as a
tool_resultblock in a newusermessage. - You call the API again. The model sees the result and either calls another tool or produces a final answer with
stop_reason: "end_turn".
The model never executes tools itself — it only requests them. The loop is your responsibility:
Why this shape matters
The loop is what makes an agent self-correcting. After each action the model sees real results — a failing test, an empty search, a 404 — and can adapt its next step. A one-shot prompt cannot do this; it commits to a plan before seeing any feedback.
Three properties follow directly from the loop:
- Context accumulates. Every tool call and result is appended, so the window grows each turn. Long loops eventually need compaction or summarization (see Domain 5).
- Termination is explicit. The loop ends when
stop_reasonis no longertool_use, or when you hit a guardrail likemax_turns/ a budget cap. Never assume the model will stop on its own — always cap turns. - Verification is a step, not an afterthought. Strong agents verify their own work (re-read a file, re-run a test) as an explicit action inside the loop rather than trusting the first result.
Single agent vs orchestration
This same loop scales up. A coordinator agent's "take action" step can be spawning a subagent, which runs its own internal loop and returns a summary. The orchestration patterns in the rest of this domain are all variations on composing agentic loops.
Exam focus: Know the four-phase loop (gather context → take action → verify → repeat) and that the application, not the model, executes tools and feeds back tool_result. The loop terminates when stop_reason ≠ tool_use. Always bound the loop with a turn/budget cap.