
Designing Tools for Claude: Why Thin API Wrappers Make Weak Agent Tools
- كاتب موثّق
- Claude API
محتويات المقال
get_customers looking for Dana, pulls her entire transaction history, then the notes, and after a dozen calls full of UUIDs it reports a refund that belongs to a different customer. Every endpoint worked. The tools still failed.
The common misconception
A client library mirrors the API it calls, so a tool set that mirrors the API feels just as correct. Generators turn an OpenAPI file into tools in minutes, and full coverage feels thorough. The endpoints are already tested, documented and permissioned, so reusing them seems safest. More tools sound like more capability.
What actually happens
A tool is a contract with a non-deterministic caller
Context is the scarce resource
list_contacts tool forces the agent to read contacts one by one, like reading the address book from the first page. A search_contacts tool lets it jump to the right page. And whatever the tool returns stays in the conversation, re-sent with every later turn.
Shape tools around tasks, not routes
schedule_event, which finds availability and books, instead oflist_users,list_eventsandcreate_event.search_logs, which returns relevant lines with some context, instead ofread_logs.get_customer_context, which compiles a customer's recent, relevant information, instead ofget_customer_by_id,list_transactionsandlist_notes.
github_list_prs.
Return what the next step needs
uuid, 256px_image_url and mime_type in favor of name, image_url and file_type, and reports that resolving arbitrary UUIDs to meaningful names "significantly improves Claude's precision in retrieval tasks by reducing hallucinations." When a later call needs an ID, a response_format parameter with concise and detailed options lets the agent ask for it. Large results get pagination, filtering or truncation with sensible defaults, and errors should say what to change instead of returning an opaque code or a traceback.
The CRM, before and after
# Generated from openapi.yaml: one tool per endpointTOOLS = [ {"name": "get_customers", "description": "GET /customers", "input_schema": {"type": "object", "properties": {"page": {"type": "integer"}}}}, {"name": "get_customer_transactions", "description": "GET /customers/{id}/transactions", "input_schema": {"type": "object", "properties": {"id": {"type": "string"}}, "required": ["id"]}}, {"name": "get_customer_notes", "description": "GET /customers/{id}/notes", "input_schema": {"type": "object", "properties": {"id": {"type": "string"}}, "required": ["id"]}}, # ...and one more for every other route in the spec]CUSTOMER_CONTEXT_TOOL = { "name": "crm_get_customer_context", "description": ( "Finds one customer by name or email and returns their profile, " "last 20 transactions and open support notes in a single call. Use " "it before answering any question about a specific customer's " "billing or history. If several customers match, it returns up to " "5 candidates instead, so ask the user which one they mean. Set " "response_format to 'detailed' only when a next step needs IDs." ), "input_schema": { "type": "object", "properties": { "customer": {"type": "string", "description": "Full name or email address"}, "response_format": {"type": "string", "enum": ["concise", "detailed"]}, }, "required": ["customer"], },}def crm_get_customer_context(customer: str, response_format="concise"): matches = crm.search_customers(customer, limit=5) # never the full list if len(matches) != 1: found = "; ".join(f"{m.name} <{m.email}>" for m in matches) or "none" return f"{len(matches)} matches for {customer!r}: {found}. Ask which." c = matches[0] lines = [f"{c.name} <{c.email}>, customer since {c.since}"] lines += [f"{t.date} {t.amount} {t.status}" for t in crm.list_transactions(c.id, limit=20)] lines += [f"note: {n.text}" for n in crm.list_notes(c.id, status="open")] if response_format == "detailed": # IDs only when a next call needs them lines.append(f"customer_id: {c.id}") return "\n".join(lines)print(crm_get_customer_context("Dana Levi"))How to apply it in your project
List tasks, not endpoints Collect the questions people really ask your assistant, from logs or support tickets, and design one tool per frequent workflow. Merge the chains If the agent always calls three tools in a row, make one tool that does all three against your API. Replace lists with search Give every collection a search with filters, a small default limit and a hint when results are truncated. Trim and humanize responses Return names and the fields the next step needs, and offer IDs through a response_formatparameter.Write descriptions for a new hire Explain what the tool does, when to use it, what each parameter means and what it won't return, and give parameters unambiguous names such as user_idinstead ofuser.
Key takeaways
A tool's caller is a model reading every token of its definitions and responses, not a program with cheap memory. Calling your API inside a tool is fine; mirroring one endpoint per tool is the mistake. Consolidate chained calls into task-shaped tools, and replace list endpoints with search. Return high-signal fields and readable names, with IDs and detail only on request. Describe each tool like you'd brief a new colleague, then improve it against an evaluation.
التعليقات (0)