Sub topic 12
Designing tools the model uses well
This chapter assumes you can already write a tool definition with JSON Schema, run the tool-calling loop, and return results tagged with a call id. Here the question changes from "does it work?" to "will the model pick this tool, with these arguments, at the right time?"
The model only sees what you write
The model never sees your Python code. For each tool it sees three things: the name, the description, and the parameter schema. It also sees whatever your function returns. Every design choice in this chapter changes one of those four things.
If two tools look alike in those four places, the model will confuse them. If a parameter is vague, the model will guess. Treat a tool definition as an interface written for a reader who is capable but has no context about your system.
Names
Choose a name that states the action and the object, such as search_orders, get_invoice, or cancel_subscription.
- Use a verb plus a noun. A name like
ordersorhandlesays nothing about what happens. - Use the same verbs everywhere. If one tool says
get_and another saysfetch_for the same kind of operation, the model has to work out whether they differ. - Avoid names that overlap.
search_docsandfind_documentswill be mixed up. - Stay inside the character limits and allowed characters of your provider. Letters, digits, underscores, and hyphens are safe.
Descriptions
The description is the main signal the model uses to decide whether to call the tool. A good description answers four questions:
- What does the tool do?
- When should the model use it?
- When should it not use it, and what should it use instead?
- What does it return, and what are the limits?
Compare these two definitions of the same tool.
{
"name": "search",
"description": "Searches things.",
"parameters": {
"type": "object",
"properties": {
"q": {"type": "string"},
"n": {"type": "integer"}
},
"required": ["q"]
}
}{
"name": "search_orders",
"description": "Find a customer's past orders by status or date range. Use when the user asks about order history or the state of an order. Do not use for product questions; use search_products for those. Returns at most 20 orders, newest first, each with id, status, total, and date.",
"parameters": {
"type": "object",
"properties": {
"customer_id": {"type": "string", "description": "Customer id, for example C-10442. Ask the user if unknown."},
"status": {"type": "string", "enum": ["pending", "shipped", "delivered", "cancelled"]},
"since": {"type": "string", "description": "Earliest order date as YYYY-MM-DD"},
"limit": {"type": "integer", "minimum": 1, "maximum": 20, "description": "Number of orders to return, default 10"}
},
"required": ["customer_id"],
"additionalProperties": false
}
}The second version tells the model what the tool covers, where its boundary is, and what shape the output has. It also tells the model what to do when it lacks a value (Ask the user if unknown), which prevents invented ids.
Parameters
Parameters are where most wrong calls come from. Apply these rules.
- Use enums for closed sets. A model can pick from
["pending", "shipped"]reliably, but it will invent spellings for free text. - Describe format and give one example for every string with a convention: dates, ids, currency codes.
- Set bounds with
minimum,maximum, andmaxLengthso your validator can reject nonsense. - Mark
requiredonly for values the call cannot work without. Give optional values safe defaults in your code and state the default in the description. - Keep the count small. Three or four parameters are easy to fill in. Ten parameters with interdependencies produce invalid combinations.
- Do not ask the model for values your code already knows. The signed-in user's id should come from your session, not from a model-generated argument. This is also a security rule, covered in the security chapter.
- Prefer names the model can understand without context.
sincewith a description beatsdt1.
Granularity: how many tools, how big
Two failure modes sit at opposite ends.
| Design | Typical problem | Example |
|---|---|---|
| Too fine-grained | Many calls per task, slow and costly, easy to break the order | `open_file`, `read_line`, `close_file` |
| Too coarse | One tool with a `mode` argument and many unrelated option combinations | `database(action, table, filter, payload, ...)` |
| About right | One tool per task the user would recognize | `get_invoice`, `list_invoices`, `void_invoice` |
Two practical guides:
- Merge steps that always occur together. If the model must call
lookup_customer_idbefore everyget_orders, accept an email address inget_ordersand do the lookup in your code. - Split tools whose arguments change meaning by mode. Separate tools give each description a clear job.
Keep the total number of tools modest. Every definition costs input tokens on every request, and a long list makes selection less accurate. If you have dozens of tools, expose only those relevant to the current task or user.
Separate reading from changing
Mark in the description whether a tool only reads or also changes state. Then enforce the difference in code.
- Read-only tools can be retried and called in parallel without harm.
- Tools that change state should be idempotent when possible. Accept a request key so a repeated call does not charge a card twice.
- For irreversible actions such as deletes, payments, or sends, have your code require a confirmation step from the user before executing. Do not rely on the model to be careful.
Shape what the tool returns
The result goes back into the conversation, so it takes up context and steers the next step.
- Return only the fields the model needs. Raw database rows with 40 columns waste tokens and bury the answer.
- Include identifiers the model might need for a follow-up call.
- Use readable values:
"status": "shipped"rather than"status": 3. - Cap list sizes and say so in the result, so the model knows the answer is incomplete.
- Keep the format consistent across tools.
import json
ALLOWED_FIELDS = ("id", "status", "total", "date")
def shape_orders(rows, limit=20):
trimmed = [{k: row[k] for k in ALLOWED_FIELDS} for row in rows[:limit]]
return json.dumps({
"orders": trimmed,
"returned": len(trimmed),
"total_matches": len(rows),
"truncated": len(rows) > limit,
})
rows = [
{"id": "O-1", "status": "shipped", "total": 42.5, "date": "2026-11-02", "internal_note": "vip"},
{"id": "O-2", "status": "pending", "total": 9.0, "date": "2026-11-20", "internal_note": ""},
]
print(shape_orders(rows))The allow-list also keeps internal fields such as internal_note out of the model's view.
Errors that teach
An error is part of the tool's interface. A message the model can act on lets it fix the call on the next step. Compare "Error 500" with "No customer with id C-999. Ids look like C-10442. Ask the user to confirm the id." The second tells the model what is wrong and what to do next. Do not put stack traces, file paths, or credentials in error text.
def get_customer(customer_id):
if not str(customer_id).startswith("C-"):
return {
"is_error": True,
"content": "customer_id must look like C-10442. Ask the user for the id.",
}
return {"is_error": False, "content": '{"id": "%s", "name": "Ada"}' % customer_id}
print(get_customer("10442"))Improve tools by reading transcripts
Tool design is iterative. When the model misuses a tool, change the definition and not only the prompt.
Common mistakes
- Descriptions that say what the tool is called and nothing else.
- Two tools with overlapping purposes and no note on which to prefer.
- Free-text parameters where an enum would do.
- Asking the model to pass identity or permission values that your code can supply.
- Returning whole records, then wondering why cost and latency rise.
- Fixing a recurring bad call by lengthening the system prompt instead of the tool definition.
Exercise
A tool named data has one parameter, query, a free-text string, and the description "Gets data." The model calls it for order questions, product questions, and refund requests, and often passes malformed queries. Name three changes that would improve it.
Show answer
First, split it into tools with specific names, such as search_orders, search_products, and request_refund, each with a description stating when to use it and when not to. Second, replace the free-text query with typed parameters, using enums for closed sets and a format description with an example for ids and dates. Third, improve the returned errors, so that a bad argument produces a message saying what format is expected. Mark request_refund as state-changing and require user confirmation in your code before it runs.