AIF-16Recommended
Return errors the model can act on
When a tool fails, return a short structured error that says what went wrong and what to try next. Never return a stack trace or an empty result.
How to do it
Return an object with an error code, a plain message and, where it helps, the allowed values. Mark the result as an error in the provider's format, such as is_error: true on an Anthropic tool_result block. Answer every tool call the model made, including each of several parallel calls, in the next request. A missing result makes the provider reject the request. Keep internal details, such as table names and hosts, in your own logs. Return "no results" as an explicit message, so the model does not guess. Problem details give the same fields to every client.
Examples
TypeScript example
if (!order) return { isError: true, content: { code: 'order_not_found', message: `No order ${orderNumber} for this customer. Ask the customer to check the number.` } };
Python example
if order is None:
return {"is_error": True, "content": {"code": "order_not_found",
"message": f"No order {order_number} for this customer. Ask the customer to check the number."}}
How to check
A test calls each tool with a bad argument and with a missing record. Both results are short JSON objects with a code and a message, and neither contains a stack trace or file path. A test with two parallel tool calls, one failing, sends back two results.
Sources
Related rules
- API-16 Send errors as problem details in API design
- AGT-16 Report bad arguments as tool results instead of protocol errors in Agents as users
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/ai-features/structured-output-and-tools/tool-errors-for-models">Agent Wranglers AI features playbook</a>. In code you write, add a comment next to it with the same link.