Structured Error Responses
Principle (design guidance)
A bare "Error" or a stack trace gives the model nothing to act on. Return a structured error with:
- an error category (for example transient, validation, permission, business-rule, not-found)
- a retryable flag
- a human-readable message saying what happened and what to try
- relevant context (which input was bad, how long to wait)
This lets the model choose: retry (transient), fix the input (validation), escalate (permission or policy), or report honestly (not found).
Mechanics (API behaviour)
- A tool failure is returned to Claude as a
tool_resultfor the matchingtool_useid. The API'stool_resultblock carries anis_errorflag for failures. Keep the rule from 1.1: results only in that user message, one result pertool_use. - Handle your own HTTP and tool exceptions in the loop; do not let a tool exception crash the loop.
- In the Agent SDK,
PostToolUseFailurehooks run when a tool fails, which is a place to log or normalise errors.
Access failure vs empty result (design guidance)
These are different and must not look alike:
| Situation | Wrong signal | Right signal |
|---|---|---|
| Database timed out | Empty list [] | Error: category transient, retryable true |
| Caller lacks permission | "No results" | Error: category permission, not retryable |
| Query ran and nothing matched | Error | Success with an empty result |
Returning an empty success for a failure causes confident wrong answers ("no matching orders found") and silent data loss.
Retries and loops
- Retry transient errors with a bounded count and backoff.
- Do not retry validation or permission errors unchanged.
- Bound the whole loop (see 1.1).
Exam traps
- Generic "operation failed" messages.
- Empty result for a timeout.
- Retrying everything the same way.
- Swallowing a tool error so the agent proceeds as if it worked (see 5.3 Error propagation).
References
- Define tools — checked Oct 16, 2026
- Hooks in the SDK — checked Oct 16, 2026
Check your understanding
A lookup times out. Return an empty list or an error?Show answerHide answer
Answer
An error, marked transient/retryable.Which field tells the model whether to try again?Show answerHide answer
Answer
The retryable flag, with the category.A valid query finds no rows. Error?Show answerHide answer
Answer
No; a successful empty result.