AI Agent Idempotency: Build Queues That Never Repeat Expensive Actions
Retries are inevitable; duplicate side effects are not. Use stable intent keys, effect ledgers, leases, and reconciliation for reliable agent queues.
Every serious agent system retries. Workers crash after sending a request but before saving the response. Networks time out while a provider completes the operation. Humans approve a task as a lease expires. At-least-once delivery turns these ordinary failures into duplicated emails, purchases, tickets, or deployments.
Idempotency means the same intended effect can be requested repeatedly while being committed once. The model cannot provide that guarantee. The queue, tool gateway, and external adapter must enforce it.
Give the Intent a Stable Identity
Generate an idempotency key when the business intent is accepted, before a worker or model attempts execution. Retries must reuse that key. Do not derive it from a timestamp, attempt number, or freshly generated model text.
Useful keys bind the tenant, workflow, action type, target, and semantic operation. Store a digest of consequential parameters beside the key. If a later attempt reuses the key with a different amount or recipient, reject it instead of guessing.
const key = hash({ tenant, workflowId, action: "refund", paymentId });
await effects.reserve(key, hash({ amount, currency }));
Put an Effect Ledger Around Every Mutation
Track states such as reserved, executing, succeeded, failed_retryable, and needs_reconciliation. Use a unique database constraint on the idempotency key. Claim work with a lease so another worker can recover abandoned jobs without creating a second logical operation.
The hard case is an ambiguous timeout: the external system may have succeeded while the local ledger still says executing. Do not immediately repeat the mutation. Query the provider by its idempotency key or external reference. If the provider cannot support lookup or idempotent writes, route ambiguity to reconciliation or human review.
Separate Planning Retries From Effect Retries
An agent may regenerate a plan many times. That does not authorize new business effects. Represent planned actions as immutable commands and require the executor to commit each command through the effect ledger.
If a revised plan truly changes the action, mint a new command identity and invalidate the old one explicitly. Never let minor wording differences create fresh payment or publication keys. Semantic authority comes from structured fields approved by policy, not the natural-language explanation.
Make Approval Bind to the Exact Effect
An approval should cover a command digest, not a conversational turn. Bind approver, expiry, target, parameters, and idempotency key. When data changes, require a new approval.
This prevents a delayed retry from using yesterday’s approval for today’s amount. It also makes audit logs intelligible: operators can see which human authorized which one-time effect.
Test the Failure Windows
Kill workers before the provider call, during the call, after success but before local commit, and during acknowledgement. Deliver the same queue message concurrently. Expire leases. Delay provider webhooks. Confirm that the final external effect occurs once and the ledger converges.
Exactly-once delivery across independent systems is usually an illusion. Exactly-once business effect is achievable when stable identity, deduplication, provider support, and reconciliation work together. Build those controls before giving the agent a tool with consequences.
Sources
> Want more like this?
Get the best AI insights delivered weekly.
By subscribing, you agree to our Privacy Policy. You can unsubscribe at any time.
> Related Articles
pgvector Can Return 4 of Your 10 Matches—Where Did the Rest Go?
A filtered vector search can quietly starve your AI app of evidence. pgvector has a fix, but builders need to measure recall and latency before trusting it.
Agent Memory Consolidation Scheduling: When to Merge, Summarize or Forget
Your agent doesn't need more memory — it needs a maintenance schedule. Here's how to build merge, summarize, and decay policies that keep long-term agents sharp instead of senile.
LLM Output Schema Evolution: Migrating Producers and Consumers Without a Flag Day
Your JSON schema will change. Here's how to evolve LLM output contracts safely — with dual readers, shadow validation, and staged rollouts instead of a risky big-bang cutover.
Most Read
- 1 Kimi K3.1 Pro: China's Open-Weight Model Just Caught the Frontier
- 2 OpenAI Is Buying the Bottleneck: Can Orchestra Bio Cut a 15-Year Drug Timeline?
- 3 1,068 Tokens Per Second: Can Mercury 3 Change How LLMs Generate Text?
- 4 Same Price, Better Coding: How Good Is DeepSeek V4.1 Flash 1017?
- 5 Used OpenCode? Should You Rotate Your API Key After the Console Breach?
Latest News
- Claude Code Left Secrets in 1,122 GitHub Files—Check This Setting
- Gemini Omni 1.2 Flash Makes 15-Second Video Cheaper—Then Comes the Edit
- GPT-6 Cyber Can Build Exploits—But Builders Face a Different Test
- 1,122 GitHub Files Exposed Secrets—What Did “Don't Ask Again” Save?
- Gemini Omni 1.2 Flash Offers $1.05 Clips—What Does That Buy You?
- GPT-6 Cyber Hits 48.5% on ExploitGym—Who Gets the Keys?
Tags
> Stay in the loop
Weekly AI tools & insights.