Deployed Lab

Module 3 · Tool design and MCP integration · Chapter 10

Return structured errors from tools so agents can recover

17 min1 clickable picture1 hands-on lab1 predict, 2 quick quizzes, final quiz5-min video review
In 30 seconds

The picture: a good error is a signpost, not a shrug.

Exam objective: Task statement 2.2

Domain 2, Tool Design and MCP Integration (18 percent of the exam). Design tool error responses that support agent recovery. You should be able to:

The cold open

Harbor & Pine Outfitters is a made-up company. Its support agent Pine, from chapter 3, uses four MCP tools: get_customer, lookup_order, process_refund and escalate_to_human. One Friday the order service slows down, and every failing tool returns the same string: "error".

Nothing was wrong with the model. The tools gave it one word to reason with.

The plan for this chapter
  1. Mark failures with isError.
  2. Sort failures into four actionable categories.
  3. Write the actionable error, safely.
  4. Bound recovery: retry caps and logs.
  5. Worked example, then a lab on all four Pine tools.

Next: the picture.

Step 1: Follow one failure around the picture

Why this step: every exam question here targets one box.

Think of a delivery note. "Failed" helps no one. "Gate locked, code needed, ask the resident" gets the parcel delivered.

Where the picture breaks down:

On a phone, swipe sideways.

How a structured tool error drives recovery A tool fails. Your code shapes it into a result with isError, a code and a category. Claude picks one action: retry, change plan, ask, or escalate. The loop caps retries and logs the failure. Tool runslookup_order It failstimeout, bad input Shape the errorisError, codecategory, next step Claude reads itpicks a response One actionretry, replanask, escalate retry goes around again, capped and logged by the loop
  1. Pine calls lookup_order for order 48213, as in chapter 3.
  2. The order service times out. Failure is normal. The design question is what happens next.
  3. Instead of throwing or returning "error", your tool returns a result marked isError, with a code, a category and a suggested next step.
  4. The category tells Claude which move fits. No guessing from a stack trace or a bare string.
  5. Transient: retry. Permanent: change plan. Needs-input: ask the user. Policy: escalate. One category, one default move.
  6. A retry re-enters the loop from chapter 3, which caps attempts and logs the failure.
Your toolsFailureShaped errorClaudeAction

Next: the failure flag.

Step 2: Mark the failure with isError

Why this step: if a failure looks like a success, the agent will treat it as one.

The same failure, two ways (illustrative):

// vague: no flag, no detail, nothing to act on
{ "content": "error" }

// structured: flagged, coded, actionable
{
  "isError": true,
  "content": [{
    "type": "text",
    "text": "ORDER_SERVICE_TIMEOUT: lookup_order timed out after 5s. "
          + "Transient failure. Safe to retry once."
  }]
}

Exact shapes vary by SDK and MCP client. Version-dependent: check the current docs. The principle is stable: flag it, code it, explain it.

Quick check: the flag.

Next: the four categories and their moves.

Step 3: Four categories the agent can act on

Why this step: a category is a decision, pre-made.

CategoryMeaningAgent movePine example
TransientFailed now, may work soonRetry, within the capService timeout
PermanentWill fail the same way againStop, change planOrder does not exist
Needs-inputA person must supply or fix somethingAsk the userEmail matches nobody
PolicyWorks, but a rule forbids the agentEscalateRefund above $500

Chapter 9 covered descriptions that set up the call. Categories do the same job for the result. For basics, see Structured outputs and tool calling.

Do this: pick your answer, then press the button.

A missing order is permanent: retrying cannot create it. Labelled permanent, Pine would change plan and ask the customer to check the number. Unlabelled, the default guess is retry.

Next: what an actionable error contains.

Step 4: Write errors the agent can act on, safely

Why this step: the category picks the move. The message carries its detail.

An actionable error has three parts:

  1. What failed. The tool and operation, in plain words.
  2. Why. The cause at the level the agent can use, not the internal cause.
  3. What to try instead. Retry, corrected input, another tool, or escalation.
// illustrative: safe on the outside, detailed on the inside
{
  "isError": true,
  "code": "REFUND_LIMIT_EXCEEDED",
  "category": "policy",
  "message": "Refund of $640.00 exceeds the $500 agent limit.",
  "suggestion": "Call escalate_to_human with the order ID.",
  "failure_id": "f-88231"   // full trace stays in server logs
}

Quick check: what stays out.

Next: bounding recovery, so a retry cannot become a runaway loop.

Step 5: Cap retries and log failures

Why this step: categories guide the model. Caps and logs are guarantees your code owns, like chapter 3's backstop.

Next: the payoff, in numbers.

Step 6: Worked example, a month of failures

Why this step: the exam rewards the design. This shows the prize.

All numbers here are made-up. Pine hits 1,000 tool failures in a month.

With vague errors (a bare "error" string):

1. The agent recovers alone in 180 of 1,000 cases, a made-up rate of 18 percent.

2. The other 820 become escalations or dead chats: 1,000 minus 180 = 820.

With structured errors (flag, code, category, suggestion):

3. Transient: 400 failures, 340 fixed by capped retries. Needs-input: 250, of which 180 get fixed after the agent asks. Permanent: 200, of which 90 resolve by a changed plan. Policy: 150, all escalated at once, correctly.

4. Recovered without a human: 340 + 180 + 90 = 610 of 1,000, a made-up rate of 61 percent.

5. Extra recoveries: 610 minus 180 = 430 chats that never reach Nadia's team.

6. At a made-up $6 per human-handled contact: 430 × $6 = $2,580 a month saved.

The gain is not a smarter model. It is failures arriving in a shape the same model can act on.

Next: the lab, designing returns for all four tools.

Step 7: Hands-on lab, design Pine's error returns

Why this exercise: writing the returns per tool is the skill task 2.2 tests.

Lab: In the cold open, every failing Pine tool returned the same word, error, and Pine looped or invented success. This lab runs in your browser and needs JavaScript enabled.

Field note, November 2026

In production support systems, the worst-handled failures are rarely exotic. They are timeouts, stale IDs and missing inputs, repeated at volume. Teams that log codes by category usually find one or two codes doing most of the damage, and fix those first. Common practice, stated generally, not a measured statistic.

The Deployed Lab jobs page (snapshot 15 Nov 2026) lists 367 open FDE roles, median posted base about $243K, base pay only.

Mix-ups to avoid

Chapter quiz

Five questions. Score 70% or more to mark this chapter complete.

Review this chapter in 5 minutes

Watch the whole chapter recapped: isError and error codes, the four failure categories and their moves, safe actionable messages, and retry caps in code.

Decision rule: if an error does not tell the agent which of retry, replan, ask or escalate to pick, it is not finished.

Independent exam preparation. Not affiliated with or endorsed by Anthropic.

Review

Glossary

isError
Flag on a tool result marking it as a failure, not data. Example: a timeout with isError: true.
Error code
Short machine-readable name for a failure kind. Example: ORDER_NOT_FOUND.
Error category
The class that picks the next move: transient, permanent, needs-input, policy.
Transient failure
May clear on its own. Example: a timeout during a spike.
Permanent failure
Will repeat identically. Example: an order number that does not exist.
Needs-input failure
Only a person can repair it. Example: an email matching no customer.
Policy failure
A rule, not a fault, blocks the agent. Example: a refund above $500.
Actionable error
States what failed, why, and what to try instead.
Retry cap
Code-enforced limit on repeat attempts. Example: two retries, then escalate.
Failure ID
Links a safe conversation error to its full server-side trace. Example: f-88231.

Next: "Give each agent the right tools: tool distribution, tool_choice and the built-in tools".