Structured output and tools Recommended
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.
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.` } };
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.
For agents: if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in Based on the Agent Wranglers ai features playbook: https://agentwranglers.se/playbook/ai-features/structured-output-and-tools/tool-errors-for-models.