Advanced about 4 h
The picture
01
Purpose
one clear job
02
Name + description
when to use
03
Arguments
typed, constrained
04
Result
compact, useful
05
Errors
actionable text
06
Limits
permissions, sandbox
Tools are an interface for a model
Treat the tool set as the agent-computer interface and give it the care you would give a human UI. Models read names, descriptions and return values literally, and they only know what you write there.
Principles
- One clear job per tool.
search_orders(customer_id, status)beatsmanage_orders(action, ...). - Names and descriptions say when to use it and when not to. Include an example and the boundary with similar tools.
- Constrain arguments. Enums for closed sets, ranges for numbers, formats for dates. Fewer free-text parameters means fewer mistakes.
- Return what the model needs, not everything you have. Human-readable names beat raw ids; trim to the relevant fields; paginate with a clear "more available" signal.
- Make errors teach. "Invalid date" is a dead end. "Date must be YYYY-MM-DD, e.g. 2025-03-14. You sent '14/03/25'" lets the model fix itself.
- Prefer idempotent operations, and require an idempotency key for ones with side effects.
# Weak
{"name": "db", "description": "Run a database thing", "input_schema": {"type": "object", "properties": {"q": {"type": "string"}}}}
# Strong
{
"name": "search_invoices",
"description": "Find invoices for ONE customer. Use for questions about what a customer owes or has paid. "
"Do not use for aggregate reporting (use run_report). Returns at most 20 rows, newest first; "
"if 'has_more' is true, call again with the next 'cursor'.",
"input_schema": {
"type": "object",
"properties": {
"customer_id": {"type": "string", "description": "Customer id such as 'C-10442'. Look it up with find_customer if unknown."},
"status": {"type": "string", "enum": ["open", "paid", "overdue"]},
"cursor": {"type": "string", "description": "Pagination cursor from a previous result."}
},
"required": ["customer_id"]
}
}
Errors the model can act on
def search_invoices(customer_id: str, status: str | None = None, cursor: str | None = None):
if not re.fullmatch(r"C-\d{4,6}", customer_id):
raise ToolError(f"customer_id must look like 'C-10442', got {customer_id!r}. Use find_customer(name) to look it up.")
rows = db.query(customer_id, status, cursor)
return {"rows": rows[:20], "has_more": len(rows) > 20, "cursor": rows[19]["id"] if len(rows) > 20 else None}
Permissions and sandboxing
A tool is a capability you hand to a system that can be tricked. Apply least privilege:
- Read-only database roles for query tools; row limits and timeouts.
- Scoped credentials per tool, never the application's admin key.
- Sandboxed execution (containers, no network, resource limits) for code or shell tools.
- Path allow-lists for file tools; resolve symlinks and reject
... - Human approval for irreversible or costly actions, enforced by your code, not by asking the model nicely.
Test tools with the model
Write 10 questions that should trigger a tool and 5 that should not. Run them and read the traces. If the model picks the wrong tool, fix the description before changing the prompt.
Common mistakes
- Overlapping tools whose descriptions the model cannot tell apart.
- Returning raw database rows with internal columns and ids.
- Giving a shell tool full access "just for now".
Exercises
- Take a tool with a one-line description, rewrite it using the principles, and measure selection accuracy on 15 prompts before and after.
- Add path validation to a
read_filetool and write tests for traversal attempts.
You are done when you can…
- Rewrite a poor tool definition so a model uses it correctly in a held-out test
- Design error messages that let the model recover without human help
- Choose sandbox and permission boundaries for a tool that touches files or the network