Overview

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.
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.
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.
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.
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 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.
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.
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.
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.
Comparing synchronous and asynchronous tool executions.
The UML class diagram for AsyncBaseTool, which shows a nearly identical structure to the BaseTool class.
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.
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.
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.
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.
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.
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.

FAQ

What is the main purpose of tools in an LLM agent framework?Tools extend what an LLM agent can do by letting it perform actions such as web search, plotting, or code execution, which increases the range of tasks it can handle.
Why does the framework define a BaseTool class?BaseTool provides a standard interface for all tools, so they can be described, invoked, and managed in a consistent way across the framework.
What attributes must every BaseTool implementation provide?Every BaseTool must provide name, description, parameters_json_schema, and a __call__() method.
What are ToolCall and ToolCallResult used for?ToolCall represents the input to a tool invocation, while ToolCallResult represents the output returned after the tool runs.
What information is stored in a ToolCall object?A ToolCall stores an identifier (id_), the selected tool’s name (tool_name), and the tool arguments (arguments).
What information is stored in a ToolCallResult object?A ToolCallResult stores the associated tool_call_id, the returned content, and an error flag indicating whether execution failed.
Why are JSON Schemas used for tool parameters?JSON Schemas give LLMs a clear, structured description of the expected tool inputs, and they match the format supported by many LLM tool-calling APIs and SDKs.
How does SimpleFunctionTool turn a Python function into a tool?SimpleFunctionTool wraps a Python function and automatically derives the tool’s name, description, parameter schema, and execution behavior from that function.
How does the framework validate tool-call arguments?It uses validate_tool_call_arguments() with the jsonschema library to check whether ToolCall.arguments matches the tool’s JSON Schema before execution.
What is the difference between BaseTool and AsyncBaseTool?BaseTool is synchronous and blocks while running, whereas AsyncBaseTool uses an asynchronous __call__() method so tools can run without blocking and can be awaited.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a Multi-Agent System (from Scratch) Build a Multi-Agent System (from Scratch) ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a Multi-Agent System (from Scratch) Build a Multi-Agent System (from Scratch) ebook for free