Sub topic 13
Returning errors the model can act on
This chapter assumes you know the tool-calling loop, can read a JSON Schema, and can read short Python examples. A tool will sometimes fail, because the model sent a bad ID, the record does not exist, or a backend timed out. What your tool returns at that moment decides whether the model recovers or gives up.
Why error text is part of your design
The model reads an error result as the next piece of input. It has no stack trace and no access to your code. A message like "Error" or "400 Bad Request" gives it nothing to change on the next attempt. It will often retry the same call, invent a value, or tell the user something went wrong without saying what.
A recoverable error answers three questions:
- What was wrong, including the value that was rejected.
- What a correct value looks like.
- What to do next, such as which tool to call or whether to ask the user.
A recoverable error in code
The get_order tool below checks its argument on the server and returns messages that follow those three points.
ORDERS = {
"ord_1001": {"id": "ord_1001", "status": "shipped", "total": 42.5},
}
def get_order(order_id: str) -> dict:
if not isinstance(order_id, str) or not order_id.startswith("ord_"):
return {
"is_error": True,
"message": (
f"Invalid order_id {order_id!r}. IDs look like 'ord_1001'. "
"If you do not have an ID, call search_orders first."
),
}
order = ORDERS.get(order_id)
if order is None:
return {
"is_error": True,
"message": f"No order found with id {order_id!r}. Check the ID with the user or use search_orders.",
}
return {"is_error": False, "order": order}The two failures are different, and the messages reflect that. A malformed ID is a format problem, so the message shows the format. A well-formed ID that matches nothing is a lookup problem, so the message suggests asking the user or searching.
Flagging the result as an error
Most provider APIs let you mark a tool result as an error. In Anthropic's API this is the is_error field on a tool_result block. Setting it tells the model the call failed. Without it, the model may treat your message as a normal answer. Check your provider's documentation for the exact field name.
Distinguishing kinds of failure
Different failures call for different next steps. Say which kind it is.
| Failure | What the message should say |
|---|---|
| Invalid argument | The rejected value, the expected format, an example |
| Not found | That the lookup ran and found nothing, and an alternative tool |
| Permission denied | That retrying will not help, and that the user should be told |
| Temporary backend failure | That a retry may work |
What to leave out
- Stack traces, file paths, and internal hostnames. They cost tokens and can expose details of your system.
- Raw upstream error bodies. Summarize them.
- Instructions that go beyond the tool's own scope. Point to another tool or to the user, and let the model decide.
Exercise
A tool returns {"is_error": true, "message": "Not found"} when the model passes ord_9999. Rewrite the message so the model can recover.
Show answer
For example: "No order found with id 'ord9999'. Check the ID with the user or use searchorders." It names the rejected value, says the lookup ran and found nothing, and gives two next steps.
Exercise
Why should a permission-denied error say that retrying will not help?
Show answer
Without that statement the model may retry the same call several times. Saying so lets it stop and tell the user instead.