BaseTool is the abstract base class for callable tools in the llm-agents-from-scratch framework. It gives an LLM agent a consistent way to describe and execute tools, even when their underlying functions differ. section 2.1 section 2.1.2
A subclass implements four parts of the interface:
- name: the tool’s name, supplied to the LLM. section 2.1.2
- description: a description of what the tool does. section 2.1.2
- parameters_json_schema: a JSON Schema dictionary describing the tool’s input parameters. section 2.1.2
- __call__: a method that executes a ToolCall and returns a ToolCallResult. section 2.1.2
The three metadata attributes are abstract properties: subclasses must implement them, while retaining the flexibility to hide internal attributes or perform validation. JSON Schema also helps align parameter descriptions with existing LLM tool-calling APIs. section 2.1.2
Listing 2.3 Defining BaseTool
# llm_agents_from_scratch/base/tool.py #A
from abc import ABC, abstractmethod
from llm_agents_from_scratch.data_structures.tool import (
ToolCall, #B
ToolCallResult, #B
)
class BaseTool(ABC):
"""Base Tool Class."""
@property #C
@abstractmethod
def name(self) -> str:
"""Name of tool."""
@property
@abstractmethod #D
def description(self) -> str:
"""Description of what this tool does."""
@property
@abstractmethod
def parameters_json_schema(self) -> dict[str, Any]:
"""JSON Schema for tool parameters."""
@abstractmethod
def __call__(
self,
tool_call: ToolCall,
*args: Any,
**kwargs: Any,
) -> ToolCallResult:
"""Execute the tool call.""" #EThe interface definition shows the abstract properties and the ToolCall-to-ToolCallResult method signature. section 2.1.2
In a tool call, the agent prepares a ToolCall and invokes the tool. The tool executes its logic and returns a ToolCallResult to the agent; the result can then be used in the LLM’s response. Each BaseTool invocation handles one call and returns one result. section 1.9 section 2.5 section 2.0
A Hailstone tool
A Hailstone subclass describes a required numeric parameter x and implements one sequence step. For x = 3, it computes 3 × 3 + 1 and returns a ToolCallResult with content 10, the originating call ID, and error set to false. section 2.1.2
AsyncBaseTool provides the corresponding interface for asynchronous tool logic. SimpleFunctionTool wraps a Python function as a BaseTool, while PydanticFunctionTool wraps a function that receives its inputs through a Pydantic BaseModel. section 2.5
