Explore Grablex
JOURNAL / AI ENGINEERING / API RELIABILITY & AGENT ACTIONS

The request timed out.
The action may not have.

A timeout tells your client it stopped waiting. It does not prove that a provider or tool did nothing. Classify the failure, set a retry deadline, and make side effects safe to replay.

A charcoal token follows a clear loop and arrives twice at the endpoint, illustrating how an unguarded retry can repeat an action.SYMPLOKA / JOURNAL / SYMPLOKA
AI ENGINEERING / API RELIABILITY & AGENT ACTIONS

How to retry AI API calls safely

A timeout tells your client it stopped waiting. It does not prove that a provider or tool did nothing. Classify the failure, set a retry deadline, and make side effects safe to replay.

01 / READ THE UNCERTAINTY

A timeout means the client did not receive an answer in time.

A remote request can reach a server, start processing, and even finish after the client has timed out. The response may have been delayed or lost on the way back. From the client’s point of view, the outcome is unknown; the timeout alone cannot tell you whether the server acted.

For a model request, replaying an uncertain call can create a second generation, use more quota, and return a different answer. For an agent tool that sends a payment, creates a record, or sends a message, replaying can repeat the real-world action. AWS describes this as a general property of remote calls: a timeout does not guarantee that side effects did not occur.

Streaming needs its own recovery rule. Once part of a response has reached the user, an automatic replay can duplicate or contradict the visible text. OpenAI’s API guidance distinguishes HTTP failures before a stream starts from error events that arrive after a stream has begun. Preserve the partial state and tell the user what happened instead of silently starting the same visible response over.

A transport failure is evidence about the connection. It is not a receipt saying the action was cancelled.

SOURCESAWS Builder’s Library: timeouts, retries, and backoff with jitter ↗OpenAI API: error handling for streaming and non-streaming requests ↗
02 / CLASSIFY BEFORE RETRYING

The status code is only part of the error.

A retry can help with a temporary capacity or network problem. It will not fix an invalid request, expired key, missing permission, or exhausted spending allowance. Read the provider’s error type and code, request ID, and retry headers as well as the HTTP status.

A 429 is a useful example. OpenAI documents temporary rate limits and rapid-traffic `slow_down` errors, for which a present `Retry-After` header should be respected. Its guidance also distinguishes limits that need a configuration or billing change. Anthropic’s API documentation says a 429 can mean a rate limit or a spend cap; its monthly spend-cap response has no `retry-after` header and continues failing until access resumes.

Transient overload may use a server error instead. OpenAI documents 503 model overload; Anthropic documents 500 internal errors and 529 overload errors. Follow the chosen provider’s current guidance for those cases. Do not infer that every 4xx or 5xx response is safe to replay just from its number.

  • Malformed request or invalid parameters: correct the request before trying again.
  • Authentication or permission error: repair the credential or access rule; repeated calls will not grant access.
  • Temporary rate limit or overload: honor `Retry-After` if present, then use a bounded recovery policy.
  • Quota or spend cap: stop and surface the limit rather than sending the same call in a loop.
SOURCESOpenAI API: error codes and recovery guidance ↗OpenAI API: rate limits and `Retry-After` headers ↗Claude API: error codes and retry behavior ↗
03 / BOUND THE RECOVERY

Backoff, jitter, and a deadline keep recovery from becoming an outage.

When a provider tells you to wait, wait at least that long. When a retryable error has no delay header, use exponential backoff: increase the delay after each failed attempt, add random jitter so many clients do not retry together, and cap both the delay and the number of attempts.

Also cap total elapsed time to fit the user’s task. A queue worker may have a longer deadline than an interactive chat. If the deadline expires, record the unresolved state and let a later worker or person decide what to do. Do not keep calls alive indefinitely just because each individual retry is allowed.

Choose one layer to own the policy. An SDK, application, and gateway can each retry independently; stacking their limits may multiply attempts and traffic. Anthropic’s current API documentation says its official SDK retries transient errors twice by default. Check your SDK and gateway settings before adding an application loop, and measure the actual number of provider requests.

  • Retry only errors your provider documents as temporary.
  • Honor `Retry-After`; otherwise use capped exponential backoff with jitter.
  • Limit attempts and total elapsed time, and stop when the caller’s deadline is reached.
  • Keep retries in one deliberate layer and report the final failure with its request ID.
SOURCESAWS Well-Architected: control and limit retry calls ↗Claude API: SDK retry defaults and streaming errors ↗OpenAI API: rate-limit recovery and gradual traffic increases ↗
04 / MAKE ACTIONS SAFE TO REPLAY

Use idempotency for a logical operation, not for each attempt.

An operation is idempotent when repeating it has the same effect as performing it once. If an API supports idempotency keys, create one stable key for the logical action and reuse it with the same parameters for each retry. Generating a fresh key for every attempt defeats deduplication.

Stripe’s API documents this pattern for creating and updating objects: a repeated request with the same key can return the saved result instead of creating or updating the object twice. The exact contract—including which operations accept keys, how parameters are compared, and how long results are retained—belongs to that API. Do not assume an AI provider or tool offers the same guarantee.

If the tool has no idempotency support, add a durable operation ID in the application, store the action’s state and result, and check the external system before replaying an uncertain request. A database record alone cannot undo an action already performed by another service, so define how to reconcile an incomplete or conflicting result.

Idempotency belongs to the side-effecting operation. It does not mean two separate user requests should be collapsed into one.

SOURCESStripe API: idempotent requests and replaying operations ↗AWS Builder’s Library: making retries safe with idempotent APIs ↗
05 / TEST THE WHOLE ACTION

A model retry and a tool retry are different events.

Keep the model request, tool execution, and user-visible result as separate states. Save the exact tool operation and its identifier before executing it; after a timeout, check whether that operation completed before deciding to run it again. Do not ask the model to recreate a payment or message from scratch when the original tool call’s outcome is still unknown.

Test the boundary where the response disappears: after the provider accepts a request, after a tool performs a side effect, and before the application records the result. Also test invalid credentials, a 429 with and without `Retry-After`, a long outage, a partial stream, and the retry limit. Confirm that the task either completes once or stops with a visible unresolved status.

Track original requests separately from retry attempts. Useful measures include retry count by error type, total added latency and cost, duplicate side effects, final task outcome, and the number of cases that needed a human to reconcile state. A falling error rate is not a win if requests quietly repeat work.

  • Did the operation run zero times, once, or more than once?
  • Can the system recover the result after its response is lost?
  • Do retries stop when the limit is a quota, permission, or invalid request?
  • Does a fallback preserve the same authorization and deduplication rules?
SOURCESAWS Well-Architected: retry limits, idempotency, and retry storms ↗OpenAI API: classify rate-limit and overload errors ↗Claude API: inspect error types and request IDs ↗