Don't Let Five Clicks Cost Five Token Bills: Idempotency Discipline for RAG Platforms
In production RAG, retries and impatient users are routine; without server-side idempotency, token bills quintuple and retrieval quality collapses into duplicate-flooded, stuttering answers. These patterns let a platform absorb repeated clicks and network flakiness at zero extra cost and keep the vector store clean.
Client retries and impatient users are a fact of life in production RAG. A single 50MB upload, re-submitted five times, can quintuple the token bill and flood the vector store with five identical chunks, so Top-K retrieval returns the same passage five times and the LLM stutters. The fix is concentrated on the server: no frontend button-disable can stop network proxies, SDKs, or malicious scripts from retrying.
The architecture works in four layers. The API facade checks an Idempotency-Key against a partial unique index, pairs it with a versioned request fingerprint to reject same-key-different-payload replays, and uses transaction-level advisory locks scoped to person, credential, and key. Below that, content-hash deduplication on the file bytes skips parsing and embedding entirely when the same file arrives again under a new key. Downstream, an in-memory asyncio lock plus a single-transaction DELETE+INSERT snapshot replacement guarantees only one clean chunk set per document. Finally, cancellation itself is idempotent: cancel markers and leases persist so a restarted container can resume cleanup until the system converges.
Frontend button disabling is ineffective because proxies, SDKs, and malicious scripts all retry independently; the server must own idempotency end to end.
Binding idempotency to credential identity prevents privilege escalation when a high-privilege token is revoked and a lower-privilege token retries with the same key.
Content-hash dedup must respect lifecycle state: reusing a hash from a failed parse can cause the new, valid file to be garbage-collected.
Single-transaction snapshot replacement is simpler and more reliable than diff-based row updates when chunk boundaries shift.
Idempotent cancellation requires persisting intent and leases so recovery coroutines can converge the system to a clean state after crashes.