Task 2.2
Structured Errors
Returning errors that agents can reason about and recover from
📘What You Need to Know
Why Structured Errors Matter
When a tool fails, returning "Operation failed" gives Claude no actionable information. Should it retry? Try an alternative tool? Escalate to a human? A flat error string forces guesswork and produces either wasteful retries or premature abandonment.
Structured error returns give the agent a decision framework. Each field answers a specific recovery question: did the tool fail (isError), what kind of failure (errorCategory), should we retry (isRetryable), and what exactly was attempted (context). This pattern is critical in multi-agent systems where coordinators must reason about subagent failures.
Required Error Fields
Every structured tool error response should include these four fields:
- •isError (boolean): signals whether the tool execution failed. true means the operation did not complete; false means it succeeded (even if results are empty).
- •errorCategory (string): classifies the failure — validation, auth, not_found, rate_limit, timeout. Categories drive recovery logic.
- •isRetryable (boolean): whether retrying might succeed. true for transient failures (timeouts, rate limits); false for permanent failures (auth, invalid input).
- •context (string or object): what was attempted and what specifically failed. Include the input values, endpoint, and error details.
Always return isError, errorCategory, isRetryable, and context. The exam penalizes generic error strings and missing retryability signals.
Access Failure vs Empty Result
This distinction is one of the most heavily tested concepts in Domain 2. Two scenarios look similar but require opposite handling:
Access failure: the tool could not reach the database or API. The search was NOT performed. Return isError: true with errorCategory "timeout" or "auth" and isRetryable as appropriate.
Empty result: the tool successfully queried the database but found no matching records. The search WAS performed. Return isError: false with results: []. The coordinator can confidently tell the user "no matches found" rather than "we could not check."
Never silently return empty results for access failures. If a subagent cannot reach the database, the coordinator must know data is MISSING — not assume nothing was found.
Error Categories and Retry Logic
Match errorCategory and isRetryable consistently. Timeouts and rate_limit errors are typically retryable — the underlying service may recover. Auth failures and validation errors are not retryable without changing inputs or credentials. not_found is usually not retryable with the same parameters.
In multi-agent architectures, subagents must propagate structured errors to the coordinator. The coordinator uses isRetryable to decide local retry, alternative subagent delegation, or human escalation. Missing isRetryable causes either infinite retry loops on permanent failures or giving up on transient ones.
// Access failure — search was NOT performed
{
"isError": true,
"errorCategory": "timeout",
"isRetryable": true,
"context": "Attempted customer lookup for CUST-10482. Database connection timed out after 30s."
}
// Empty result — search WAS performed, nothing found
{
"isError": false,
"results": [],
"context": "Searched customers table for email jane@example.com. No matching records."
}
// Permanent failure — do not retry
{
"isError": true,
"errorCategory": "auth",
"isRetryable": false,
"context": "API key expired. Cannot access order service."
}Anti-Patterns in Error Handling
Anti-Pattern 1: Returning generic "Operation failed" strings without structured fields. The agent cannot determine recovery strategy.
Anti-Pattern 2: Silently returning isError: false with results: [] when the database is unreachable. The coordinator assumes a successful empty search.
Anti-Pattern 3: Throwing unhandled exceptions that crash the tool execution loop instead of returning structured error responses the agent can process.
Anti-Pattern 4: Omitting isRetryable, forcing the agent to guess whether a timeout deserves another attempt or immediate escalation.
GPS saying "Route blocked — detour available via Highway 5" is a structured error. "Error" is not. Build tool errors like the GPS version.
⚠️Exam Traps
Trap: Returning generic "Operation failed" errors without structured fields
Why it's wrong: The agent cannot determine whether to retry, escalate, or try an alternative approach.
Trap: Silently returning empty results (isError: false, results: []) for database access failures
Why it's wrong: The agent assumes the search was performed and found nothing — a fundamentally different conclusion.
Trap: Omitting isRetryable from error responses
Why it's wrong: Without retryability signal, Claude either retries everything (wasting calls) or gives up on transient errors.
Trap: Throwing unhandled exceptions instead of returning structured tool errors
Why it's wrong: Exceptions crash the execution path; the agent cannot reason about or recover from unstructured failures.
✓Concept Check
A tool cannot connect to the database. How should it respond?
🔨Build Exercise
Intermediate35 minutesImplement Structured Error Responses
What you'll learn
- •Build a reusable error response helper with all required fields
- •Distinguish access failures from empty results in practice
- •Wire error propagation for multi-agent coordinator patterns