Sub topic 3
Defining a tool: name, description, parameters
This chapter covers the three parts of a tool definition and how the model uses each one. It assumes you can read JSON, have called a chat-style LLM API, and know the basics of JSON Schema (types, properties, required). The model never runs anything. A definition is a description of a function that the model reads when it decides whether to ask your code to call it.
What a tool definition is
A tool definition is a small JSON object sent with each chat request, next to the messages. It has three parts:
- A name that identifies the tool.
- A description in plain language that says what the tool does and when to use it.
- A parameters object, written in JSON Schema, that describes the arguments.
The provider shows these to the model as part of its context. When the model decides a tool would help, it writes a tool call that names the tool and supplies arguments shaped like your schema. Your code then runs the real function.
Here is a provider-neutral definition:
{
"name": "get_weather",
"description": "Get the current temperature for a city. Use when the user asks about current weather.",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name, for example Oslo"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["city"],
"additionalProperties": false
}
}The name
The name is the handle your dispatcher uses to find the right function. Providers usually restrict it to letters, digits, underscores, and hyphens, with a length limit (often 64 characters). Follow these rules:
- Use a verb and an object, such as
get_weather,search_orders, orcreate_ticket. - Keep names unique within one request.
- Pick one naming style and keep it across all tools, for example
snake_case. - Avoid vague names such as
run,helper, ordata. The model cannot tell what they do.
The name is also part of the model's decision. get_weather signals its purpose before the model reads a word of the description.
The description
The description is the most important text in the definition. The model has no other source for knowing when your tool applies. A weak description is the most common reason a model picks the wrong tool or skips a useful one.
A good description answers these questions:
- What does the tool do?
- When should the model use it?
- When should it not use it, if a similar tool exists?
- What does it return, and are there limits?
Compare two descriptions for the same tool:
| Weak | Better |
|---|---|
| "Weather tool." | "Get the current temperature for a city. Use when the user asks about current weather. Does not provide forecasts." |
| "Searches stuff." | "Search customer orders by order number or email. Returns at most 10 orders, newest first. Use `get_order` when you already have an order ID." |
The second example in the table names a neighboring tool. When two tools overlap, say which one handles which case. Otherwise the model guesses, and it may guess differently from one request to the next.
Keep descriptions short enough to scan, but do not trim them below what the model needs. Descriptions count as input tokens on every request, so a verbose description for 30 tools adds cost and latency to each call.
The parameters
The parameters field is a JSON Schema object. The top level should be "type": "object", because the model produces the arguments as a JSON object with named fields.
Properties and types
Each key under properties is one argument. Give it a type, and add a description unless the name makes the meaning obvious. Descriptions on individual properties work like the tool description, but they are specific to one argument. Include an example value, as the weather tool does with "for example Oslo". Examples steer the format the model uses.
Common types are string, number, integer, boolean, array, and object. Prefer integer over number when fractions make no sense.
Required and optional arguments
List the arguments the function cannot run without in required. Leave the others optional and give your Python function a default for them, as unit="celsius" does in the dispatcher. An argument that is optional in the schema but required in your code will fail whenever the model leaves it out.
Enums
Use enum when only a fixed set of values is valid. The model picks from the list instead of inventing a string. This is much safer than describing the allowed values in prose, such as "celsius or fahrenheit". It also makes validation simple, because anything outside the list is wrong by definition.
additionalProperties
Setting "additionalProperties": false tells the model, and your validator, that no extra keys are allowed. Some providers require it when you turn on strict schema enforcement. In strict modes, they often also require every property to appear in required, with optional values expressed as a type that allows null. Check your provider's rules before you rely on this.
A richer example
This schema uses more of the available constructs:
{
"name": "search_orders",
"description": "Search customer orders by email address. Returns at most 10 orders, newest first. Use get_order when you already have an order ID.",
"parameters": {
"type": "object",
"properties": {
"email": {
"type": "string",
"description": "Customer email, for example [email protected]"
},
"status": {
"type": "string",
"enum": ["pending", "shipped", "delivered", "cancelled"],
"description": "Only return orders with this status"
},
"limit": {
"type": "integer",
"minimum": 1,
"maximum": 10,
"description": "How many orders to return"
}
},
"required": ["email"],
"additionalProperties": false
}
}Keywords such as minimum and maximum guide the model, but you should not assume the model always obeys them. Treat the schema as a strong hint, not a guarantee. A later chapter covers checking arguments before you run anything.
How the definition reaches the model
The sequence below shows where definitions fit in a request.
Two consequences follow from this flow:
- You must send the definitions again on every request in a conversation. The API is stateless, so the model does not remember them from the previous turn.
- The model sees only the definition. It cannot read your function's code, so anything it needs to know about behavior must be in the description.
Keeping the definition and the code in sync
The schema and the Python function describe the same interface in two places, so they can drift apart. Typical failures:
- You rename an argument in the function and forget the schema. The model keeps sending the old name and every call raises
TypeError. - You add a required argument to the function but not to the schema. The model never supplies it.
- You change what the tool returns but leave the description unchanged. The model misreads the result.
A simple habit helps: keep each definition next to its function and put both in one module. A small registry also keeps them together:
def get_weather(city, unit="celsius"):
return {"city": city, "temperature": 7, "unit": unit}
GET_WEATHER_DEFINITION = {
"name": "get_weather",
"description": (
"Get the current temperature for a city. "
"Use when the user asks about current weather."
),
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name, for example Oslo"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["city"],
"additionalProperties": False,
},
}
TOOLS = {"get_weather": get_weather}
DEFINITIONS = [GET_WEATHER_DEFINITION]
print([d["name"] for d in DEFINITIONS])Some libraries generate the schema from type hints or from a model class such as a Pydantic model. That removes the drift, but read the generated output at least once. Generated descriptions come from docstrings and field names, and they are often too thin.
Common mistakes
- Vague description. The model cannot tell when to use the tool. Say what it does and when to call it.
- Too many tools. Accuracy usually drops as the list grows, because similar tools compete. Send only the tools relevant to the current task.
- Free-text arguments where an enum fits. The model invents values such as
"in transit"that your code does not recognize. - Overlapping tools with no guidance. Add "use X instead when..." to the descriptions.
- Hidden requirements. If a date must be in
YYYY-MM-DDformat, say so in the property description. - Secrets as parameters. Do not ask the model to pass API keys or user IDs it should not control. Supply those from your own code.
Checklist
Before you ship a definition, confirm the following:
- The name is a short verb and object, unique, and in your chosen style.
- The description says what the tool does, when to use it, and when not to.
- Every argument has a type, and a description when the name is not self-evident.
- Fixed value sets use
enum. requiredlists exactly the arguments your function cannot run without.- The function's signature matches the schema.
Exercise
A model keeps calling search_orders with a status of "in transit", which your code rejects. The schema declares status as a plain string, and the only guidance is the description "Order status". Name two changes to the definition that would fix this.
Show answer
First, change status to use an enum listing the exact valid values, such as pending, shipped, delivered, and cancelled, so the model picks from a closed list. Second, improve the property description to say what the values mean, for example that "shipped" covers orders that are in transit. Optionally set additionalProperties to false and validate the value in your code as well, because the schema alone does not guarantee the model's output.
Exercise
You have two tools, search_orders (find orders by email) and get_order (fetch one order by ID). The model often calls search_orders even when the user supplies an order ID. How would you rewrite the descriptions?
Show answer
State the boundary in both descriptions. For search_orders: "Search orders by customer email. Use only when you do not have an order ID. Use getorder when you do." For `getorder`: "Fetch one order by its order ID. Use this whenever the user provides an order ID." Each description names the other tool and the condition that selects it, so the model no longer has to guess.