AI Run Lab

All chapters

How a language model uses a tool

A rectangular room measures 5 metres by 5 metres. Follow the question all the way to 25 square metres and see precisely which component produces the number.

Open this chapter with experiments and audio

Which calculation does the question require?

The question supplies two lengths and asks for an area. For a rectangle the operation is multiplication. The units multiply too, so metres times metres gives square metres. Change the request to “How much skirting board goes around the room?” and the very same dimensions require a different operation. The perimeter is twice the sum of the two sides.

An LLM can generate a textual answer directly. A correct answer by itself does not reveal whether the network reused a frequent pattern or arrived at the answer through several internal computations. When the application wants an auditable calculation, it can expose a function with a name, an argument schema and a description of when to use it.

Watch the handoff from text to numbers

Change a dimension below and follow the values. In a deployed assistant, a tool-capable model chooses the operation and supplies the arguments. Here the choice is illustrated by a fixed example. The numerical result is computed from the displayed dimensions. Read each stage and ask which component could make a mistake there.

At 5 × 5 metres the function returns 25. At 5 × 8 it returns 40. If the question gives centimetres, converting both sides to metres first preserves the promised unit. A calculator can be perfectly accurate on the wrong inputs.

Run the function yourself

The following script reproduces the execution stage. The model’s example proposal is a Python dictionary here. The dispatcher checks which operation was requested. The function checks the data before it computes a value and returns the unit explicitly. Run it with 6 and 4, then try a negative length.

from math import isfinite

def rectangle_area(length_m, width_m):
    for side in (length_m, width_m):
        if type(side) not in (int, float) or not isfinite(side) or side <= 0:
            raise ValueError("Expected a positive length in metres")
    return {"area": length_m * width_m, "unit": "m²"}

proposal = {"name": "rectangle_area", "arguments": {"length_m": 5, "width_m": 5}}
if proposal["name"] == "rectangle_area":
    print(rectangle_area(**proposal["arguments"]))

When does a tool call become an agent loop?

A single call ends when the application returns the result. An agent loop gives the result back to the model so it can decide what comes next. For example, it may need a room dimension from a drawing before it can calculate the area. An observation supplies the missing value, the calculation produces the area and a final message explains it.

Each additional iteration costs time and may repeat earlier work. Keep a record of proposed calls, validated arguments and results. Stop after a final message, an unrecoverable error or a defined call budget. When an action changes something outside the conversation, its permission belongs in application code. Tool descriptions help the model select actions, while the application controls which actions can actually run.

The assistant says 25 m. What is wrong?

The magnitude may be right but the unit is for length. The area is 25 m². Preserve both value and unit in the tool result.

Ollama · Tool calling

A valid tool call can still refer to the wrong thing

A customer asks when their lamp will arrive. The model proposes an order lookup using ID 481. A schema can check that 481 is an integer. The application must separately check which customer is signed in and which orders they may read. The order database supplies the shipment date. That tool result is appended as a message before the generator words the answer.

Suppose the lookup times out. A read-only query can often be retried within a deadline. Creating a second shipment could duplicate a real action if the first request succeeded and only its reply was lost. Give the operation a unique identifier, record its state and query that state before a retry. These decisions belong to the program that executes the call. The generated sentence “I have booked it” provides no independent evidence that the booking exists.

Follow the handoff and inspect the returned value →