{"slug": "designing-api-errors-agents-can-recover-from", "title": "Designing API Errors Agents Can Recover From", "summary": "A developer building the public REST API for FinHot, a free bilingual finance intelligence service, designed a machine-readable error contract for AI agents that combines RFC-style problem details, stable error codes, request IDs, ETags with 304 Not Modified responses, CORS preflight handling, and documented Retry-After hints on 429 and 503 responses. The work is packaged as a zero-dependency Python example using only the standard library that performs read-only requests. The developer argues that 304 and retry behavior should be part of the documented OpenAPI contract rather than accidental cache details, so agents can decide programmatically whether to correct input, reuse a cached payload, back off, or stop.", "body_md": "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.\n\nThat 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.\n\nI 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.\n\nThe first requirement is simple. An error should be structured the same way every time.\n\nWhen 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:\n\n```\ntype=/problems/invalid-request\ncode=invalid_request\nrequestId=325e8613-37a9-46ea-9a7c-d57acb947eec\n```\n\nThe 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.\n\nA missing story returns a different problem:\n\n```\nHTTP 404\ntype=/problems/not-found\ncode=not_found\n```\n\nThis 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.\n\nErrors are not the only recovery path. A good API also prevents unnecessary work.\n\nThe compact hot-topics endpoint returns an ETag:\n\n```\nGET /api/v1/hot-topics?lang=en&limit=1&fields=minimal\nHTTP 200\nETag: W/\"v1-hot-a9b39bb8fa6df230\"\n```\n\nA later conditional request with `If-None-Match` returned:\n\n```\nHTTP 304 Not Modified\nbytes=0\n```\n\nFor 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.\n\nThe 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.\n\nA browser-based agent does not only care about the GET response. It first needs a successful preflight.\n\nFinHot's preflight check returned:\n\n```\nHTTP 204\nAccess-Control-Allow-Origin: *\nAccess-Control-Allow-Methods: GET, HEAD, OPTIONS\n```\n\nThis 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.\n\nRate limiting and temporary unavailability are normal operational states. They should not be mysteries.\n\nFinHot'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.\n\nThis 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.\n\nPublic 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.\n\nFor 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.\n\nCompatibility 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.\n\nThe example script checks six production behaviors:\n\n`Retry-After` behavior.\nThe live production run passed all six checks. The output is plain enough for a person and precise enough for CI:\n\n```\nsummary: 6/6 checks passed\n```\n\nBecause 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.\n\nAgent-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.\n\nNone 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.", "url": "https://wpnews.pro/news/designing-api-errors-agents-can-recover-from", "canonical_source": "https://dev.to/wangdada8208/designing-api-errors-agents-can-recover-from-52ma", "published_at": "2026-12-06 22:06:58+00:00", "updated_at": "2026-12-06 22:14:24.994672+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools", "artificial-intelligence"], "entities": ["FinHot", "GitHub"], "also_reported_by": [], "takeaway": "Expose stable problem type and code fields plus a requestId on every error, and document ETag/304 and Retry-After behavior in the OpenAPI contract so agents can choose between correcting input, reusing a cached payload, backing off, or stopping.", "recommendation": {"advice": "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.", "playbook": "agent-architecture", "playbook_url": "https://wpnews.pro/playbooks/agent-architecture"}, "alternates": {"html": "https://wpnews.pro/news/designing-api-errors-agents-can-recover-from", "markdown": "https://wpnews.pro/news/designing-api-errors-agents-can-recover-from.md", "text": "https://wpnews.pro/news/designing-api-errors-agents-can-recover-from.txt", "jsonld": "https://wpnews.pro/news/designing-api-errors-agents-can-recover-from.jsonld"}}