By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A customer asks for a refund on order A17. process_refund times out after ten seconds. Your wrapper raises, the loop catches the exception, and the retry sends the same request again. The provider had already accepted the first call, so the customer is refunded twice.
Two decisions caused this. The tool hid its failure inside an exception, and the loop treated a timeout as proof that nothing had happened. Neither decision is safe.
A tool call can fail in many ways: a bad argument, a missing record, a rate limit, a permission error, a timeout with an unknown outcome. Production code has to tell these apart, because the right response is different for each one.
Retry the 429, after two seconds. It is a temporary refusal and nothing changed. Do not retry 'already refunded': that state is final, so the same call cannot succeed. Retrying it burns turns and can push the agent into a duplicate refund on a later order.
The model is the caller. When a tool fails, the useful move is to send the failure back as a result the model can read, not to raise an exception that ends the loop.
The Messages API marks this with is_error: true on the tool_result block. The Model Context Protocol (MCP) uses isError: true on the call result. The idea is identical in both: the call failed, the conversation continues, and the model chooses the next step.
Why does it matter who decides? Your code knows how the call failed. The model knows what the user asked. A result keeps both pieces of knowledge in the loop. An exception throws one of them away.
A message on its own is not an error contract. Four fields make the failure actionable, for the model and for your tests.
| Field | What it tells the model | Example |
|---|---|---|
isError | This is a failure, not data | true |
code | Which failure, as a stable token | order_already_refunded |
retryable | Whether the same call may succeed later | false |
next_action | What to do instead, in one sentence | Give the refund date; do not call again |
Keep code stable and machine-readable, because your dashboards and tests will key on it. Keep next_action short and specific, because it is the sentence the model will follow.
import json
def tool_error(code: str, message: str, *, retryable: bool,
next_action: str) -> dict:
return {
"isError": True,
"code": code,
"message": message,
"retryable": retryable,
"next_action": next_action,
}
REFUND_REJECTED = tool_error(
"order_already_refunded",
"Order A17 was refunded on 12 November. The provider rejects a second refund.",
retryable=False,
next_action=(
"Tell the customer the refund is already on its way and give the date. "
"Do not call process_refund again."
),
)
print(json.dumps(REFUND_REJECTED, indent=2))
# The same payload travels back as a tool_result block.
RESULT_BLOCK = {
"type": "tool_result",
"tool_use_id": "toolu_01A",
"is_error": True,
"content": [{"type": "text", "text": json.dumps(REFUND_REJECTED)}],
}Build the error once and reuse the shape for every tool. The model then learns one contract rather than four, and your tests can assert on code without parsing prose.
The loop still owns two policy decisions. Retry only when retryable is true, cap the attempts, and honour Retry-After. For a write with an unknown outcome, read the state back before you retry.
Tools are grouped by where the code runs, and the group changes what you own. The exam expects you to tell the groups apart.
| Kind | Who runs it | Example | What you own |
|---|---|---|---|
| Client tool | Your application | lookup_order | Schema, execution, retries, logging |
| Server tool | Anthropic, inside the API | web search | The request, the result handling, the spend |
| MCP tool | The MCP server process | orders__lookup_order | The server, its auth and its deployment |
The third row surprises people. From the Messages API, an MCP tool is still a client tool: your code receives tool_use, calls the server, and returns the tool_result. MCP moves the integration, not the loop.
An approval pattern decides which calls need a person before they run. It is a runtime rule, not a sentence in the system prompt, because the model cannot grant itself permission.
Put the gate between tool_use and execution. The model has already proposed the call, so you can show a person the tool name and the exact arguments, and the decision applies to that one call.
is_error: true with code: approval_denied lets the model propose an alternative.code, the retryable flag and a single next_action.Key takeaways
Return failures as results, never as exceptions that end the loop.
Four fields make an error actionable: isError, code, retryable and next_action.
Retry only temporary failures, cap the attempts, and read the state back after an ambiguous write.
A client tool runs in your app; a server tool runs inside the API; an MCP tool runs in a separate server.
Approval is a runtime gate keyed to the exact call, and a denial comes back as a result.
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.