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
| Situation | Example | Should the model retry? |
|---|---|---|
| Not found | No log for this run | No; continue without it, mention the gap |
| Empty result | The test has no failures in its history | No; this is evidence, not an error |
| Temporary failure | CI API timed out | Maybe; or let your code retry first |
| Not allowed | Tool may not read that repository | No; 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:
| Category | Example in TestPilot | Retry? |
|---|---|---|
| Transient | CI API timeout, rate limit | Yes, with backoff, preferably in your code |
| Validation | runs: 500 where the maximum is 50 | Yes, once, after the model fixes its input |
| Permission | Repository not allowed for this agent | Never |
| Business / not found | No log exists for this run | No; 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).