Sub topic 5
Defining tools: names, descriptions, and schemas
This chapter assumes you can read JSON and JSON Schema basics, and that you know a model call is stateless: the harness sends the full conversation each time. Here you will see how a tool is described to the model, and what makes a definition work well or badly.
What a tool definition is
A language model cannot run code. It can only produce text. To let it act, the harness sends a list of tool definitions along with the messages. Each definition tells the model what tools exist and how to ask for them. The model reads these definitions as part of its input, so they use up context window space like any other text.
A definition has three parts:
- Name: a short identifier the harness uses to find the matching function.
- Description: plain-language text the model reads to decide when to use the tool.
- Input schema: a JSON Schema that says which arguments the tool takes, their types, and which are required.
{
"name": "read_file",
"description": "Read a UTF-8 text file from the workspace and return its contents.",
"input_schema": {
"type": "object",
"properties": {
"path": {"type": "string", "description": "Path relative to the workspace root"}
},
"required": ["path"]
}
}Field names vary by provider. Some call the schema parameters or inputSchema. The three ideas stay the same.
Writing good descriptions
The description is the only guidance the model has about when to pick a tool. A vague description leads to wrong or missing calls. Compare these two:
| Weak | Better |
|---|---|
| "Files tool" | "Read a UTF-8 text file from the workspace and return its contents. Output is cut at 20,000 characters." |
| "Runs stuff" | "Run a shell command in the workspace and return combined stdout and stderr. Times out after 30 seconds." |
Good descriptions say what the tool does, what it returns, and any limits. If two tools overlap, say how they differ, so the model can choose between them.
Writing good schemas
The schema constrains the arguments the model produces. Keep these habits:
- Describe every property, as
pathis described above. - Mark arguments as
requiredonly when the tool cannot work without them. - Use
enumfor a fixed set of choices, such as"mode": {"enum": ["read", "append"]}. - Prefer few, simple arguments. Deeply nested objects raise the chance of malformed calls.
From definition to implementation
The name in the definition must match a key in the harness's registry. The registry maps names to functions, and the harness calls the function with the model's arguments.
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}Both functions cut their output to a fixed length, so one large file or noisy command cannot fill the context window. read_file keeps the start of the text, and run_command keeps the end, where errors usually appear.
Common mistakes
- Name mismatch: the definition says
read_filebut the registry key isreadFile. The call fails with aKeyError. - Schema and function disagree: the schema names the argument
path, but the function expectsfilename. Python raises aTypeError. - Unbounded output: a tool returns a whole log file and crowds out the rest of the conversation.
- Model arguments are not trusted: the schema describes what the model should send, but the harness should still validate values, such as rejecting paths outside the workspace.
Exercise
A model keeps calling run_command with cat notes.txt instead of using read_file. Name two changes to the tool definitions that could fix this.
Show answer
Make the read_file description specific, saying it reads text files and returns their contents with a size limit, so the model sees it as the right choice. Also state in the run_command description that file reading should use read_file, so the overlap is resolved. Checking that the read_file schema is simple and clear also helps.
Exercise
Your registry has the key search_text, but the definition sent to the model is named search. What happens when the model calls search, and where should the fix go?
Show answer
The registry lookup TOOLS["search"] raises a KeyError, which the agent loop reports as an error result. Fix it by making the name in the definition and the registry key identical, in either place.