Skip to content

< CollectionsWriting effective tools for agents · 5 of 8 ·104 · Level 04, Tool Calling

Return an Error the Model Can Fix

medium · debug · Tool Calling

Fix tool_result(block). It runs one tool call from a real model and returns the block that goes back. When a tool fails, that block is all the model learns, and today it holds str(error).

block has .id, .name and .input. TOOLS (provided) maps a name to a function.

  1. On success, return {"type": "tool_result", "tool_use_id": block.id, "content": str(result)}.
  2. On any failure, return the same shape with "is_error": True and your message as content. tool_result never raises.
  3. Four errors come out of the tools. Their str(error) is for a log (E1042, a database address with a password in it), so never send it. Write from what the error means:
    • BadOrderId (.got): the wrong form. Order ids are ORD- and five digits. Customers often give only the digits, and the model can fix that itself.
    • NoSuchOrder (.order_id): no such order. find_orders lists a customer's orders, if the model has the email.
    • OrdersDown: briefly unavailable. Nothing is wrong with the request, and the same call will work.
    • OverLimit (.limit_cents): no refund was made. Over the limit needs a person's approval, so no retrying or splitting. The customer should hear the limit, in dollars.
  4. Anything else is an is_error result with nothing from the exception in it. An unknown tool: name the tools that exist. Wrong arguments (a TypeError): name the arguments the tool takes. A bug in a tool: say retrying will not help.

The catch. Describing the failure is not enough. Told Order ids look like ORD-12345, this model asks the customer to type the number again. Told Service unavailable, it gives up. Write each message as an instruction: what happened, and what to do now.

The tests run a real model on your tool_result: it must find the status after a bad id, call get_order again after an outage, name the order find_orders lists after a missing one, and tell the customer the limit with no second try.