# Designing API Errors Agents Can Recover From

> Source: <https://dev.to/wangdada8208/designing-api-errors-agents-can-recover-from-52ma>
> Published: 2026-12-06 22:06:58+00:00

**Recommendation:** Give agents a machine-readable error contract: stable type/code fields, request IDs, documented ETag/304 revalidation, and Retry-After on 429/503, then test the CORS preflight as deliberately as the data path.
More in the playbook "Designing agent systems": <https://wpnews.pro/playbooks/agent-architecture>

Agents do not experience an API error the way a person does. A person can read a page, understand a vague message, and try again. An agent has to decide programmatically whether to correct input, use a cached copy, wait, follow another link, or stop.

That decision needs a machine-readable error contract. While building the public REST API for [FinHot](https://github.com/wangdada8208/finhot-mcp), a free bilingual finance intelligence service, I found that the useful contract is larger than an error status code. It includes problem details, stable codes, request identifiers, revalidation headers, CORS behavior, and written retry hints.

I packaged the production check as a zero-dependency Python example: [`examples/api-error-handling.py`](https://github.com/wangdada8208/finhot-mcp/blob/main/examples/api-error-handling.py). It uses only the standard library and performs read-only requests.

The first requirement is simple. An error should be structured the same way every time.

When a client sends an unsupported query parameter to FinHot's hot-topics endpoint, the response is HTTP 400 with a problem object. In the production run I used for this article, the object had:

```
type=/problems/invalid-request
code=invalid_request
requestId=325e8613-37a9-46ea-9a7c-d57acb947eec
```

The status tells the client the broad outcome. The `type` and `code` tell it what happened. The request identifier gives operators a precise object to correlate with logs.

A missing story returns a different problem:

```
HTTP 404
type=/problems/not-found
code=not_found
```

This separation matters. A client can correct invalid input without guessing from prose. It can treat a missing story as a lookup failure and follow canonical story links instead. It can also display the detail field to a person without parsing it as a control signal.

Errors are not the only recovery path. A good API also prevents unnecessary work.

The compact hot-topics endpoint returns an ETag:

```
GET /api/v1/hot-topics?lang=en&limit=1&fields=minimal
HTTP 200
ETag: W/"v1-hot-a9b39bb8fa6df230"
```

A later conditional request with `If-None-Match` returned:

```
HTTP 304 Not Modified
bytes=0
```

For a polling agent, this is a cost-control feature. It can keep the last good payload, pay almost no transfer cost for an unchanged result, and avoid treating a temporary empty response as a data deletion. The same pattern is useful for watchlists, mirrors, and feed monitors.

The important part is that the 304 behavior is part of the public contract, not an accidental cache detail. It is documented in the OpenAPI response set, so a client can implement it before the first production poll.

A browser-based agent does not only care about the GET response. It first needs a successful preflight.

FinHot's preflight check returned:

```
HTTP 204
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, HEAD, OPTIONS
```

This is small, but it prevents a common integration failure. Server-side code may work with curl while a browser client fails before the real request is sent. A public API should test the preflight path as deliberately as the data path.

Rate limiting and temporary unavailability are normal operational states. They should not be mysteries.

FinHot's OpenAPI document advertises `Retry-After` on 429 and 503 responses. That does not mean a client should deliberately trigger those states. It means the recovery rule is visible before a failure happens. A client can prepare a bounded backoff path, preserve the last good response, and stop after a small number of retries.

This is one of the differences between an API that is merely reachable and an API that is operable. Agents need to know whether retrying is useful, how long to wait, and when to give up.

Public API reviews often suggest cleaner field names, flatter objects, or fewer compatibility aliases. Those changes may be elegant in isolation. They are not automatically improvements for a live API.

For example, a compact hot-topics item can carry both a first-class `storyId` and a link to the canonical story API. A purist might remove one representation. An integrator sees value in both: the identifier is easy to store, while the link is easy to follow. The cost of keeping them is small compared with the cost of breaking existing clients.

Compatibility does not mean freezing the API. It means additive change first, clear versioning when behavior must change, and documentation that tells clients which fields are stable integration points.

The example script checks six production behaviors:

`Retry-After` behavior.
The live production run passed all six checks. The output is plain enough for a person and precise enough for CI:

```
summary: 6/6 checks passed
```

Because the script exits non-zero on failure, it can run as a periodic public-contract monitor. It is also short enough for an integrator to read before copying the pattern.

Agent-friendly APIs should design the failure path as carefully as the success path. Use typed problem objects. Keep stable machine-readable codes. Include request identifiers. Make revalidation cheap. Test browser preflight. Publish retry hints. Then prove the behavior with a small runnable check.

None of this proves that a service is secure or correct for every use case. It does something more immediate. It gives an agent a reliable way to decide what to do next.
