Sub topic 6
Designing tools a model can use well
This chapter assumes you can write Python, handle JSON, and have seen the tool registry and run_tool function from the tool-calling chapter. A model never sees your code. It sees only a tool's name, description, and argument schema, and it chooses actions from those three things. Tool design is therefore prompt design.
Name and describe the tool
A good name is a verb phrase that says what the tool does, such as get_weather or search_orders. Avoid vague names like process or helper.
The description should state three things:
- What the tool does.
- When to use it, and when not to.
- What it returns.
A weak description reads "Weather tool." A stronger one reads "Returns the current temperature in Celsius for one city. Use it for current conditions only, not forecasts."
Define a precise argument schema
Give every argument a type and a short description. Use an enum when only a few values are valid. Mark required arguments. Fewer arguments mean fewer mistakes.
GET_WEATHER_SPEC = {
"name": "get_weather",
"description": (
"Returns the current temperature in Celsius for one city. "
"Use for current conditions only, not forecasts."
),
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "City name in English, for example 'Oslo'.",
}
},
"required": ["city"],
},
}Return results the model can use
A tool result goes back into the context window, so it should be short and clear. Return only the fields the model needs. Use readable names such as temp_c instead of codes. Truncate long lists and say so, for example "showing 10 of 240 results".
Errors are results too. The run_tool function from the earlier chapter returns {"error": ...} as text. Make that text useful: "unknown city: 'Osl'. Did you mean 'Oslo'?" lets the model fix its own call. A bare "failed" gives it nothing to work with.
Keep the tool set small and distinct
Tools with overlapping purposes make the model guess. If search_docs and find_documents do nearly the same thing, merge them. Many agents work best with a handful of well-separated tools. Prefer one tool that does a complete task over several tiny steps the model must chain correctly.
Match risk to the tool
Separate read-only tools from tools that change things. A tool like delete_file should be narrow and, later in this tutorial, gated by approval. Do not give a model one general run_sql tool when a get_order_status tool covers the real need.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Vague name or description | Model picks the wrong tool | Say what, when, and returns |
| Untyped or undocumented arguments | Malformed calls | Add types, descriptions, enums |
| Huge raw API responses | Wasted context, confusion | Return only needed fields |
| Errors like "failed" | Model cannot recover | Explain the cause and a fix |
| Overlapping tools | Inconsistent choices | Merge or clearly separate |
Exercise
A tool named query has the description "Runs a query" and returns the raw 5,000-line database response. Name three changes that would make it easier for a model to use.
Show answer
Rename it to say what it queries, for example search_orders, and describe what it does, when to use it, and what it returns. Return only the needed fields and truncate long results with a note on how many were omitted. Add typed, documented arguments, with an enum where the values are limited, and validate them inside the tool.