Writing Idempotency Key Code for Your API With LLMs in November 2026: Claim the Key Before You Do the Work, Replay the Saved Response, and Reject a Reused Key With a Different Body
A customer taps "Pay" on a slow mobile connection. The request reaches your server, the charge goes through, and then the response is lost on the way back. The app retries. Without protection, you have just charged that customer twice. Idempotency keys are the standard fix: the client sends a unique key with each logical operation, and the server makes sure that operation happens at most once no matter how many times the request arrives. Ask an LLM to "add idempotency keys to this endpoint" and you will usually get something that looks right: read the header, check a table, do the work, save the result. The bugs hide in the order of those steps and in what happens when two copies of the same request arrive at the same moment. This guide covers how to prompt for idempotency key code, what to check in the draft, and the tests that prove it actually works. Examples use Node.js and PostgreSQL, but the design carries over to any stack. Pressing an elevator button fiv...