Sub topic 6
Tool calls: the model asks, the harness acts
This chapter assumes you know basic Python, JSON, and how a chat-style model API sends a list of messages. It also assumes you have seen a tool definition (a name, a description, and an input schema). Here you will see what happens after the model decides to use one.
The model never runs anything
A language model only produces text. When it "calls a tool," it emits a structured request that says which tool to run and with what arguments. The harness is the program around the model. It reads that request, runs the real function, and sends the output back as a new message.
This split matters for safety and for debugging. The harness decides whether a request is allowed, how long it may run, and how much of its output the model sees.
One tool call, end to end
The model's reply contains a tool call with three parts:
- An
idthat identifies this specific request. - A
namethat matches one of the tools you defined. arguments, a JSON object that should fit the tool's input schema.
The harness answers with a tool result message that carries the same id. That id lets the model match each result to its request, which matters when it asks for several tools in one reply. Field names differ between providers, but the shape is the same.
Executing a call
The harness looks up the name in a registry and passes the arguments to the function. This is the registry from the earlier tool code:
import subprocess
from pathlib import Path
def read_file(path):
return Path(path).read_text()[:20000]
def run_command(command):
r = subprocess.run(command, shell=True, capture_output=True, text=True, timeout=30)
return (r.stdout + r.stderr)[-5000:]
TOOLS = {"read_file": read_file, "run_command": run_command}The functions are ordinary code. Two details are worth noting:
- Each function cuts its output to a fixed length, so one result cannot fill the context window.
run_commandkeeps the end of the output, because errors usually appear last.read_filekeeps the start.
A single dispatch step looks like this:
def execute(call):
try:
return str(TOOLS[call["name"]](**call["arguments"]))
except Exception as e:
return f"error: {e}"The except clause turns a failure into text. An unknown tool name, a missing argument, or a missing file all become a result the model can read and react to, instead of a crash.
Common errors
- Wrong or missing arguments. The model may omit a required field or invent one. Python raises a
TypeError, and the message you return tells the model what to fix. Validating against the schema first gives clearer messages. - Hallucinated tool names. The model may call a tool that does not exist. Return an error that lists the valid names.
- Missing result. If you skip a tool result for a call id, many APIs reject the next request. Every call needs exactly one result.
- Silent failure. An empty result looks like success. Return something explicit, such as "command produced no output, exit code 0."
- Unbounded output. Without truncation, one
caton a large log can use most of the window.
Exercise
A model requests two tool calls in one reply, with ids "a1" and "a2". The harness runs both but sends back only one tool result, with id "a1". What is likely to happen, and what should the harness do instead?
Show answer
Many APIs reject the next request, or the model loses track of what happened to "a2", because every call id needs a matching result. The harness should send one result message for each call, with ids "a1" and "a2", even when a call fails. A failed call gets a result that starts with "error:".