liveBook · Manning

Chapter 9: Understanding the agentic loop

What you will learn in chapter 9 of AI Agents in Action, Second Edition

The introduction distinguishes the familiar internal SPAL loop from broader agentic looping used for long-horizon goals and multi-agent collaboration.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction distinguishes the familiar internal SPAL loop from broader agentic looping used for long-horizon goals and multi-agent collaboration.

Key terms: SPAL, sense-plan-act-learn, internal agent loop, agentic looping, long-horizon, goal-driven agents

9.1 Peeling back the three agentic loop layers

Introduces the internal SPAL loop as an agent’s core layer and identifies the task and meta loops as two layers that extend its work.

Key terms: internal loop, SPAL, task loop, meta loop

9.1.1 Layer 1: The inner loop (sense-plan-act-learn)

This section explains how an agent’s inner loop cycles through sensing, planning, acting, evaluating, and deciding whether to continue.

Key terms: level-1 agentic loop, internal state, termination condition, agentic, while loop, boundaries, goal, plan, tool, state

9.1.2 Layer 2: The task loop

This section explains how an external task loop lets one agent pursue long-horizon goals by keeping plans and state beyond its short-lived internal cycle.

Key terms: layer-2 agentic loop, goal-plan-state-decision, short-horizon goals, long-horizon goals, context window, long-running iterations, externalizing the agent’s plan and state, programmatic, deterministic constructs, decision, tool

9.1.3 Layer 3: The meta loop

This section defines the agent-controlled meta loop and distinguishes its orchestration and collaboration forms.

Key terms: meta-agentic loop, layer-1, layer-2, meta layer, orchestration-controlled meta loop, collaboration of agents, delegating agent, delegation subagents, decide, state, and plan

9.2 Layer 2: Looping with a deep research agent

This section introduces deep research as a layer-2 agentic loop and explains why external control of state, tools, and stopping decisions matters.

Key terms: deep research agent, layer-2 agentic loop, longer-horizon, state between iterations, termination conditions, external tools, runaway cost generator, code external to the agent, state, plan objects

9.2.1 Creating the initial state and plan

This section introduces the shared research state and plan used across loop iterations and explains how to pass selected parts of them to the agent.

Key terms: ResearchState, ResearchPlan, external state, shared memory, to_context, context window

9.2.2 Adding the tools

This section describes setting up search tools for a research agent and notes that the agent can use other MCP services and tool sources.

Key terms: STDIO, MCP server, Brave Search, BRAVE_API_KEY, .env file, tools, MCP server sources, search, analysis, aggregation

9.2.3 Understanding iteration body output

This section explains how each research-agent iteration produces structured output that updates loop state and helps determine whether another iteration is needed.

Key terms: iteration body, Runner.run(), state/plan, ResearchIteration, strongly typed output, loop controller, follow_up_questions, goal_satisfied, subtopic updates, Agents SDK

9.2.4 The termination gate

Explains how a termination gate uses layered conditions to decide whether a research-agent loop should continue or stop.

Key terms: termination gate, hard limit, budget limit, goal satisfaction, quality threshold, stagnation detection, maximum iteration count, cost ceiling, tokens, latency

9.2.5 Coding the deep research loop

This section assembles the deep research loop and explains three design choices: limiting findings in context, ordering follow-up questions, and cleaning up the MCP server.

Key terms: loop controller, agent instructions, plan, state, stop condition, to_context, findings, context window, follow_up_questions, breadth-first exploration

9.2.6 Synthesizing the final output

The section explains how a separate synthesis agent turns a completed research loop’s findings into a report.

Key terms: research agent, synthesis agent, separation of concerns, findings, all findings, final status, context window pressure, caveat

9.2.7 When to use an agentic loop

This section gives criteria for deciding when an agentic loop helps and when a single pass is better.

Key terms: iterative discovery, progressive refinement, multisource aggregation, batch processing, conditional branching, single pass, latency, cost

9.2.8 Building a repetitive task loop agent

This section explains how a repetitive task loop processes a defined queue and why it does not need an external plan.

Key terms: repetitive task completion, task queue, progress tracker, termination condition, state, decision flow, external plan

9.3 Layer 3: Multi-agent orchestration loops

This section explains how an LLM-driven orchestrator manages a multi-agent loop, why it can adapt its plan, and when its extra cost is justified.

Key terms: layer-3 meta loop, orchestrator agent, worker agents, handoff mechanism, global plan and state, dynamically replan, subtasks, decomposition, worker’s findings, orchestrator overhead

9.4 Building collaborative agentic loops

This section explains how peer agents collaborate through shared state and review, and when that pattern is preferable to orchestration.

Key terms: layer-3 meta loop, collaboration loop, shared state, turn manager, researcher-critic-synthesizer triad, adversarial validation, layer-2 agent loop, layer-3 collaboration meta loop, critic agent, orchestration

9.5 Exercises

Six exercises ask the reader to build and compare research, task, orchestration, and collaboration loops.

9.6 Summary

Summarizes how to design a goal-directed agentic loop, control its iterations safely, and apply it to research and repetitive tasks.

Where this is used later

Concepts explained in this chapter

← Chapter 8: Deploying agents and agentic systems Chapter 10: Exploring the cognitive agent that thinks, monitors, and adapts →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.