CrabTalk
Blog

Idempotency keys for AI agent tool calls: retry without sending twice

When an agent's tool call times out, did the email go? How Stripe, Resend and AWS idempotency keys work, and the rules an agent needs to retry safely.

CrabTalk Team

An agent calls a tool that sends an email. The connection drops before the answer comes back. Did the email go out? Retrying might send it twice; not retrying might send it never. This is the oldest problem in distributed systems, and agents meet it constantly, because their tools call third-party APIs over the network and their runs are long enough to be interrupted.

The standard answer is an idempotency key. This post compares how three APIs implement one, and then sets out the rules an agent, or the runtime around it, has to follow for a retry to be safe.

A call has three outcomes

After a request, a caller knows one of three things:

  1. It succeeded. There's a response.
  2. It failed before doing anything. For example, it was refused for invalid input, or the connection was refused before anything was sent.
  3. Nobody knows. The request left, and no response came back: a timeout, a dropped connection, a crash.

The first two are easy. The third is the one that sends duplicate emails and charges cards twice, and a retry is only safe there if the server can recognize it as the same request.

Reads are always safe to retry

A tool that only reads, such as search, fetch or lookup, can run twice with no harm. MCP lets a server say so with tool annotations: readOnlyHint for a tool that changes nothing, and idempotentHint for one where repeating a call with the same arguments has no further effect. The spec warns that clients must treat annotations as untrusted unless they come from a trusted server, so a hint helps an agent decide; it doesn't guarantee anything.

Writes need something stronger: a key the server remembers.

How three APIs do it

The caller generates a key, sends it with the request, and sends the same key with every retry. The server stores the outcome under that key and answers a retry with the stored outcome instead of acting again.

Stripe Resend AWS EC2
Where the key goes Idempotency-Key header Idempotency-Key header ClientToken parameter
Key length Up to 255 characters 1 to 256 characters Up to 64 ASCII characters
How long it's remembered At least 24 hours, then may be pruned 24 hours Not stated on the linked page
Same key, same request The saved response, 500 errors included The original response; the email isn't sent again The original result
Same key, different request An error 409 invalid_idempotent_request IdempotentParameterMismatch
Same key, still running Not saved; safe to retry 409 concurrent_idempotent_requests
Sources Stripe Resend EC2

Three details matter for agents:

  • The window ends. Stripe may prune a key after 24 hours, and a reused key after that creates a new request. Resend's window is also 24 hours. A retry after the window isn't a retry; it's a second send.
  • The payload must match. All three compare the retry with the original and refuse a mismatch. An agent that rebuilds the request on retry, with a new timestamp or a regenerated message body, gets an error instead of the stored result.
  • Some failures aren't stored. Stripe saves a result only after the endpoint starts running. A request that failed validation can be fixed and retried with the same key.

Rules for an agent that retries

  1. Make the key before the first attempt. One key per logical operation, such as welcome-user/123 or a UUID, generated before anything is sent. A key generated on the retry is a new request.
  2. Save the key and the exact payload before sending. A runtime that crashes between sending and recording loses the key and can't retry safely. Write both to durable storage first, then send.
  3. Retry with the identical payload. Not a payload rebuilt from the same intent: the bytes that were saved.
  4. Retry only inside the window. Record when the first attempt started. Past the provider's window, don't resend: check the provider for what happened (list recent emails, look up the payment) and continue from that.
  5. Never retry an uncertain write without a key. If the API offers no idempotency, the only safe moves are to check its state or to ask a person.

Rules 2 and 4 are why this belongs in the runtime rather than the prompt. A model can be told to reuse a key, but it can't promise that the key survives a crash, or know when a window started.

How a durable workflow applies them

A workflow that runs for minutes or hours, calling several tools and saving checkpoints between them, hits the third outcome whenever it's interrupted mid-call. On CrabTalk workflows, continuing an interrupted run follows these rules:

  • A step that only reads runs again.
  • A write whose outcome is unknown is retried only if the tool it called declares an idempotency window, the saved request still carries its original idempotency key, and the window hasn't expired since the first attempt.
  • Otherwise continuation is refused, with the reason: no window, a changed key, or an expired window.

A service declares the window per tool, matching what its provider promises: the Resend service declares 24 hours on its send tools. So a run interrupted while sending email can continue the next morning without sending twice, and one interrupted two days ago stops and says why, instead of guessing.

Checklist

  • Mark tools that only read as read-only, so agents retry them freely.
  • For each write, find the provider's idempotency key, its window and its mismatch error.
  • Generate the key once, save it with the payload before sending, and retry with both unchanged.
  • Past the window, look the outcome up instead of resending.