When the API Says No: Errors, Retries and Timeouts
Why This Matters
TestPilot runs inside your pipeline. If one API hiccup turns a red build into a crashed triage job, nobody will trust it. Testers know how to test the unhappy path; this lesson applies that to the API itself.
The errors you will meet
| Situation | Typical status | What to do |
|---|---|---|
| Invalid request (bad field, too long) | 400 | Fix your code; retrying will not help |
| Authentication or permission problem | 401 / 403 | Fix the key or its scope; do not retry |
| Rate limit reached | 429 | Wait and retry, using the retry-after hint when the API sends one. A 429 for a reached spend limit has no hint and keeps failing until the limit is raised |
| Request too large | 413 | Do not retry; trim the input |
| Billing problem | 402 | Do not retry; fix the billing |
| Server error or overloaded | 5xx (including 529) | Retry with backoff |
| Timeout or network failure | none | Retry with backoff |
Check Anthropic’s error documentation for the current list.
Two real failures
While building this course, TestPilot hit both kinds of error.
A 400: the bug was ours. .env.example contained an empty TESTPILOT_MODEL=. The code fell back to the default model with ??, which only catches a missing value, not an empty string, so it sent model: "". The API rejected the request immediately, with a message that says exactly what is wrong.
Retrying a 400 would have failed forty times in a row. The fix was one character in the code (|| instead of ??) and a test that proves an empty model name falls back to the default.
No status code at all: the network. Run from a sandbox without internet access, the same command fails before it ever reaches the API. There is no HTTP status, only a connection error with a DNS failure underneath.
This one is worth a retry if it is a hiccup, but if it fails every time, the problem is the environment, not your request. Report it as such.
Where the limits live
Rate limits are per model and per organisation tier: input tokens, output tokens and requests. A nightly triage of a big red pipeline is exactly the kind of burst that runs into them.
What the SDK already does
The official SDK retries some failures automatically (connection errors, rate limits and server errors) with a short backoff, and lets you set the number of retries and a timeout:
const client = new Anthropic({ maxRetries: 3, timeout: 60_000 });
That covers the brief hiccups. What it cannot decide for you is what TestPilot should do when a request still fails after the retries.
Partial success is still success
A run with 40 failures should never end with “triage failed” because request 17 timed out. Triage each failure independently and report honestly:
const outcomes = await Promise.allSettled(failures.map((f) => triageFailure(client, run, f)));
for (const [i, o] of outcomes.entries()) {
if (o.status === "fulfilled") report.triaged.push(o.value);
else report.untriaged.push({ testId: failures[i].id, reason: describeError(o.reason) });
}
The report then says: 37 triaged, 3 not triaged (rate limit), listed for a human. That is far more useful than a crash, and far more honest than marking the three as “unknown”.
Note that Promise.allSettled on 40 failures sends 40 requests at once, which is a good way to hit the rate limit. Limit concurrency to a handful at a time.
Test the unhappy path
Your fake client can throw just as easily as it can answer:
const parse = vi.fn()
.mockResolvedValueOnce({ parsed_output: validTriage })
.mockRejectedValueOnce(Object.assign(new Error("Overloaded"), { status: 529 }));
Write tests for: one failure out of two fails → one triaged, one untriaged; an authentication error → the run stops with a clear message instead of retrying 40 times.
Exercise
Implement the partial-success report and the two tests. Decide, and write down, which errors stop the whole run and which only skip one failure.


