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
