liveBook · Manning

TaskResult

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

TaskResult is a framework data structure containing a task identifier and the result of executing that task.

TaskResult is the final result of an LLMAgent task. It pairs the task’s identifier with the content produced by execution, allowing the result to be matched to the original instruction. section 4.1 section 4.1.1

TaskResult is a Pydantic BaseModel with two string fields:

  • task_id matches the id_ of the corresponding Task. section 4.1.1
  • content holds the result of the overall task execution. section 4.1.1
Listing 4.1 Implementing Task and TaskResult
# llm_agents_from_scratch/data_structures/agent.py
import uuid
from typing import Literal
from pydantic import BaseModel, Field
 
 
class Task(BaseModel):
    """Represents a single task with an instruction.
 
    Attributes:
        id_: Identifier for task.
        instruction: The instruction for the task.
    """
 
    id_: str = Field(default_factory=lambda: str(uuid.uuid4()))
    instruction: str
 
    def __str__(self) -> str: #A
        """String representation of Task."""
        return self.instruction
 
 
class TaskResult(BaseModel):
    """The result of the task execution.
 
    Attributes:
        task_id: The ID `Task` that was executed.
        content: The content results of the task execution.
    """
 
    task_id: str #B
    content: str
 
    def __str__(self) -> str:
        """String representation of TaskResult."""
        return self.content

The implementation defines Task and TaskResult together, including the identifier link and string fields. section 4.1.1

LLMAgent.run(task) returns a TaskHandler, an asyncio.Future that resolves to a TaskResult. The handler runs a processing loop in the background: get_next_step() returns either a TaskStep to execute or a TaskResult marking task completion. The loop can also stop on an error or a configured step limit. section 4.5 section 4.2.3

Retrieving a Hailstone result

For a task generating the Hailstone sequence from 4, the result can have content '4 -> 2 -> 1' and a task_id matching the submitted Task. Await the handler, or check handler.done() before calling handler.result(); calling result() too early raises InvalidStateError. The run() return value can also be awaited directly. section 4.3

In supervised execution, an operator inspects successive TaskSteps and TaskStepResults. When get_next_step() returns a TaskResult instead of another step, the operator can accept it with complete(), or reject it and pass the resulting RejectedTaskResult to the next get_next_step() call to continue processing. section 8.4.2

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

How deep it goes
Defines the model’s fields and traces their use through asynchronous execution and supervised acceptance or rejection.
Examples
Uses a Hailstone sequence starting at 4 to demonstrate result retrieval and supervised review.
What it assumes
Assumes familiarity with LLM and tool components while defining the task and result models from scratch.
What you can do after
Build it from scratch, retrieve it from an agent handler, and review or reject it during supervised execution.
How it connects
Connects task execution with asynchronous futures, agent processing loops, and human-in-the-loop supervision.

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.