DEV Community

Cover image for Day 25/90 — Why AI Agents Can Execute the Same Action Twice After a Crash
Rimsha Kanwal
Rimsha Kanwal

Posted on

Day 25/90 — Why AI Agents Can Execute the Same Action Twice After a Crash

Handling Side Effects, Execution Replay, and Idempotency in LangGraph

One of the biggest challenges in building production-ready AI agents is not just making them recover from failures. It's making sure they don't repeat operations that have already been completed.

Imagine an AI agent successfully creates a support ticket, crashes unexpectedly, and resumes execution.

What happens if the agent creates the same ticket again?

This is where execution replay, side effects, and idempotency become critical.

The Problem: Recovery Doesn't Always Mean Correctness

Consider an AI customer support agent built using Python, LangGraph, and PostgreSQL.

A user asks:

"Create a support ticket for my delayed order."

The agent performs the following operations:

  1. Understands the user's request.
  2. Validates the order information.
  3. Calls the create_support_ticket() tool.
  4. Successfully saves the ticket in PostgreSQL.
  5. Crashes before the completed operation is recorded in the workflow checkpoint.

When execution resumes, the unfinished operation may run again.

The result?

Two support tickets are created for the same request.

Technically, the agent recovered successfully. But from a business perspective, the system failed.

This is an example of a side-effect duplication problem.

Why Checkpointing Alone Isn't Enough

LangGraph supports persistent execution through checkpoints, allowing workflows to resume after interruptions and failures.

However, checkpointing doesn't automatically guarantee exactly-once execution of external operations.

Consider the difference:

  • Checkpointing: Helps recover previously saved execution state.
  • Replay: Re-executes work that wasn't durably recorded as completed.
  • Side effects: Operations that modify external systems, such as creating database records or sending emails.
  • Idempotency: Ensures repeating the same logical operation doesn't produce additional effects.

An important distinction is that a workflow can have reliable state recovery without having reliable side-effect handling.

The Engineering Solution: Idempotent Tool Execution

Instead of relying exclusively on checkpoints, we can design external tool operations to tolerate retries.

For example, every ticket creation request can receive a unique idempotency key.

def create_support_ticket(
    idempotency_key: str,
    ticket_data: dict
):
    existing = db.get_ticket_by_key(
        idempotency_key
    )

    if existing:
        return existing

    return db.create_ticket(
        idempotency_key=idempotency_key,
        **ticket_data
    )
Enter fullscreen mode Exit fullscreen mode

This simplified example demonstrates the principle.

In production, the database should also enforce uniqueness on the idempotency key, with an atomic transaction or conflict-handling mechanism to prevent concurrent requests from creating duplicate records.

The key must remain stable across retries of the same logical operation.

What About LangGraph?

LangGraph's durable execution model can help resume workflows from persisted state.

For operations with side effects, task boundaries and checkpoint placement matter.

However, external actions such as database writes, payments, and API calls still require their own retry-safe design.

A reliable architecture combines:

  • Persistent checkpoints to support execution recovery.
  • Idempotent operations to avoid duplicate side effects.
  • Database constraints to protect against concurrent duplicates.
  • Controlled retries to handle transient failures.
  • Failure injection tests to verify replay behavior.

A Practical Development Experiment

To test this behavior, build a small LangGraph workflow containing three steps:

Step 1: Validate a support ticket request.

Step 2: Create a ticket in PostgreSQL using an idempotency key.

Step 3: Return the created ticket information.

Now deliberately simulate a crash after the database insertion but before the operation's successful completion is checkpointed.

Resume the workflow using the same execution context and operation identifier.

Compare the results with and without idempotency protection.

The expected outcome is simple:

One logical ticket creation request should result in one ticket, even when execution is retried.

Final Takeaway

Building reliable AI agents requires thinking beyond prompts, reasoning, and tool selection.

The real engineering challenges become visible when systems crash, retry operations, lose connections, or resume partially completed workflows.

A successful agent isn't just one that can finish a task.

It's one that can recover from failure without introducing incorrect business behavior.

Checkpointing makes recovery possible. Idempotency makes repeated execution safer.

And understanding that difference is essential when moving from AI agent prototypes to production systems.

Research references: LangGraph documentation on durable execution, persistence, and fault tolerance.

Top comments (0)