liveBook · Manning

Stopping criterion

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

A stopping condition is a rule that determines when iterative task execution must terminate.

A stopping criterion determines when an agent’s iterative task execution ends. The loop continues through sub-steps until the task is complete or a limit—such as a maximum number of steps—is reached. Irrecoverable errors can also stop execution. section 1.3 section 4.2.3

In an LLMAgent, TaskHandler alternates between choosing the next step and running it. The run() method accepts max_steps; reaching that limit raises MaxStepsReachedError. section 4.2.3

Croissant search task

An agent searches for bakeries, analyzes prices and ratings, then concludes when it has enough information. A maximum-step limit provides another endpoint if execution continues. section 1.3

Listing 4.13 Implementing LLMAgent: run()
# llm_agents_from_scratch/agent/llm_agent.py
… #A
from llm_agents_from_scratch.errors import (
    MaxStepsReachedError,
)
 
   class LLMAgent:
    … #B
 
    def run(
        self,
        task: Task,
        max_steps: int | None = None,
    ) -> TaskHandler:
        """Agent's processing loop for executing tasks."""
 
        task_handler = self.TaskHandler( #C
            llm_agent=self, task=task,
        )
 
        async def _process_loop() -> None: #D
            """The processing loop for task execution."""
            step_result = None
            while not task_handler.done():
                try:
                    if task_handler.step_counter == max_steps: #E
                        raise MaxStepsReachedError("Max steps reached.")
 
                    # planning
                    next_step = (await 
                       task_handler.get_next_step(step_result) #F
                    )
 
                    match next_step:
                        case TaskStep():
                            step_result = await task_handler.run_step( #G
                                next_step,
                            )
                        case TaskResult():
                            task_handler.set_result(next_step) #H
 
                except Exception as e:
                    task_handler.set_exception(e) #I
 
        task_handler.background_task = (
            asyncio.create_task(_process_loop()) #J
        )
 
        return task_handler

See the run() implementation’s maximum-step guard. section 4.2.3

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

How deep it goes
Shows a maximum-step check and names task completion and irrecoverable errors as endpoints, without detailing other configurable policies.
Examples
Uses a croissant search-and-analysis task and a one-step Hailstone task that ends when its requested step is complete.
What it assumes
Assumes agent planning and tool calling, and situates stopping within repeated task sub-steps.
What you can do after
Configure task execution with an optional max_steps limit that raises MaxStepsReachedError when reached.
How it connects
Connects stopping to agent processing loops, task handlers, step results, and execution trajectories.

Also explained in

Compare how 14 books explain stopping criterion →

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.