liveBook · Manning

Processing loop

As explained in Build a Multi-Agent System (from Scratch)

A processing loop is a repeated execution structure in which an LLM agent plans, invokes tools, evaluates results, and continues through successive steps until a stopping condition is met.

A processing loop executes an LLM agent’s task through repeated planning and tool use. At each step, the agent uses the task instruction and progress so far to decide what to do next. It can adapt its plan when an earlier approach fails and continue until the task is complete or another stopping condition is met. section 1.3 section 4.2.3

Finding the best-value croissant

An agent first searches for bakeries, prices, and ratings. It then uses a code interpreter to analyze the returned information and produces a final recommendation. Each plan, tool call, and result contributes to the task’s trajectory. section 1.3

One implementation divides execution into discrete sub-steps. LLMAgent.run() creates a TaskHandler, schedules the loop as a background asyncio.Task, and returns the handler. The handler is an asyncio.Future that can be awaited for the eventual TaskResult. section 4.2.3 section 4.5

The handler coordinates each cycle:

  • get_next_step() receives the previous TaskStepResult, if any. On the first cycle it creates a TaskStep from the task instruction; later it returns either another TaskStep or a final TaskResult. section 4.2.3
  • run_step() executes a TaskStep, including any tool calls, and returns a TaskStepResult. The loop passes that result back into the next planning decision. section 4.2.3
  • The loop ends on a TaskResult, an irrecoverable error, or a configured maximum number of sub-steps. section 4.2.3
Figure 4.9 The process for executing tasks using the run() method. When a task is submitted, the helper TaskHandler is created to manage task execution on behalf of the LLMAgent.

Follow how run() delegates task execution to a handler that coordinates the loop. section 4.2.3

The handler maintains a step counter and a rollout, also called a trajectory. After each sub-step, it formats the step’s chat history and appends it to the rollout. This record of plans, tool calls, and results helps debug, evaluate, and improve the agent; it is typically not included in the result shared with the user. section 4.2.3 section 1.3

The boundaries of a sub-step are a design choice: other loops, such as ReAct, define steps differently. An optional human-approval extension can also change completion: a rejected TaskResult becomes feedback for a new step, and the loop resumes until a result is approved. section 4.5 section 8.3.1

How Build a Multi-Agent System (from Scratch) covers it

How deep it goes
Implements asynchronous orchestration, step decisions, tool execution, stopping conditions, and rollout recording, then extends the loop with result approval.
Examples
Uses a bakery search and price-rating analysis, plus a Hailstone sequence task with human result approval.
What it assumes
Assumes existing LLM and tool interfaces, then builds the agent’s task orchestration and supporting structures.
What you can do after
Build a processing loop from scratch using task and step structures, a handler, and asynchronous execution.
How it connects
Connects LLM planning, tool calling, trajectories, ReAct, and human-in-the-loop approval.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Multi-Agent System (from Scratch). The section links point to the parts of the book it draws on.