API Idempotency: The Complete Guide
What an idempotency key actually promises, how to generate and scope one, and what Stripe, Gmail and other providers guarantee once a retry is already in flight.
An idempotency key does not make a call safe by itself. It tells the provider: if you see this exact key again, return the first result instead of running the request a second time. The guarantee only holds for the window the provider stores that key, and only for the operations the provider has chosen to make idempotent. A retried write without a key is not a second attempt. It is a second charge, a second refund, or a second email, and an agent calling the API has no way to tell the difference from the outside.
Key generation: deterministic vs. random
Two approaches work in production. A deterministic key, built from the operation and its real identifiers (customer ID, invoice ID, amount, a timestamp bucket), means the same logical action always produces the same key, so a retry from any source collapses onto the original call. A random key, a UUID generated once per logical action and reused across its retries, works too, but it puts the burden on the caller to persist that UUID correctly across timeouts and process restarts.
For an agent making the call, deterministic keys are the safer default. The agent's own retry loop, or a human re-running a failed step an hour later, does not need to remember anything to stay safe.
Scope and TTL: what the key actually protects
Keys are scoped to an account or API key, not globally unique, so the same key string used by two different tenants is not a collision. TTL is where providers diverge most, and it is the detail that catches teams who tested idempotency once and assumed it always holds. A key that protected a retry five minutes after the first call may not protect a retry five days later, once the provider has expired it from its store. Treat the TTL as the real safety window, not the existence of the header.
What providers actually guarantee, as of this check
Stripe documents an Idempotency-Key header with a 24-hour retention window, applied to its write endpoints. That is a strong, documented guarantee and the one most engineering teams model their own expectations on. Most email providers do not document the same pattern for sending a message; we cover the provider-by-provider picture separately. Coverage varies by provider and changes over time, so verify the current behavior in that provider's docs before you rely on it, not from a blog post written on a different date.
Where this breaks down for AI agents specifically
A human who isn't sure if a request went through usually waits, checks a dashboard, or asks someone. An agent in a loop retries. That difference is the whole risk surface. A retried write is a second refund, not a second attempt, and the agent's own reasoning about whether the first call "probably failed" is not a safety mechanism. The only thing that is, is the API refusing to run the same write twice.
How Swytchcode enforces this by default
Swytchcode's execution pipeline retries POST and PATCH calls on rate-limit errors only when an idempotency key is set on that call. No key, no automatic retry of a write. The pipeline covers key handling and backoff behavior the same way regardless of whether the call came from a CLI run, an SDK call, or an MCP client.
FAQ
Does every API support idempotency keys?
No. Support is inconsistent across categories, and email sending APIs are the clearest gap; see our provider-by-provider breakdown in which email APIs support idempotency keys.
What happens if I reuse a key with different parameters?
Behavior varies by provider: some reject the request outright, others silently return the original response and ignore the new parameters. Test this deliberately against the specific provider and endpoint rather than assuming either behavior.
Can idempotency replace retries with backoff?
No. Idempotency makes a retry safe to attempt; it doesn't decide when to retry, how long to wait, or what to do after repeated failures. That's the job of a durable execution layer.
Related reading
More content
How to Debug an AI Agent That Called the Wrong API
Working backwards from the response your agent got to the tool-selection decision that caused it, which is where most of the real debugging time goes.
iPaaS Was Built for Integrations, Not Agents
Deterministic trigger-action versus non-deterministic tool choice, and why the governance model that works for one doesn't transfer cleanly to the other.
Build Your Own MCP Server or Buy an Execution Layer
The real cost line isn't the connectors. It's the maintenance of the stitching layer underneath them, and that bill doesn't stop arriving after launch.
