Home Knowledge Base Idempotency

Idempotency is the property of an operation where executing it multiple times produces the same result as executing it once — a critical design principle for AI agent systems, payment processing, and distributed APIs where network failures and retry logic can cause the same request to be executed multiple times, requiring safe deduplication to prevent duplicate charges, double-sends, and repeated side effects.

What Is Idempotency?

Why Idempotency Matters for AI Systems

Idempotency Implementation Patterns

Pattern 1 — Idempotency Key (API Standard): Client generates a UUID per logical operation and includes it as a header:

import uuid

idempotency_key = str(uuid.uuid4())  # Generate once, reuse on retries

def create_payment(amount: int, retry: bool = False) -> dict:
    return stripe.PaymentIntent.create(
        amount=amount,
        currency="usd",
        idempotency_key=idempotency_key  # Same key on retries
    )

Server behavior: if idempotency_key already seen → return cached response without re-executing. If not seen → execute and cache response.

Pattern 2 — Database Upsert (Write Idempotency):

-- Non-idempotent INSERT (fails on duplicate)
INSERT INTO orders (order_id, user_id, amount) VALUES ('ord_123', 1, 100);

-- Idempotent UPSERT (safe to retry)
INSERT INTO orders (order_id, user_id, amount) VALUES ('ord_123', 1, 100)
ON CONFLICT (order_id) DO UPDATE SET amount = EXCLUDED.amount;

Pattern 3 — Check-Then-Act (Conditional Write):

def send_notification(notification_id: str, message: str) -> bool:
    # Check if already sent
    if notification_store.exists(notification_id):
        return True  # Already sent — safe no-op

    # Send and mark as sent atomically
    notification_service.send(message)
    notification_store.mark_sent(notification_id)
    return True

Pattern 4 — Event Deduplication (Message Queue):

def process_event(event_id: str, payload: dict):
    # Deduplicate at consumer level
    if redis.setnx(f"processed:{event_id}", "1"):  # Atomic set-if-not-exists
        redis.expire(f"processed:{event_id}", 86400)  # 24hr TTL
        handle_event(payload)  # Process only if not already processed
    # else: duplicate — silently ignore

AI Agent Idempotency Design

For AI agents executing multi-step workflows:

1. Assign step IDs: Each planned action gets a unique step ID. 2. Check before executing: Before each tool call, check if step_id already completed. 3. Record completion: After successful tool call, record step_id as completed. 4. Resume safely: On agent restart, skip already-completed steps using recorded state.

def execute_step(step_id: str, action: Callable, *args) -> Any:
    # Check if already completed
    if agent_state.is_completed(step_id):
        return agent_state.get_result(step_id)  # Return cached result

    # Execute and record
    result = action(*args)
    agent_state.mark_completed(step_id, result)
    return result

Idempotency vs. Atomicity

Idempotency and atomicity solve different problems:

Both are needed: atomic operations prevent partial state; idempotent operations enable safe retry of atomic operations that may have failed after executing but before confirming.

Idempotency is the design property that makes retry logic safe — without idempotency, the combination of network unreliability and necessary retry logic creates systems that silently duplicate critical operations, and in AI agent systems where the same action might be attempted multiple times due to planning errors or execution failures, idempotent operations are the difference between reliable automation and chaotic double-execution of consequential actions.

idempotencyduplicatesafe

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.