Skip to content
mentoor

Topic 2 of 6 · 4 min read

Tool errors that help the agent recover

Goal: Replace an unhelpful tool error with one that tells the agent the cause and a concrete next step.

A tool error is the next prompt the agent reads, so write it to say what failed and what to try next. The agent cannot open your logs. Whatever the tool returns on failure is everything it knows.

Check yourself · 1 of 3

A search tool's backend is down. What should the tool return?

Choose one answer

Why it matters

An agent that gets Error: 400 has three options: retry the same call, give up, or invent a fix. A stack trace is worse: hundreds of tokens about internals the model cannot act on. And a silent empty result ([] for a search that failed to authenticate) is the worst, because the agent reports "nothing found" as fact.

How to do it

  1. Return errors as tool results, not crashes. In MCP, report tool failures in the result with an error flag so the model sees them; protocol-level errors are for the harness. Most agent APIs have the same split (an is_error field on the tool result in Claude's API).
  2. Say what went wrong in plain words. "No project with key PAY. Valid keys: PAY2, CORE, OPS."
  3. Say what to do next. Name the fix: the parameter to change, the tool to call first, how long to wait.
  4. Separate failure kinds. Bad input (fix the arguments), not found (try another lookup), transient (retry after a delay), permission (stop and ask a human). The agent reacts differently to each.
  5. Never return success-shaped failures. An empty list means "searched, found none". A failed search must say it failed.
  6. Keep it short. One to three sentences. Log the stack trace server-side.

Before and after

// before
catch (e) {
  return { content: [{ type: "text", text: String(e.stack) }] };
}

The agent receives forty lines of internals and no instruction. After:

catch (e) {
  if (e instanceof RateLimitError) {
    return {
      isError: true,
      content: [{ type: "text", text: "Rate limit reached. Wait 30 seconds, then retry the same call once." }],
    };
  }
  logger.error(e); // full detail stays on the server
  return {
    isError: true,
    content: [{ type: "text", text: "The search service failed (ref 7f3a). Do not retry more than once; tell the user the search is unavailable." }],
  };
}

What goes wrong

  • Retry loops. A vague error plus an eager agent produces the same failing call ten times. State the retry rule in the message.
  • Leaked secrets. Raw exceptions can contain connection strings or tokens. Sanitise before returning.
  • Blame on the user. "Invalid input" with no field name sends the agent hunting.

Examples

From a bare 404 to a recoverable error

Before: {"error": "404"} after the agent asked for order A-1042.

After: Order A-1042 not found. Order ids look like ORD-10042. If you only have the customer's email, call orders_search with customer_email.

The agent now knows the id format was wrong and which tool gets the right one, so it recovers in one step instead of guessing.

A silent empty result

def search_docs(q):
    try:
        return index.search(q)
    except Exception:
        return []  # looks like "no matches"

When the index is down, the agent tells the user "there is no documentation on that". Return an error result such as Search index unavailable; results could not be checked. Retry once, then tell the user.

Key points

  • The error text is the agent's only view of the failure, so it must name the cause and the next step.
  • Return failures as marked tool results the model can read, not as crashes or empty successes.
  • Never let a failed call look like an empty result; the agent will report "nothing found" as fact.
  • Distinguish bad input, not found, transient and permission failures so the agent reacts correctly to each.
  • Keep stack traces and secrets on the server; return a short message and a reference id.

Common mistakes

  • Returning a raw stack trace or exception string and calling it an error message.
  • Catching every exception and returning an empty list or null, which hides the failure.
  • Writing "Invalid input" without naming the field or the accepted format.
  • Giving no retry guidance, so the agent loops on a permanent failure or abandons a transient one.

Check yourself · 2 of 3

Which tool error message helps the agent recover best?

Choose one answer

Check yourself · 3 of 3

Why should an error message tell the agent whether a failure is worth retrying?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic