AI Agent Tool Calling Best Practices
Give an agent five tools and it either ignores them or calls them with the wrong IDs. Tool calling is not just registering functions - descriptions, schemas, error shapes and confirmations all decide the outcome.
📜 Table of Contents
📖 Keep reading
Tool Calling Best Practices: Making an Agent Use Your API Correctly
Five tools, and the agent answers from memory instead of querying, writes when it should read, drops a digit from an order ID, then retries the same failure three times. That is not a dumb model - the tool layer never gave it enough information.
Seven fixes, ordered by cost of change.
1. Say when NOT to use a tool
The model chooses from names and descriptions. "Query order" is half the information.
Bad: Query order information
Good: Look up status, amount and shipment for an order by order ID.
Use only when the user provides a full order ID.
If they give a phone number or name, use search_orders_by_customer.
Does not return live stock - use check_stock.
Change: every description gets applicable and non-applicable conditions plus the alternative tool. This single edit usually produces the biggest drop in wrong-tool calls.
2. Lock parameters with JSON Schema
{"name":"refund_order",
"parameters":{"type":"object","properties":{
"order_id":{"type":"string","pattern":"^ORD-[0-9]{10}$"},
"reason":{"type":"string","enum":["damaged","late","wrong_item","customer_request"]},
"amount_cents":{"type":"integer","minimum":1}},
"required":["order_id","reason","amount_cents"],"additionalProperties":false}}
Change: enums over free text, pattern where a format exists, money as integer minor units.
3. Cap the tool count and load in groups
Past roughly 8-10 tools, selection accuracy degrades - the model compares near-identical descriptions. Namespace by domain (billing_, shipping_) and load non-core groups on demand, which in MCP terms means mounting servers per session.
4. Errors the model can act on
{"ok":false,"error":{"code":"ORDER_NOT_FOUND","message":"No such order",
"hint":"Ask the user to check the ID, or call search_orders_by_customer",
"retryable":false}}
Change: every error code carries a hint naming the next tool or question, plus a retryable flag. Without it, models retry non-retryable errors until timeout.
5. Confirmation and idempotency for writes
- Confirm before acting: restate object and amount to the user, execute after approval
- Idempotency: same
request_idreturns the first result, never a second refund
def refund(order_id, amount_cents, request_id):
if seen(request_id):
return cached_result(request_id)
return do_refund(order_id, amount_cents, request_id)
Change: add request_id to every side-effecting tool, enforce limits in the tool layer (NEED_HUMAN_APPROVAL above a threshold), not in the prompt.
6. Parallel calls, timeouts, retries
Reads: 8-10s timeout, two retries with backoff. Writes: short timeout, no automatic retry unless idempotent. Parallelize only independent reads.
7. Log calls as data
Tool name, redacted arguments, latency, success, error code, session. A week of that tells you which tool fails, which calls the model invents, and which field it keeps omitting.
Combined effect
| Change | Typical result |
|---|---|
| Descriptions only | Fewer wrong-tool selections |
| + schemas | Far fewer malformed arguments |
| + error hints | Retry loops mostly disappear |
| All seven | Higher success rate, auditable failures |
FAQ
More tools, stronger agent? Past a point, the opposite - similar tools interfere.
Let the model write SQL? Fixed-parameter tools are safe but narrow; open code execution is flexible but risky. Common queries get tools, long-tail goes to a sandbox, both audited.
Model refuses to call tools? Check descriptions and add "when unsure, look it up" to the system prompt; as a last resort force tool_choice.
Note: provider support for tool calling differs - see official docs.
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.