Practical guide · Agent reliability

AI agent retries, timeouts and idempotency

An agent sends a write, waits, and receives no answer. The write may have failed, committed, or still be running. A blind retry can repeat the side effect. This guide shows how to set deadlines, classify the outcome, keep retries at one layer, reuse one stable business key, and prove the design with forced failures.

Published November 23, 2026 · 14 minute read

The short answer

Retry only failures that may improve with time. Give every attempt a deadline and count it against one total budget. Treat a timed-out write as unknown until the destination confirms what happened. Reuse one idempotency key for every attempt at the same intended business effect, reject that key with changed arguments, and test the crash window between the external commit and your local acknowledgement.

A timeout means the outcome is unknown

A timeout limits how long the caller waits. It says nothing conclusive about the destination. The request may have been dropped before delivery, committed while the response was lost, or remained in progress after the caller gave up. Reads are usually safe to repeat. Writes need a stronger contract.

AWS's durable-execution guidance distinguishes at-least-once from at-most-once execution and warns that neither creates an end-to-end exactly-once guarantee. Replay and retry can both run an operation again. The system must either make the effect safe to repeat or reconcile the destination before trying again.

Observed failureDefault responseWhy
429 with Retry-AfterWait, then retry within budget.The provider declares a temporary capacity limit.
503 or connection reset on a readRetry with bounded backoff and jitter.The operation has no external side effect.
Timeout after sending a writeMark unknown and reconcile.The effect may already exist.
Invalid schema or argumentsRepair the request or stop.Waiting does not change invalid input.
401, 403 or policy denialStop and escalate through the approved path.A retry must not search for a way around authorization.
Worker crashResume from durable state.Completed steps should not be repeated unnecessarily.
Partial multi-system writeContinue, compensate or request review.Repeating the whole workflow can create more inconsistency.

Choose one retry owner

The model, agent framework, provider SDK, HTTP client, queue, workflow engine and user interface can all retry. If three layers each make three attempts, one logical action can reach the dependency many more times than expected. Pick the layer that has enough context to classify the failure, preserve the operation identity and enforce the total deadline. Disable or tightly bound retries below it.

Set both a per-attempt timeout and an operation deadline. The first stops one hung call. The second stops a sequence of technically valid attempts from outliving the user's task. Count queue delay, backoff, model calls, tool calls and reconciliation inside the total budget. Respect a provider's Retry-After value when it fits that budget; otherwise stop with a clear terminal state.

Backoff is load control, not a safety guarantee

Exponential backoff spaces attempts. Jitter prevents many workers from waking together. A cap prevents the final delay from becoming absurd. None of these prevents a duplicate write. A write becomes safe only when the destination deduplicates it, the application detects the existing effect, or the operation is designed as an upsert with a stable key.

Give one business effect one stable key

The key identifies the intended effect, not one network attempt. Generate it before dispatch, persist it with the operation, and reuse it across HTTP retries, queue redelivery, process restart and workflow replay. A new random key on every attempt defeats deduplication. A model-generated tool-call ID can also change when the model is asked again, so it is a poor business identity.

Stripe's idempotent-request contract stores the result associated with a key and rejects reuse with different parameters. OpenAI's session guidance gives the same practical rule for message submission: create one key per logical message and reuse the key, session and message after a timeout or lost response.

{
  "operation_id": "refund-order-1842-v1",
  "idempotency_key": "acct_7:refund:order_1842:v1",
  "action": "create_refund",
  "arguments_hash": "sha256:8c14...",
  "state": "outcome_unknown",
  "attempts": 2,
  "provider_reference": "re_927",
  "outcome": null,
  "deadline_at": "2026-11-23T12:00:00Z"
}
FieldPurpose
operation_idNames one business intent across all attempts.
idempotency_keyLets the destination or local ledger deduplicate repeats.
arguments_hashRejects accidental key reuse with changed parameters.
stateSeparates pending, in progress, unknown, completed and terminal failure.
attemptsEnforces the shared retry budget.
provider_referenceSupports read-back and reconciliation.
outcomeReturns the original accepted result on duplicate submission.
deadline_atStops attempts after the operation is no longer useful.

Reconcile before repeating an uncertain write

  1. Mark the operation outcome as unknown. Do not tell the user it failed.
  2. Query the destination by idempotency key, provider reference or a unique business reference included in the request.
  3. If the effect exists and matches the intended arguments, record it as completed and return the stored result.
  4. If the destination proves that no effect exists, retry with the same key and remaining budget.
  5. If neither state can be established, pause the operation and request review. Do not create a second business intent to escape the uncertainty.

Some APIs do not support idempotency keys or status lookups. Wrap them with an application-owned ledger and a unique downstream reference when possible. If no reliable deduplication or reconciliation path exists, choose at-most-once behavior for consequential writes: stop on ambiguity and let a person decide.

Put durable boundaries around side effects

Temporal's error-handling guidance recommends making activities idempotent because they may execute more than once. A durable engine can preserve completed results and resume after a crash, but the in-flight operation still has a failure window. External effects require their own key and reconciliation contract.

Checkpoint before dispatch so the operation identity survives a crash. Record the external result before allowing the agent to choose its next action. Keep model reasoning outside the transaction: a model can suggest an operation, while deterministic application code validates authorization, obtains any required approval, assigns the stable key, performs the call and records the outcome.

Test the failure window, not only the happy path

A healthy mock proves almost nothing about recovery. Inject failures at the points where state can disagree, then assert the final external effect and ledger state.

  1. Kill the worker before dispatch. The resumed run should make one attempt with the original key.
  2. Commit the external effect, drop the response, and restart the worker. The resumed run should reconcile or receive the stored duplicate result without a second effect.
  3. Submit the same key concurrently. Exactly one attempt should own the work; the other should wait or receive the recorded result.
  4. Reuse a key with changed arguments. The system should reject the mismatch rather than returning an unrelated result.
  5. Return a 429, repeated 503s and a permanent 403. Verify backoff, the total deadline and the no-retry rule for authorization failures.
  6. Crash after saving the result but before the next agent step. The workflow should replay the saved result instead of calling the tool again.
  7. Exhaust the budget. The run should enter one named terminal or review state, with no background attempts still active.

Trace the operation ID, key, attempt number, failure class, wait time, provider reference and final outcome. The agent observability guide shows how to capture those fields without dumping sensitive tool payloads into logs.

AI agent retry checklist

  • Every outbound call has a per-attempt timeout and an operation deadline.
  • Failures are classified as transient, permanent, invalid, denied, unknown or partial.
  • One layer owns each retry and every attempt counts against one shared budget.
  • Backoff respects provider guidance, adds jitter and has a maximum delay.
  • Every state-changing call has a stable business-intent key or an explicit no-retry policy.
  • Reusing a key with changed arguments is rejected.
  • Timed-out writes enter an unknown state and are reconciled before repetition.
  • Crash, replay, concurrency and ambiguous-outcome tests assert the final external effect.

Continue learning