4 The LLMAgent Class
This chapter introduces the first complete LLM agent in the framework by combining the backbone LLM, tools, prompt templates, and task-oriented data structures into a working system. It begins by defining the agent’s core responsibilities: receiving a task, planning how to solve it, and invoking tools when needed. To support this, the chapter adds simple structured models for tasks and task results, along with matching step-level structures that will later drive the agent’s internal execution flow.
The middle of the chapter focuses on the agent’s architecture and the processing loop that powers its behavior. The agent class stores the LLM, a tool registry for fast lookup, and templates used to standardize prompts. A nested task handler manages execution as a sequence of discrete sub-steps: it asks the LLM what to do next, runs one step at a time, executes any requested tool calls, and records a rollout history of the interaction. The design uses structured outputs to decide whether to continue with another step or return a final result, and it wraps the whole process in an awaitable future so task execution can run concurrently in the background.
The chapter closes by demonstrating the agent on a Hailstone-sequence example, showing how the processing loop coordinates repeated tool use until the task is complete. The example highlights why prompt design matters, including the need to hide obvious cues so the model actually uses the tool rather than relying on memorized knowledge. Finally, the chapter connects this agent design to larger multi-agent systems by showing how the same mechanism can support specialized subagents, delegated work, and remote agents in future framework extensions.
Having defined classes for tools and LLMs in our framework, we can now complete part 1 by building the LLM agent.
Our LLM agent, now showing the LLMAgent class with its core attributes and run() method.
Submitting Task objects to the run() method of an LLMAgent produces a TaskResult.
UML class diagrams for LLMAgent and the new data structure classes, Task and TaskResult, that it works with to perform tasks.
Templates are customizable text structures that help to standardize interactions with an LLM.
Executing a task through sub-steps within a processing loop. Each sub-step uses the LLM's planning and tool-calling capabilities, with the results informing the next sub-step's instruction.
The execution of sub-steps within a processing loop using the data structures TaskStep, TaskStepResult and NextStepDecision
UML class diagrams for TaskStep, TaskStepResult, NextStepDecision, and TaskHandler
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.
Adding new entries to the rollout attribute after every sub-step during task execution
The get_next_step() execution to produce either a TaskStep or TaskResult
The run_step() execution using a single-turn LLM chat interaction between the LLMAgent and its backbone LLM
Summary
- The LLMAgent class creates LLM agents from an LLM and a set of Tool objects.
- The Task data structure contains an instruction for an LLMAgent to execute with the run() method. Awaiting the run() method returns a TaskResult data structure.
- Executing a task within the run() method involves various interactions with the backbone LLM. To standardize our interactions, we use prompt templates defined by the LLMAgentTemplates type.
- The processing loop, where task execution occurs, is performed through a sequence of discrete sub-steps. Implementations of the processing loop, such as the ReAct processing loop, differ in their definitions of sub-steps and the work performed within them.
- Our processing loop implementation is simple, involving one planning and one tool-calling phase. Between each sub-step, the LLMAgent uses its backbone LLM to determine whether a new sub-step is required or if the overall task is complete.
- The data structures TaskStep, TaskStepResult, and GetNextDecision help to facilitate the implementation of our processing loop.
- The TaskHandler is a helper class, nested within the LLMAgent class. It’s designed to orchestrate the execution of a submitted Task through the discrete sub-steps of our processing loop.
- TaskHandler inherits from asyncio.Future, ultimately resolving to a TaskResult. It also supports two methods get_next_step() and run_step(), that are executed in tandem within the processing loop until task completion.
- The run() method creates a TaskHandler and schedules the processing loop for execution as a background asyncio.Task, then returns the TaskHandler for flexible concurrency.
- When task execution completes, the result of the TaskHandler is set to the TaskResult. As a subclass of asyncio.Future, the TaskHandler and thus also the run() method can be awaited.
- Rollouts, otherwise known as trajectories, provide a useful log of the LLM agent’s actions and reasoning during task execution. The information they provide can be used to evaluate and improve an LLM agent.
Build a Multi-Agent System (from Scratch) ebook for free