liveBook · Manning

__call__() method

As explained in Build a Multi-Agent System (from Scratch)

The __call__() method is a Python special method that defines how an object behaves when invoked with function-call syntax.

The Python special method __call__() defines what happens when an object is invoked with function-call syntax. For an LLM agent tool, it provides a consistent execution interface: the agent passes a ToolCall to the selected tool and receives a ToolCallResult. section 2.1.2 section 2.1

BaseTool declares __call__() as abstract. Each subclass supplies its own execution logic, alongside the name, description, and parameters_json_schema properties used to describe the tool to an LLM. The method accepts a ToolCall and returns a ToolCallResult; its signature also allows additional positional and keyword arguments. 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."""  #E

The BaseTool definition shows the abstract execution signature and the properties every tool subclass implements. section 2.1.2

Calling a Hailstone tool

A Hailstone subclass implements one sequence step. Calling hailstone_tool(tool_call) with arguments {"x": 3} invokes its __call__() method and returns a result with content 10, the original tool-call ID, and error=False. section 2.1.2

The same interface supports different execution strategies:

  • SimpleFunctionTool validates arguments against its JSON Schema, then delegates to the wrapped Python function. Validation and execution errors are packaged as ToolCallResult objects. section 2.2.1
  • UseSkillTool checks that the requested skill name is a string and exists in its registry, then returns formatted skill content. Its validation is deliberately less strict than the schema enum to support explicit activation. section 6.4.2
  • HumanInputTool validates a prompt and optional choices, presents them to a human operator, and waits for a response. section 8.2
  • UseA2AAgentTool implements an asynchronous __call__() that forwards a task to a remote A2A agent and converts the response to a ToolCallResult. An optional task ID supports resuming a paused task. section 10.2.2
Listing 2.9 Implementing SimpleFunctionTool.__call__() (validation)
# llm_agents_from_scratch/tools/simple_function.py
…  #A
from llm_agents_from_scratch.tools.utils import validate_tool_call_arguments #B
 
class SimpleFunctionTool(BaseTool):
    """Simple function calling tool.
 
    Turn a Python function into a tool for an LLM.
    """
    …  #C
 
    def __call__(
        self,
        tool_call: ToolCall,
        *args: Any,
        **kwargs: Any,
    ) -> ToolCallResult:
        """Execute the function tool with a ToolCall.""" #D
       
        if validation_error_details := validate_tool_call_arguments( #E
            tool_call,
            self.parameters_json_schema,
        ):
            return ToolCallResult( #F
                tool_call_id=tool_call.id_,
                content=json.dumps(validation_error_details),
                error=True,
            )
 
        …  #G

The validation listing shows how a failed argument check returns an error result before function execution. section 2.2.1

How Build a Multi-Agent System (from Scratch) covers it

How deep it goes
Implements an abstract execution interface and concrete validation and delegation paths, rather than examining Python call-dispatch internals.
Examples
Demonstrates a Hailstone calculation, a wrapped function, skill activation, human input, and remote agent dispatch.
What you can do after
Build callable tool subclasses from scratch that validate requests, execute logic, and package results.
How it connects
Connects callable objects to abstract classes, JSON Schema validation, tool calling, and asynchronous execution.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Multi-Agent System (from Scratch). The section links point to the parts of the book it draws on.