liveBook · Manning

Tool use

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

ToolCall is a data structure representing a requested tool invocation and its arguments.

Tool calling lets an LLM request an action, such as searching for bakery prices, without executing it itself. The surrounding agent supplies tool descriptions, executes the requested tool, and returns its result to the LLM for synthesis. section 1.0 section 1.2.1

A tool description provides its name, purpose, and input parameters as JSON Schema. The LLM selects a tool and supplies argument values; the application packages the request as a ToolCall. Provider APIs typically generate requests in a structured format, avoiding brittle extraction from natural language. section 2.5 section 2.1.1

The Python ToolCall model has three fields:

  • id_: a string identifier, generated as a UUID by default. section 2.1.1
  • tool_name: the name of the selected tool. section 2.1.1
  • arguments: a dictionary of parameter names and values for execution. section 2.1.1
Listing 2.1 Implementing ToolCall
# llm_agents_from_scratch/data_structures/tool.py  #A
import uuid
from typing import Any
 
from pydantic import BaseModel, Field
 
 
class ToolCall(BaseModel):
    """Tool call.
 
    Attributes:
        id_: A string identifier for the tool call.
        tool_name: Name of tool to call.
        arguments: The arguments to pass to the tool execution.
    """
    
    id_: str = Field(default_factory=lambda: str(uuid.uuid4()))
    tool_name: str  #B
    arguments: dict[str, Any]  #C

The Pydantic model defines the request fields and the identifier’s default factory. section 2.1.1

A typical execution flow separates the request from the action:

  • Read ToolCall objects from the assistant ChatMessage’s tool_calls attribute and select each named tool. section 3.1.2
  • Pass each ToolCall to the selected BaseTool’s __call__() method. The tool executes its logic using the arguments and returns a ToolCallResult. section 2.1
  • Match each result to its request with tool_call_id. The result carries content and an error flag, then returns to the LLM as a tool message for synthesis. section 2.1.1 section 3.1.2

A Hailstone step

An OllamaLLM chat can request ToolCall(tool_name="hailstone", arguments={"x": 3}). Calling the Hailstone tool with that object produces a result containing 10. Passing the result and chat history to continue_chat_with_tool_results() lets the LLM respond with the computed value. section 3.2.2

Within an agent processing loop, planning and tool calls can recur across task steps. One implementation executes calls in a step concurrently but permits only one tool-calling phase per step. With MCP tools, the same request-and-result flow applies; execution takes place on an external MCP server. section 1.3 section 4.2.3 section 5.1.2

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

How deep it goes
Implements request and result models, tool interfaces, chat continuation, and concurrent execution, with one tool-calling phase per task step.
Examples
Demonstrates a bakery web search and a Hailstone calculation that returns 10 for input 3.
What you can do after
Builds tool-call infrastructure from scratch and performs a complete invocation with OllamaLLM.
How it connects
Connects tool calling with agent planning, JSON Schema, chat messages, and Model Context Protocol.

Also explained in

Compare how 18 books explain tool use →

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.