Module 2 — Calling Tools
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.
# 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.
Check your understanding
Answer a short set of questions on tool definitions and tool invocations.
This activity needs JavaScript.