chapter four

4 The LLM Agent Class

 

This chapter covers

  • Building our first LLM agent from scratch
  • Implementing a processing loop for an LLM agent
  • Data structures for tasks and their results

In the previous two chapters, we established that LLM agents are comprised of a backbone LLM and a collection of tools. Now that we have the core components in place and know how to work with tools, we can bring everything together to build our first LLM agent.

To do this, we work with a new class, LLMAgent, that enables the creation of LLM agents that can perform tasks within our framework. To get an LLMAgent to perform a task, we have to implement a processing loop that leverages the planning and tool-calling capabilities of its backbone LLM. As with the previous chapters, we’ll need to define a few new data structures to facilitate the overall task execution, including structures for tasks and their results.

Let's quickly revisit our build plan in figure 4.1, which shows the core classes for tools and LLMs we've added to our framework thus far and highlights the focus of this chapter: building an LLM agent and implementing a processing loop.

Figure 4.1 Having defined classes for tools and LLMs in our framework, we can now complete stage 1 by building the LLM agent.

4.1 LLMAgent: a simple LLM agent class

4.1.1 Implementing Task and TaskResult

4.1.2 Implementing LLMAgent: Part 1

4.2 Implementing a processing loop for LLMAgent

4.2.1 Designing a processing loop

4.2.2 Implementing TaskStep, TaskStepResult, and NextStepDecision

4.2.3 Implementing LLMAgent: Part 2

4.3 Putting it all together: Hailstone LLM agent

4.4 Summary