← All AI Agents & Tool Use modules

Module 2 — Calling Tools

The core loop · hands-on · about 25 minutes.

A language model demonstrates strong performance on natural-language tasks but performs poorly on operations that appear trivial — such as multiplying two numbers. The model does not compute; it predicts the next token, producing digits that are statistically plausible rather than numerically correct. Requesting 18.5% of $84.50 may yield a confidently stated but incorrect answer. The remedy is the second foundational element of every agent: provide the model with a tool it may invoke, so it can delegate the computation rather than attempt it directly.

A tool is a function the agent may invoke

A tool comprises a name, a description of its function, an input, and a returned result. A calculator tool accepts "84.50 * 0.185" and returns "15.63". The agent's responsibility is not to perform the arithmetic itself but to identify the operation as a calculation, delegate it to the calculator, and incorporate the returned value. The returned result enters the loop as the next observation, in the same role as the "too high / too low" feedback in Module 1.

Observe the agent's tool invocation

Select a question. On the left is the typical behavior of a model that generates the answer token by token without tool use. On the right, the agent instead invokes the calculator tool — the Action, the Observation returned by the tool, and the final Answer constructed from that observation are all displayed. The tool's result is exact; the token-by-token generation is frequently not.

This activity needs JavaScript. The lesson below still covers everything.

Invoke the tool directly

Submit an arithmetic expression to the calculator tool. This is the same tool invoked by the agent — it supports + - * / % and parentheses. (The implementation is a safe expression parser, not a general code executor: it performs arithmetic only.)

This activity needs JavaScript.

Tool declaration in code
# the agent is handed a list of tools it may call
calculator = {
    "name": "calculator",
    "description": "Evaluate an arithmetic expression, e.g. '84.5 * 0.185'.",
    "run": lambda expr: str(safe_eval(expr)),
}

# the model decides: call the tool instead of guessing the digits
action = {"tool": "calculator", "arg": "84.5 * 0.185"}
observation = tools[action["tool"]]["run"](action["arg"])  # "15.6325"

This structure is identical to the OpenAI "function calling" and Claude "tool use" APIs: the tools are described to the model, the model emits a structured invocation, the host application executes it, and the result is returned to the conversation as an observation.

AI anchor — tools are the interface between models and the external world Every action a modern AI assistant performs beyond text generation is implemented as a tool call. Web retrieval, code execution, weather lookups, file search, and message dispatch are each functions the model elected to invoke, with the result returned to inform subsequent decisions. The model contributes the judgment of which tool to invoke and what input to provide; the tool contributes the capability and the ground-truth result. This separation of concerns — model decides, tool acts — is the defining architectural pattern of agentic AI.

Check your understanding

Answer a short set of questions on tool definitions and tool invocations.

This activity needs JavaScript.

Why this matters next A single tool invocation suffices for a single-step question. However, a question such as "how many more people live in Tokyo than in Paris?" requires two retrievals followed by a calculation. Module 3 introduces the ReAct pattern, which interleaves reasoning, action, and observation across multiple iterations.
Summary: a tool is a named function with a defined input and return value that the agent may invoke. Rather than generating answers to tasks at which a language model is unreliable — exact arithmetic, for example — the agent invokes the appropriate tool and incorporates the result as an observation in the next iteration of the loop.

Next: The ReAct Pattern →