Overview

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.

FAQ

What are the three layers of the agentic loop, and how do they differ?The three layers are: layer one, the inner Sense-Plan-Act-Learn loop inside a single agent; layer two, the task loop that externalizes plan, state, and decision logic to support long-horizon work; and layer three, the meta loop where an agent or group of agents controls other agents or processes. Each layer adds more control and coordination outside the core agent.
What makes the inner agentic loop different from a normal programming while loop?Unlike a regular while loop, the agentic loop is driven by the agent’s own decisions at each step. The agent decides how to proceed, which tool to use, whether the result is sufficient, and when to stop. The developer sets the goal, tools, and boundaries, but the agent controls the iteration.
Why does layer two externalize state and plan outside the agent?Layer two externalizes state and plan to support longer-horizon goals without overwhelming the model’s context window. By storing plan and state externally, the system can persist progress across many iterations, enabling tasks like deep research or multi-step analysis that would otherwise exceed the agent’s internal limits.
What is the purpose of a ResearchState and ResearchPlan in a deep research loop?ResearchState acts as shared memory across iterations, storing the goal, findings, sources, follow-up questions, and loop status. ResearchPlan stores the strategy, sub-topics, and progress through the research. Together, they let the loop maintain continuity and steer future iterations.
Why is strongly typed iteration output important in an agentic loop?Strongly typed output makes loop control reliable. Instead of parsing fragile free-text responses, the controller can directly read fields like summary_of_findings, follow_up_questions, and goal_satisfied. This makes it easier to update state, decide whether to continue, and avoid errors in termination logic.
What termination conditions should be used to stop a research loop safely?Good termination conditions are layered: a hard iteration limit, a budget limit, goal satisfaction, a quality threshold, and stagnation detection. The hard limit and budget limit are essential safeguards, while the others help stop the loop when the agent has converged or is no longer making meaningful progress.
How do you detect stagnation in an agentic loop?Stagnation can be detected by comparing recent iteration outputs for semantic similarity. If consecutive summaries are too similar, such as exceeding a cosine similarity threshold for multiple iterations, the loop should terminate because additional iterations are not adding value.
When should you use orchestration instead of collaboration in a multi-agent loop?Use orchestration when a complex goal can be broken into independent sub-tasks handled by specialists. Use collaboration when the sub-tasks are interdependent and benefit from critique, shared reasoning, and multiple perspectives. Orchestration is simpler; collaboration is better for adversarial review and refinement.
What is the main difference between orchestration and collaboration in layer three?In orchestration, one agent delegates work to other agents and makes the final decisions. In collaboration, multiple agents work as peers, sharing state and taking turns contributing, critiquing, and refining the result. Orchestration is hierarchical; collaboration is more peer-to-peer.
Why is it recommended to separate the research agent from the synthesis agent?The research agent should focus on exploring, searching, and discovering information, while the synthesis agent should focus on compiling and structuring the final report. Keeping them separate avoids biasing the research process toward report formatting and helps preserve search quality and clarity of responsibility.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • AI Agents in Action, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • AI Agents in Action, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • AI Agents in Action, Second Edition ebook for free