Tools are the agent's hands and eyes. The model never sees your code, only each tool's name, description and argument schema, and then whatever text the tool returns. That makes tool design a kind of interface design for a very literal, very fast reader who cannot ask questions. Good tools are few, clearly named, hard to misuse, and return results that make the next step obvious. This lesson covers tool definitions and schemas, how to choose the right level of abstraction, designing results and errors, safety properties such as idempotency and dry runs, and how to evaluate tools.
- Write tool definitions with clear names, descriptions and JSON schemas
- Choose task-level tools over thin wrappers around low-level APIs
- Design concise, structured results and actionable error messages
- Make tools safe with validation, idempotency, limits and dry runs
- Evaluate and iterate on tools using agent transcripts
What the model actually sees
A tool is offered to the model as a name, a natural-language description and a JSON Schema for its arguments. The model decides whether to call it and with what arguments using only that text. Treat the description like documentation for a new colleague: say what the tool does, when to use it and when not to, what the arguments mean with formats and examples, and what it returns.
{
"name": "orders_search",
"description": "Find a customer's orders (max 10, newest first). Use before a refund.",
"input_schema": {
"type": "object",
"properties": {
"customer_email": {"type": "string", "format": "email"},
"status": {"type": "string", "enum": ["open", "shipped", "delivered", "refunded"]},
"placed_after": {"type": "string", "description": "ISO date, e.g. 2026-09-01"}
},
"required": ["customer_email"],
"additionalProperties": false
}
}- Use enums for closed sets so the model cannot invent values.
- Mark required fields and forbid extra properties.
- Give formats and examples for dates, IDs and units.
- Prefer unambiguous names with a namespace prefix (
orders_search,orders_refund) when you have many tools.
The right level of abstraction
Wrapping every REST endpoint as a tool is tempting and usually wrong. Low-level tools force the agent to chain many calls, carry IDs between them and handle pagination, and every extra step is a chance to fail and a cost in tokens. Design tools around the tasks the agent performs. If agents always look up a customer, then their orders, then the shipment, a single orders_search returning the joined, relevant fields serves them better.
| Low-level | Task-level |
|---|---|
| list_users, get_user, list_orders, get_shipment | orders_search(customer_email, status) |
| read_file with no limits | read_file(path, offset, limit) with line numbers |
| run_sql(query) | orders_report(period, group_by) with a fixed safe query |
| send_http(method, url, body) | tickets_create(title, priority, body) |
Fewer tools also help selection: when two tools overlap, the model has to guess, and it will guess differently on different runs. Merge or remove overlapping tools before writing longer descriptions to tell them apart.
Results and errors that drive the next step
The tool result is the agent's only view of what happened. Return what the next decision needs and little else: relevant fields rather than whole records, human-readable names next to opaque IDs, and a clear note when results were truncated and how to get more. Errors deserve the same care. A stack trace or "400 Bad Request" leaves the agent guessing; an error that says what was wrong and how to fix it usually leads to a correct retry.
from pydantic import BaseModel, EmailStr, ValidationError
class SearchArgs(BaseModel):
customer_email: EmailStr
status: str | None = None
def orders_search(raw_args: dict) -> str:
try:
args = SearchArgs(**raw_args)
except ValidationError as e:
msg = e.errors()[0]["msg"]
return f"Invalid arguments: {msg}. Pass customer_email as an email address."
rows = db.find_orders(args.customer_email, args.status)
if not rows:
return f"No orders for {args.customer_email}. Check spelling or ask for the order ID."
lines = [f"{r.id} | {r.placed:%Y-%m-%d} | {r.status} | {r.total:.2f}" for r in rows[:10]]
more = ""
if len(rows) > 10:
more = f"\n({len(rows) - 10} older orders not shown; narrow with status)"
return "id | placed | status | total\n" + "\n".join(lines) + moreSafety properties
- Validate everything on the server side; the schema guides the model but does not enforce anything.
- Idempotency: write tools accept an idempotency key or check current state, so a retried call does not refund twice.
- Limits: cap amounts, rows, file sizes and time; reject requests outside policy with a clear message.
- Dry runs and previews: for consequential actions, a
previewmode returns what would change, which a human or the agent can confirm. - Least privilege: tools act with the end user's permissions, not a superuser account.
- Mark side effects: label tools read-only or write so the permission layer can treat them differently.
Evaluating and iterating on tools
Tools are evaluated through the agent. Run a benchmark of realistic tasks and read the transcripts with tool design in mind: which tools were never used, which were used wrongly, which arguments were invalid, where the agent called three tools in a row to answer one question, and which results it misread. Count invalid-argument rates and calls per task. Then change one tool description, schema or result format at a time and re-run. Small edits to a description often fix errors that look like model failures.