Skip to content

Write tool definitions Claude can pick and recover from

Write a tool definition whose description says when to use it and how it differs from its neighbours, with a precise input schema, a handler that returns failures as error tool results, and approval before side effects.

Scenario

Your support assistant has two tools: orders ("Gets an order.") and shipments ("Gets shipment info."). A customer pastes a tracking number and asks where the parcel is.

Claude calls orders with the tracking number as the order ID. Your handler throws OrderNotFound, the loop crashes, and the customer sees a generic error page.

The model was fine. The tool definitions gave it nothing to choose with, and the handler gave it nothing to recover with.

Explanation

Claude never sees your handler. It sees the name, the description and the input_schema, and it decides from those alone whether and how to call the tool. The tool-use docs call detailed descriptions "by far the most important factor in tool performance".

1. The description says when, and when not. Aim for at least three or four sentences:

  • what the tool does and what it returns (and what it does not return)
  • when to use it, in the user's terms ("when the customer gives a tracking number")
  • when to use a neighbouring tool instead: overlapping tools are the main reason Claude picks the wrong one
  • caveats: formats, limits, slow or costly calls

2. The schema is precise. Name parameters unambiguously (order_id, not id), describe each one with a format example, use enum for a fixed set of values, and list what is required. Prefix names with the service (shop_get_order) once tools span several systems. Setting strict: true makes Claude's inputs always match the schema.

3. Failures come back as results, not exceptions. When the handler fails, return a tool_result with "is_error": true and a message that says what went wrong and what to try next ("No order with that ID. Ask the customer to check it."). Claude can then retry, try another tool or explain. A crash or an empty string gives it nothing.

4. Side effects wait for approval in code. You execute client tools, so your loop can stop before a refund, an email or a deletion, ask a person, and send the decision back as the tool_result. An instruction in the prompt is a request; a check in the loop is a guarantee. For human-in-the-loop approval, the docs point you to a manual loop rather than the SDK's tool runner.

Worked example

The shipments tool from the hook, before:

{
  "name": "shipments",
  "description": "Gets shipment info.",
  "input_schema": {
    "type": "object",
    "properties": { "id": { "type": "string" } }
  }
}

After, the description (one string in the definition):

Returns the carrier, current status and latest scan location for one parcel, looked up by its tracking number (for example 1Z999AA10123456784). Use it when the customer gives a tracking number or asks where a parcel is. If they give an order number (ORD-12345) instead, use shop_get_order, which returns the order's items and its tracking number. It does not return order contents or prices.

And the schema:

{
  "name": "shop_track_shipment",
  "strict": true,
  "input_schema": {
    "type": "object",
    "properties": {
      "tracking_number": {
        "type": "string",
        "description": "Carrier tracking number"
      }
    },
    "required": ["tracking_number"]
  }
}

What changed: a namespaced name, a description that names its neighbour and the input that decides between them, a parameter name that says what goes in it, and a required field. The handler now catches ShipmentNotFound and returns "Unknown tracking number. Check the digits, or look up the order with shop_get_order." with is_error set.