Return an Error the Model Can Fix
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.
- On success, return
{"type": "tool_result", "tool_use_id": block.id, "content": str(result)}. - On any failure, return the same shape with
"is_error": Trueand your message ascontent.tool_resultnever raises. - 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 areORD-and five digits. Customers often give only the digits, and the model can fix that itself.NoSuchOrder(.order_id): no such order.find_orderslists 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.
- Anything else is an
is_errorresult with nothing from the exception in it. An unknown tool: name the tools that exist. Wrong arguments (aTypeError): 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.