Sub topic 5
Defining tools: names, descriptions, and schemas
This chapter assumes you can read JSON and basic Python, and that you know a model call is stateless and that a conversation is sent as a list of messages. Here you will learn what a tool definition contains, how the model uses it, and which mistakes make tools hard for a model to use.
What a tool definition is
A language model can only produce text. It cannot read a file or run a command. A tool is a function that the harness (the program that runs the model in a loop) is willing to execute on the model's behalf. The model never sees your function's code. It sees a short description of the function, and it decides from that description whether to ask for a call.
That description is the tool definition. It has three parts:
- A name that identifies the tool. The model writes this name when it asks for a call.
- A description in plain language. The model reads this to decide when the tool is the right choice.
- An input schema written in JSON Schema. It lists the arguments, their types, and which ones are required.
The harness sends the definitions to the model along with the messages on every call. Because the model is stateless, the definitions must be sent every time, just like the conversation.
{
"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"]
}
}Exact field names vary by provider. Some use input_schema, others use parameters, and some wrap the definition in an extra function object. The three ideas are the same everywhere.
How the model uses a definition
The definitions are placed into the model's input, usually in a format the provider controls. They count as tokens in the context window, so they are part of what the model "reads" before it answers. When the model decides to use a tool, it does not run anything. It emits a structured request: a tool name and a JSON object of arguments. The next chapter covers that exchange in detail. For now, the key point is that the definition is the only information the model has about the tool.
The name
Choose names that say what the tool does, in the style of a function name.
- Use a verb and an object:
read_file,search_code,run_command. - Keep names distinct.
searchandfindare hard to tell apart.search_codeandlist_filesare not. - Follow the character rules of your provider. Many allow only letters, digits, underscores, and hyphens, and limit the length.
The name is also the key the harness uses to find the function. In the registry below, the dictionary key must match the name field in the definition exactly. A mismatch gives you a KeyError at run time, not a helpful message.
The description
The description is the most important text in the definition. The model has no other way to know what the tool does, what it returns, or when to prefer it over another tool.
A good description answers four questions:
- What does the tool do?
- What does it return, including any limits?
- When should the model use it?
- When should it not use it?
Compare two descriptions of the same tool.
| Weak | Stronger |
|---|---|
| "Runs a command." | "Run a shell command in the workspace and return combined stdout and stderr. Output is cut to the last 5,000 characters. Times out after 30 seconds. Use for tests, builds, and git. Do not use it to read files; use read_file instead." |
The stronger version tells the model about the output limit, so it will not be surprised by a cut-off result. It tells the model about the timeout, so it will not start a long-running server. It also steers the model toward the right tool for reading files.
Describing limits honestly
If your function truncates output, say so in the description. A model that does not know about truncation may assume it has seen the whole file and draw wrong conclusions from the partial text. The same holds for any other behavior the model cannot observe, such as a working directory or a read-only mode.
The input schema
The input schema is a JSON Schema object. It tells the model which arguments to send, and it lets the harness check them before running anything.
The parts you will use most often are:
type:string,integer,number,boolean,array, orobject.properties: one entry per argument, each with its owntypeanddescription.required: a list of argument names the model must always provide.enum: a fixed list of allowed values.defaultandminimum/maximum: hints for optional values and ranges.
Each property should have its own description. The one on path above says "relative to the workspace root". Without it, the model might send an absolute path that your function rejects.
Here is a slightly richer definition for a search tool.
{
"name": "search_code",
"description": "Search workspace files for a regular expression and return matching lines as 'path:line: text'. Returns at most max_results matches. Use to locate definitions and usages before reading whole files.",
"input_schema": {
"type": "object",
"properties": {
"pattern": {"type": "string", "description": "Regular expression to search for"},
"glob": {"type": "string", "description": "Optional file filter such as '*.py'"},
"max_results": {"type": "integer", "minimum": 1, "maximum": 200, "default": 50}
},
"required": ["pattern"]
}
}Only pattern is required. The model can omit the others, and your function should supply defaults for them.
From definition to implementation
The definition describes the tool. A separate function implements it. The harness joins the two through a registry that maps names to functions.
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}Notice three design choices in this code.
- The argument names,
pathandcommand, match the schema's property names. The harness will callTOOLS[name](**arguments), so the keys in the model's JSON become keyword arguments. - Each function cuts its output to a fixed length.
read_filekeeps the first 20,000 characters.run_commandkeeps the last 5,000, because the end of a build or test log usually holds the failure. One large result could otherwise fill the context window. - Each function returns plain text. Whatever the function returns will be sent back to the model as text.
The definition and the function can drift apart. If you rename a parameter in the function but not in the schema, the model keeps sending the old name and every call fails. Keeping both in one place, or generating the schema from the function signature, reduces this risk.
Validate arguments anyway
The schema is guidance for the model, not a guarantee. A model can still send a wrong type, omit a required field, or invent an argument. Some providers check arguments against the schema, and many do not. The harness should always validate before executing.
def validate(arguments, schema):
props = schema.get("properties", {})
missing = [k for k in schema.get("required", []) if k not in arguments]
unknown = [k for k in arguments if k not in props]
if missing:
return f"missing required argument(s): {', '.join(missing)}"
if unknown:
return f"unknown argument(s): {', '.join(unknown)}"
return NoneIf validation fails, return the message as the tool result instead of raising an exception that ends the run. The model can read "missing required argument(s): path" and retry correctly. A production harness would use a full JSON Schema library to check types and ranges as well.
Common mistakes
- Vague descriptions. "Does file stuff" gives the model nothing to decide with. State the action, the return value, and the limits.
- Overlapping tools. Two tools that do nearly the same thing make the model pick between them at random. Merge them or write descriptions that say when to use each.
- Too many tools. Every definition costs tokens on every call, and a long menu makes selection less reliable. Include only the tools the task needs.
- Too many optional arguments. Each extra field is another chance for a wrong value. Start with the smallest schema that works.
- Free-form strings where an enum fits. If a
modeargument accepts only"read"or"write", declare anenum. - Hidden behavior. Truncation, timeouts, and side effects that are not in the description surprise the model.
- Schema and function out of sync. Renamed or removed parameters cause repeated failures that look like model errors.
Edge cases
- Nested objects and arrays. Schemas can describe them, but models make more mistakes as nesting deepens. Prefer flat arguments.
- Extra properties. JSON Schema allows unknown properties by default. Setting
"additionalProperties": falsedocuments that extras are not accepted, although your harness still has to enforce it. - Strict modes. Some providers offer a mode that constrains output to the schema. It reduces malformed calls but supports only part of JSON Schema, so check the provider's documentation.
- Large results. A tool that can return a huge result needs a limit in code, as above, and a note in the description.
- No required arguments. A tool like
list_filescan have an emptypropertiesobject. The model then sends{}.
Exercise
A tool named run_command has the description "Runs a command." During a task, the model starts a development server with it, and the call hangs until the 30-second timeout. Rewrite the description so this is less likely, and name the two facts you added.
Show answer
For example: "Run a shell command in the workspace and return combined stdout and stderr, cut to the last 5,000 characters. The command is stopped after 30 seconds, so do not start long-running processes such as servers or watchers. Use for tests, builds, and git." The two added facts are the 30-second timeout and the output limit, plus guidance on what the tool is for and what to avoid. The model now knows the constraint before it makes the call.
Exercise
The schema for read_file requires path. The model sends {"file": "main.py"}. What should the harness return, and why is that better than raising an uncaught exception?
Show answer
It should return a text result such as "missing required argument(s): path; unknown argument(s): file" as the tool result. The model reads this in the next call and can retry with the correct argument name. An uncaught exception would end the whole run, and the model would get no chance to correct itself.