2 Working with Tools
This chapter explains why tools are essential for LLM agents: they extend an agent’s abilities beyond text generation to tasks such as searching the web, plotting data, and executing code. It introduces the idea that a tool must be described in a way the LLM can understand, then invoked through a structured tool-calling flow where the model requests a tool, the tool runs, and the result is returned for synthesis.
To make that process reliable, the chapter builds a framework around a common tool interface and a pair of data structures that represent tool input and output. A base tool abstraction standardizes key fields such as name, description, and parameter schema, while tool-call objects carry the selected tool name and arguments and tool-call-result objects capture the output or an error state. The chapter also distinguishes between synchronous and asynchronous tools, showing how an async variant enables non-blocking execution when tools depend on external calls.
The second half of the chapter shows how to turn ordinary Python functions into tools. It first creates a lightweight wrapper that infers a tool’s name and description from the function, generates a JSON Schema from the function signature, validates incoming arguments, and delegates execution to the wrapped function. It then presents a more robust Pydantic-based approach that uses built-in schema generation and validation. Overall, the chapter lays the groundwork for equipping agents with tools and hints at how those tools can later support more specialized responsibilities in multi-agent systems.
The focus of the current chapter, through the lens of our build plan introduced in Chapter 1. Before we can build an LLM agent, we first need to properly define tools and how they can be used within our framework.
Each tool inherits from the BaseTool class, allowing the LLM agent to access the textual descriptions and execute the logic of each tool in the same manner.
Incorporating our new classes—BaseTool, ToolCall, and ToolCallResult—into the tool-calling process, first shown in Chapter 1.
The UML class diagrams for BaseTool, ToolCall and ToolCallResult.
Creating a ToolCall object from an LLM’s natural language tool-call request. Since the web-search tool is a BaseTool, we must first convert the request into a ToolCall object before invoking it.
Creating a ToolCallResult object from the selected tool’s execution, handling both successful and failed executions.
Revisiting the tool-equipping process. With a BaseTool, we can use its attributes to fill in the values required for textual descriptions that are passed to the LLM.
The Hailstone tool performs a Hailstone step on a given positive integer. By implementing the Hailstone tool as a subclass of BaseTool, we can use it within the tool-call process for LLMs.
Comparing synchronous and asynchronous tool executions.
The UML class diagram for AsyncBaseTool, which shows a nearly identical structure to the BaseTool class.
Wrapping Python functions with SimpleFunctionTool so that they can be used as tools for LLMs and LLM agents.
UML class diagrams for SimpleFunctionTool and AsyncSimpleFunctionTool.
A visual breakdown of the three steps involved in the implementation of the helper function_signature_to_json_schema.
Executing a SimpleFunctionTool involves validating the parameter data of the ToolCall object and subsequently delegating to the wrapped function.
Wrapping an asynchronous Python function to create an AsyncBaseTool object automatically.
Summary
- To build an LLM agent, we need to build the required infrastructure, including abstractions representing tools that they work with to perform tasks.
- LLMs require the following information to be able to make a tool call request for a given tool: its name, a description of its functionality, and a JSON Schema of its input parameters.
- A BaseTool object executes a single ToolCall and returns a single ToolCallResult object.
- The AsyncBaseTool class is designed for tools that execute their logic asynchronously.
- The SimpleFunctionTool is a wrapper class for turning Python functions into a BaseTool objects.
- The AsyncSimpleFunctionTool is a wrapper class for turning async Python functions into a AsyncBaseTool objects.
- The PydanticFunctionTool is similar to the SimpleFunctionTool, but wraps a special function we called PydanticFunction instead. These functions get passed their input parameters for logic execution through a ~pydantic.BaseModel.
Build a Multi-Agent System (from Scratch) ebook for free