A retried LLM request after a timeout or 5xx must not double-charge, double-send or repeat a tool action the model already triggered, and idempotency keys are what make that possible.

Idempotency keys and backoff on re-issued requests

Idempotency & Retries with LLM CallsDiagram with these labels, in reading order: Client Request; Check for; Idempotency Key; Send LLM; Request; Receive Response; or Error; Cache Result; by Request Hash.Client RequestCheck forIdempotency KeySend LLMRequestReceive Responseor ErrorCache Resultby Request Hash
Idempotency in LLM Call Handling

Wrap each call in exponential backoff with jitter. On a 429 status, which means too many requests, honor the Retry-After header before trying again. Attach an idempotency key, a unique identifier for the request, so that downstream effects such as database updates or external API calls can be tracked and not repeated.

Results can also be cached by a hash of the request. If a generation is retried with identical input, the cached answer comes back immediately and costs nothing extra.

  • Attach an idempotency key to requests.
  • Use exponential backoff with jitter.
  • Cache results by request hash.
  • Use upsert for side-effecting tools.
  • Avoid naive retries on irreversible actions.

Why the deduplication key comes from the input

The discipline resembles idempotent HTTP PUT requests and at-least-once message queues: repeating an operation must not create unintended copies. One difference matters. The message here is nondeterministic, so a retried generation can return different text each time.

Because the output varies, the key must be built from the input. A key built from the output would produce several entries for one input, which defeats its purpose.

Making tool calls safe to repeat

When the model re-emits the same tool call after a partial failure, the tool should create the record once or update the existing one. Use upsert on a natural key in place of a plain insert.

A naive retry loop is the tempting shortcut, and it can re-run send_email or charge_card. The result is multiple charges or spam. The assumption that a retry on error is safe is the root problem. Idempotency keys track the request and its effects, so only one action happens.

Naive retries Plain retry loops can cause double charges and duplicate emails. Pair every side-effecting call with an idempotency key.

Retry policy by task type

Task typeRetry approachReason
Pure text generationRetry freely, cache by input hashNo side effects, and the cache cuts cost.
Agents that call APIs or modify databasesIdempotency keys on every callRepeats would duplicate the action.
Irreversible actionsGate behind a confirmation step, no auto-retryA repeat cannot be undone.

Example scenario: 15% of calls are retried after 5xx errors

Example scenario In one deployment 15% of LLM calls are retried after 5xx errors, and some retries risk double charges. The figure is illustrative.
Idempotency & Retries with LLM CallsDiagram with these labels, in reading order: 5xx Error Rate > 10%; Check Idempotency Key; Handling; Review Retry Logic; Verify Tool Action; Deduplication; Add Idempotency Tests.5xx Error Rate > 10%Check Idempotency KeyHandlingReview Retry LogicVerify Tool ActionDeduplicationAdd Idempotency Tests
Debugging LLM Idempotency Issues

The likely cause is a system that does not track the state of requests, so a retry runs the same action again. A request that is not idempotent can trigger a second effect from the same input.

  • Check request logs to see whether the first request was recorded before the error.
  • Verify that the retry reuses the identifier of the original request.
  • Inspect application logic to confirm duplicate processing is blocked on retries.
The call Give each logical request a unique ID and reuse it on every retry. The tradeoff is extra bookkeeping to track requests, in return for no duplicate actions. Monitor the count of duplicate outputs per user request.

Back to the knowledge baseRequest an assessment