9 Understanding the Agentic Loop
The chapter explains that effective agents are often built as loops, but not all loops live entirely inside the model. It starts from the familiar Sense-Plan-Act-Learn pattern and shows how agentic systems can expand into longer-running workflows by moving parts of the loop outside the agent itself. The central idea is that the developer sets goals, tools, and boundaries, while the agent decides how to proceed at each step, how to judge progress, and when to stop.
The first extension is the task loop, which externalizes planning, state, and termination so an agent can work on long-horizon goals such as deep research. This requires structured state objects, typed iteration outputs, and carefully designed stop conditions like iteration limits, budget ceilings, goal satisfaction checks, quality thresholds, and stagnation detection. The chapter emphasizes that context management matters: state should be serialized selectively so the loop stays focused and does not overwhelm the model, and the research process should use tools like search services while keeping the loop controller deterministic and reliable.
The chapter then moves to meta-loops, where agents control other agents. In orchestration, a central agent delegates sub-tasks to specialized workers and can revise the plan as new information arrives, making it a good fit for complex goals with uncertain decomposition. In collaboration, multiple peer agents share state, critique one another, and refine outputs together, which works best when adversarial review and diverse perspectives improve quality. Across all patterns, the chapter’s main message is to choose loops only when iteration adds value, and to keep exploration, coordination, and final synthesis separated so each part of the system can do its job well.
The core agentic loop. The agent cycles through Sense, Plan, Act, and Evaluate until the goal is satisfied or a termination condition is triggered. Internal state accumulates across iterations, giving each cycle more context than the last.
compares short- and long-horizon goals and shows how this requires promoting the single-agent internal loop to the level-two external task-based agentic loop.
compares level 3 of the agentic loop using an orchestrator controller and the more free form collaboration pattern
shows the external component/code sections that control a deep research agent and how the code functions through each outside step of the agentic loop
workflow of a repetitive task loop agent. The agent pulls items from a task queue, processes each one using tools from MCP servers, records the result, and tracks progress until the queue is empty or a stop condition fires.
shows the orchestrator agent loop flow, how the state and plan are accessed and managed as well as what decisions are made and how worker agents are delegated to.
shows the component workflow for an agent collaboration layer 3 meta agentic loop
Summary
- The agentic loop is the core iteration engine that powers research agents, task agents, and any agent that must iterate toward a goal rather than answer in a single pass.
- Every agentic loop rests on three pillars: a goal (what to achieve), an iteration strategy (what to do each cycle), and termination conditions (when to stop). All three must be explicit in your design.
- State accumulators carry context between iterations. Control what the agent sees at each cycle to manage context window usage by truncating, summarizing, or compressing earlier findings as the loop progresses.
- Strongly typed iteration output (Pydantic models) is non-negotiable for loop agents. The loop controller needs structured data—follow-up questions, goal satisfaction flags, confidence scores—to manage flow control.
- Termination conditions should be layered: hard iteration limits (safety net), budget limits (cost control), goal satisfaction (ideal exit), quality thresholds (evaluator-driven), and stagnation detection (diminishing returns).
- MCP servers provide dynamic tool discovery for loop agents, allowing agents to search the web, access file systems, and call external services without hard-coding @function_tool wrappers. Use MCPServerStdio with async context managers for clean lifecycle management.
- Deep research loops iterate through search-read-synthesize cycles, accumulating findings and generating follow-up questions. Use a separate synthesis agent to compile findings into a polished report after the loop completes.
- Repetitive task loops process work queues with retry logic for transient failures. Track completed, failed, and pending items explicitly, and feed previous errors back to the agent on retries.
- Parallel iteration with asyncio.gather dramatically speeds up task loops when items are independent, but watch rate limits and ensure task independence before enabling it.
- Adaptive iteration budgets let the agent estimate and request iteration counts based on task complexity while respecting hard ceilings set by the developer.
- Never deploy an agentic loop without both a hard iteration limit and a cost ceiling. An unconstrained loop is a production incident waiting to happen.
AI Agents in Action, Second Edition ebook for free