Skip to content

Tool design for agents

How to write tool contracts an agent can use reliably: narrow schemas, clear errors and least privilege.

6 MIN READUPDATED 2026-11-18Context & protocols
ORIGINAL DIAGRAM · API GATEWAYCLIENTSweb / appGATEWAYAUTHENTICATERATE LIMITROUTESERVICE AordersSERVICE Busers

Original gateway diagram — not a copied image

§ 00Notes & rules
Validate server-side
Never trust tool arguments because the model 'should' follow the schema. Validate types, enums, ranges and permissions in your application before executing any action.
Describe when not to use a tool
Good tool descriptions reduce misfires. Include boundaries, required preconditions and examples of cases where a different tool is the right choice.
§ 01Key concepts
Tool contractThe name, description, input schema and output shape exposed to the agent.
Idempotency keyA stable identifier that makes repeated calls safe to retry.
Tool resultStructured output returned to the model, including errors.
§ 02When to use
  • An agent must call business systems.
  • Tool calls are unreliable or duplicated.
  • Permissions differ across operations.
§ 03How it works
  1. 01Define one narrow tool per intent, with a typed input schema.
  2. 02Write descriptions that say when not to use the tool.
  3. 03Return structured errors the model can reason about.
  4. 04Add idempotency and server-side validation before execution.
§ 04Use cases & scenarios

Order lookup with a narrow schema

Context
An agent needs to fetch an order by ID.
Problem
It guesses IDs and calls the tool with malformed arguments.
Approach
Constrain the ID with a pattern, describe when not to use the tool and return actionable errors.
Outcome
Fewer invalid calls and clearer recovery when an order is missing.

Retry-safe refund tool

Context
A refund call may be retried after a network timeout.
Problem
A retry could issue the same refund twice.
Approach
Require an idempotency key and return an explicit success state.
Outcome
Safe retries without duplicate payouts.
§ 05Examples

Typed tool contract

json
{
  "name": "lookup_order",
  "description": "Fetch an order by ID. Use only when the user provides an order ID. Do not guess IDs.",
  "input_schema": {
    "type": "object",
    "properties": {
      "order_id": { "type": "string", "pattern": "^ORD-[0-9]{6}$" }
    },
    "required": ["order_id"],
    "additionalProperties": false
  }
}

Narrow schemas, enums/patterns and `additionalProperties: false` make validation failures actionable.

Return actionable errors

json
{
  "ok": false,
  "error": {
    "code": "ORDER_NOT_FOUND",
    "message": "No order matches ORD-104200.",
    "retryable": false,
    "next_step": "Ask the user to confirm the order ID."
  }
}
§ 06Trade-offs

Many small tools

Up: Clear choices, easy permissions

Down: More context to describe them

Few broad tools

Up: Compact context

Down: Ambiguous arguments and wide permissions

Free-form arguments

Up: Flexible

Down: Frequent validation failures

§ 07Implementation checklist
  • Use descriptive tool names with verbs.
  • Include units, formats and examples in the schema.
  • Make errors actionable: what went wrong and what to try instead.
  • Deny unknown fields in validation.
§ 08Configuration reference
KeyTypical valueNote
tool_scoperead/write splitGrant per role.
timeoutshortFail fast and retry safely.
idempotencyrequired for writesPrevents duplicate actions.
§ 09Troubleshooting
SymptomCauseFix
Agent calls a tool repeatedlyAmbiguous result or no idempotencyReturn explicit success state and require idempotency keys.
Invalid argumentsWeak schema descriptionAdd types, enums and examples.
§ 10Frequently asked questions

How many tools should an agent see?

Only those needed for the current step. Too many choices degrade tool selection.

Should errors be hidden from the model?

No. Structured, actionable errors help the agent recover or ask for help.

More in Context & protocols