Agentic Architecture & Orchestration

The Agentic Loop

9 min de leitura

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:

  1. You send the conversation (system prompt, user goal, prior tool_use / tool_result blocks).
  2. Claude responds. If it wants to act, it emits one or more tool_use blocks and stops with stop_reason: "tool_use".
  3. Your code executes the tool and appends the result as a tool_result block in a new user message.
  4. 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:

python

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_reason is no longer tool_use, or when you hit a guardrail like max_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_reasontool_use. Always bound the loop with a turn/budget cap.