By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
The order service takes five seconds to time out. Your agent calls lookup_order, waits, calls it again, waits, and calls it a third time. The customer waits fifteen seconds for a message that says something went wrong.
The tool did fail. The failure was not the real problem. The wrapper raised a Python exception, the loop treated it as a crash, and the model never saw a result it could reason about.
It is terminal. The order will not become un-refunded, so the same call cannot succeed. A system that retries it burns turns and may double-refund a different order later in the conversation.
A tool has two ways to fail. It can return a result that says it failed, or it can raise an exception that stops the loop. For an agent tool, the first is almost always right.
The model is the caller. If the failure comes back as a tool result, the model can read it, try another tool, ask a question, or escalate. If it comes back as a stack trace, your code decides instead, and your code knows less about the request.
The messages API spells this is_error: true on the tool_result block. The Model Context Protocol (MCP), covered in step 29, spells it isError: true. The idea is the same: a failed call is still a result, and it belongs in the conversation.
A single string is not an error contract. The model needs to know what happened, whether trying again could work, and what to do next. Those are three questions, so give three fields.
| Field | Type | What it is for |
|---|---|---|
| code | string | A stable slug such as order_not_found. The model can branch on it and your logs can count it. |
| message | string | One sentence a human could read, with no secrets and no internals. |
| retryable | boolean | True only when the same call could succeed later. |
| next_action | string | The suggestion: retry once, ask for another id, or escalate. |
Keep code stable. Write it once, alert on it, and never change it without a version bump. The model learns the code from your description, and your dashboards learn it from your logs.
Two kinds of failure, two behaviours. A retryable error invites the same call again after a short wait. A terminal error forbids it. Retrying a terminal error wastes turns and makes the system look broken to the person waiting.
| Failure | Retryable | Why |
|---|---|---|
| Upstream timeout | Yes | The service may answer on the next attempt |
| Rate limit or 429 | Yes | The limit resets on a known timescale |
| Service temporarily unavailable | Yes | A short backoff usually clears it |
| Record not found | No | The same id will still be missing |
| Permission denied | No | The credential will not change between calls |
| Policy refusal, such as a refund above the original charge | No | The rule is the answer, not an obstacle |
Add a retry budget. Two attempts with backoff is usually enough for a network call. After that the error becomes terminal for the loop, and the model gets the escalation path instead of another silent retry.
import json
def tool_error(code, message, *, retryable, next_action):
body = {
"code": code,
"message": message,
"retryable": retryable,
"next_action": next_action,
}
return {
"isError": True,
"content": [{"type": "text", "text": json.dumps(body)}],
}
not_found = tool_error(
"order_not_found",
"No order A17 exists on this account.",
retryable=False,
next_action="Ask for a different order number, or escalate to a human.",
)
timeout = tool_error(
"upstream_timeout",
"The order service did not answer within 5 seconds.",
retryable=True,
next_action="Retry once, then escalate if it fails again.",
)
for result in (not_found, timeout):
payload = json.loads(result["content"][0]["text"])
print(result["isError"], payload["code"], payload["retryable"])Log the detail on your side, keyed by a correlation id, and return that id in the message. The model does not need the stack trace; your on-call engineer does, and they can look it up.
The error becomes input to the next decision. Here is the loop you want, including the path where the tool keeps failing.
A good error makes the next turn cheap. The model reads the code, sees that the order does not exist, and asks for a different id. A bad error makes the next turn a repeat, and a repeated call with the same arguments spends the whole budget on one missing record.
isError: true result with the four fields.retryable for a timeout and for a not-found, with one line of reasoning each.Key takeaways
Return the failure as a tool result, not an exception, so the model can change its plan.
Every error carries a stable code, a safe message, a retryable boolean and a next action.
Retryable means the same call can succeed later; terminal means a retry repeats the failure.
Never put secrets, stack traces, SQL or other customer data in an error message.
Bound retries, then escalate with the error code and a correlation id.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.