Skip to main content

Errors the Model Can Act On

Why This Matters

When a tool fails, the model only knows what your tool result tells it. "Error" teaches it nothing, so it guesses, retries blindly or gives up. "Log not found for run 1042: logs are kept for 7 days" lets it choose a sensible next step: continue with the history and say the log was unavailable.

Four situations that look alike

SituationExampleShould the model retry?
Not foundNo log for this runNo; continue without it, mention the gap
Empty resultThe test has no failures in its historyNo; this is evidence, not an error
Temporary failureCI API timed outMaybe; or let your code retry first
Not allowedTool may not read that repositoryNo; never retry, report it

Treating all four as “error” is how agents end up in loops.

Study guides usually group tool errors into four categories. They map onto the table like this:

CategoryExample in TestPilotRetry?
TransientCI API timeout, rate limitYes, with backoff, preferably in your code
Validationruns: 500 where the maximum is 50Yes, once, after the model fixes its input
PermissionRepository not allowed for this agentNever
Business / not foundNo log exists for this runNo; continue and mention the gap

An empty result is none of these: it is a successful answer.

Naming detail: in the Messages API the flag is is_error: true on a tool_result block. In an MCP server the same idea is isError: true on the tool’s result. Either way the error is returned to the model as a result, not thrown, so the model can decide what to do next.

A structured error result

return {
  type: "tool_result",
  tool_use_id: block.id,
  is_error: true,
  content: JSON.stringify({
    error: "not_found",
    message: "No log for run-1042/t-005. CI keeps logs for 7 days; this run is 9 days old.",
    retryable: false,
    suggestion: "Continue with the error message and history; state that the log was unavailable.",
  }),
};

Four fields carry most of the value: a type the model can reason about, a message in plain language, whether a retry makes sense, and a suggestion for what to do instead.

Empty is not an error

get_test_history returning no failures is one of the most important pieces of evidence TestPilot can get: it argues strongly against “flaky”. Return it as a normal result:

{ "testId": "t-002", "runs": 20, "failures": 0, "failureRate": 0, "note": "No failures in the last 20 runs." }

Retry in code or in the model?

  • Transient failures (timeouts, rate limits): retry in your tool code with backoff, two or three times, before the model ever sees an error. The model does not need to know about a hiccup.
  • Permanent failures: return a clear error immediately and let the model adapt its plan.

Never leak internals

Tool results go into the model’s context and often into logs and reports. Do not pass raw stack traces, connection strings, tokens or internal hostnames. Map exceptions to safe, useful messages.

Exercise

Make read_log return the four situations above from fixture data. Script a run where the log is missing and check that TestPilot still produces a verdict that mentions the missing log.

Compare with solution/lesson-31-mcp-server, which covers lessons 24–31 (see docs/exercises.md, module 4).