Skip to content
AgenticAIWiki
  1. Learn
  2. Part 7
  3. 34. Designing good tools
  4. 34.3

Lesson 34.3 · 3 of 6 · 6 min read · Reviewed 24 Nov 2026

Structured errors: isError, errorCategory, isRetryable

By the end, you can

  • Return structured errors from tools.
  • Help the agent choose between retrying, fixing input and escalating.

When a tool fails, the agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. has to decide: retry, fix the input, try another tool, or tell a person. A good error makes that easy.

Mark it as an error

  • In the Claude API, set "is_error": true on the tool_result.
  • In MCPModel Context Protocol (MCP): An open protocol that standardises how AI applications connect to tools, data and prompts through servers., a tool returns isError: true in its result.

Never return an empty success for a failure. “No orders found” and “The order service is down” lead to very different next steps.

Make it structured

{
  "is_error": true,
  "content": "{\"errorCategory\": \"transient\", \"isRetryable\": true, \"message\": \"Order service timed out after 5s.\"}"
}

Useful categories:

Category Example Agent should
validation Bad order ID format Fix the input and retry
permission Not allowed to refund this account Stop and escalate
transient Timeout, rate limit Retry after a pause
business Refund exceeds policy limit Explain or escalate, don’t retry

Retry inside the tool, report outside

Let your tool code handle quick, safe retries for transient failures. Report to the agent only what it can act on. That keeps the loop short and the context clean.

Key points

  • A tool error should tell the agent what went wrong and what to do next.
  • Mark failures clearly (is_error in the API, isError in MCP) and include a category such as validation, permission, transient or business.
  • Say whether a retry could help; never return an empty success when the call failed.
Hands-on labRuns in the Playground

Return structured errors from a tool

Map each kind of failure to a category and a retry flag the agent can act on.

You will have: A tool_charge() wrapper and a table of five calls with their error categories.

Steps

  1. Run the starter: every failure says just Error.
  2. Map each exception type to a category and isRetryable.
  3. Return those fields in a JSON body with is_error true.
  4. Run it and decide what an agent should do for each row.

Done when

  • The timeout is transient and retryable.
  • The decline is business and not retryable.
  • The bad card token is validation.

Open in the Playground No API key needed.

Starter code (Python)
# Turn failures inside a tool into structured errors the agent can act on.
import json


class Declined(Exception):
    pass


def charge(card, amount):
    if card == "slow":
        raise TimeoutError("payment service timed out after 5s")
    if card == "blocked":
        raise PermissionError("this agent may not charge business accounts")
    if amount > 500:
        raise Declined("amount over the card limit")
    if not card.startswith("test-"):
        raise ValueError("card token must start with test-")
    return {"charged": amount}


# TODO: map each exception type to (errorCategory, isRetryable):
# TODO: TimeoutError transient, PermissionError permission, Declined business, ValueError validation
CATEGORY = {}


def tool_charge(card, amount):
    try:
        return {"is_error": False, "content": json.dumps(charge(card, amount))}
    except Exception as e:
        # TODO: return is_error True with a JSON body holding errorCategory, isRetryable and the message
        return {"is_error": True, "content": json.dumps({"errorCategory": "unknown", "isRetryable": False, "message": "Error"})}


cases = [("test-visa", 40), ("slow", 40), ("blocked", 40), ("test-visa", 900), ("4242", 40)]
for card, amount in cases:
    r = tool_charge(card, amount)
    body = json.loads(r["content"])
    print(f"{card:<10} {amount:>4}: " + (f"ok {body}" if not r["is_error"] else f"{body['errorCategory']:<10} retry={body['isRetryable']}"))
Show a solution
# Turn failures inside a tool into structured errors the agent can act on.
import json


class Declined(Exception):
    pass


def charge(card, amount):
    if card == "slow":
        raise TimeoutError("payment service timed out after 5s")
    if card == "blocked":
        raise PermissionError("this agent may not charge business accounts")
    if amount > 500:
        raise Declined("amount over the card limit")
    if not card.startswith("test-"):
        raise ValueError("card token must start with test-")
    return {"charged": amount}


CATEGORY = {
    TimeoutError: ("transient", True),
    PermissionError: ("permission", False),
    Declined: ("business", False),
    ValueError: ("validation", True),
}


def tool_charge(card, amount):
    try:
        return {"is_error": False, "content": json.dumps(charge(card, amount))}
    except tuple(CATEGORY) as e:
        cat, retry = CATEGORY[type(e)]
        return {"is_error": True, "content": json.dumps({"errorCategory": cat, "isRetryable": retry, "message": str(e)})}


cases = [("test-visa", 40), ("slow", 40), ("blocked", 40), ("test-visa", 900), ("4242", 40)]
for card, amount in cases:
    r = tool_charge(card, amount)
    body = json.loads(r["content"])
    print(f"{card:<10} {amount:>4}: " + (f"ok {body}" if not r["is_error"] else f"{body['errorCategory']:<10} retry={body['isRetryable']}"))

Do it Handle a hanging tool in the failure lab in the Playground. No API key needed.

Quick checkA lookup tool times out. Which result helps the agent most?
How sure are you?
Pick an answer and how sure you are