# Long-running task Agent checkpoint-resume: first mark the actions that cannot be replayed

Date: 2025-12-08
Category: Technology
Tags: Agents, Fault Tolerance, Idempotency
Summary: Starting from a stress-test incident in which a notification email was sent repeatedly, this breaks down the recovery design of long-running task agents: classifying side effects, checkpoints as recovery contracts, idempotency keys that travel with side effects, and how to handle partially successful states.

Turn a batch ticket-processing Agent from "rerun the whole thing on failure" into one that can resume from checkpoints. The problem showed up during stress testing: it got killed after sending a notification email at some intermediate step, and on recovery it sent the email again. The checkpoint was actually being saved—the problem was that I treated the checkpoint as a progress bar and didn't treat side effects as state.

## First, mark the actions that cannot be replayed

The difference between long tasks and short tasks isn't the number of steps—it's that they touch the outside world along the way. For actions like classification, summarization, and draft generation, recomputing after a failure costs at most some tokens; for actions like sending email, writing to the CRM, charging money, and closing tickets, replaying once is an incident.

I now start by listing a side-effect table and sorting every action into four categories: recomputable, overwritable, exactly-once, irreversible. Recomputable covers model inference and writing temp files; overwritable covers writing drafts and refreshing caches—as long as the written content is identical, it can be redone; exactly-once covers notifications, payments, and external system state changes; irreversible covers deletion, archiving, and external commitments. This table determines where checkpoints should be cut, rather than making checkpoints adapt to the code structure.

The first cut I make is usually between "decide" and "execute." The Agent first produces a plan; the plan itself is pure data and can be regenerated, compared, and modified repeatedly. During actual execution, each side-effecting action is recorded separately. That way, on recovery the plan can be recomputed, but the execution records cannot be replayed.

## A checkpoint is a recovery contract

The most common checkpoint I've seen looks like this: task_id, current step, last message. It tells you "how far it got," but not "which things have actually happened." Recovery logic resumes from that step right away, easily mistaking a half-successful state for one that never happened.

The checkpoint I store now roughly contains these fields: task_id, plan_version, cursor, input_hash, step_outputs, effect_ledger, schema_version, retry_count. step_outputs stores only the structured results that later steps actually need, not the whole conversation; effect_ledger is the side-effect ledger, with one record per external action and only three states: intent, committed, unknown.

Order matters. Write intent before the external call, write committed after the call succeeds, and leave it at unknown if it fails or the process is killed. On recovery, unknown must not be retried directly—query the external system first, and hand it to a human for confirmation if the query finds nothing. This design costs one extra database write, but it makes "uncertainty" explicit. Many duplicate side effects aren't caused by too many retries; they're caused by the system treating uncertainty as failure.

There's also schema_version. When tool parameters, prompt structure, or output format change, an old checkpoint may no longer be safe to resume from. My approach is: if the version is incompatible, don't force a continuation—mark it as pending migration, or just start a new task. Resuming from a checkpoint doesn't have to mean resurrecting the old state; sometimes admitting the old state isn't recoverable is cheaper.

## Idempotency keys must travel with side effects

Don't generate just one idempotency key at the task level. If a long task sends multiple notifications and writes to the database multiple times, a task-level idempotency key either makes all actions look the same or stops working on retry. A more robust approach is one stable key per side effect: task_id + step_id + effect_type + payload_hash. Retrying the same intent keeps the key unchanged; only when the content actually changes does the key change.

If the downstream supports idempotency keys, pass them through. If not, you need a local deduplication table backed by a unique constraint. For example, insert a pending record before sending a notification; a unique-key conflict means the action has already been initiated. But this is only a local fallback—the window for cross-process crashes still exists, so you also need the unknown state and external queries described earlier for reconciliation.

LLM calls themselves usually have no external side effects, so retrying them is relatively safe—but not entirely free. I look at model calls and tool calls separately: model output can be regenerated, tool execution needs a ledger. Don't build complex idempotency at the model layer; focus on the tool layer.

## Half-success is the norm

The most failure-prone spot in batch tasks is half-success: sixty of a hundred tickets processed, and among them three notifications were sent but the status wasn't written back. If the checkpoint only records at the "batch" level, recovery can only redo the whole batch. So checkpoints must be fine-grained down to individual records—at minimum, recovery logic should be able to continue per item rather than per batch.

I add a dry-run at the recovery entry point: first compute a list of "what to do next" from the checkpoint, marking which items are recomputations, which are retries, and which need human confirmation. Execute only after confirmation. This dry-run doesn't solve everything, but it lets me see the duplication risk before an email actually goes out.

Testing is simple too: randomly kill the process before and after external calls, run a few dozen rounds, and finally check whether the actual number of effects in the external system matches the number of committed entries in the ledger. A mismatch means there's still a window that hasn't been modeled. For monitoring, I watch three numbers: recovery count, unknown side-effect count, and duplicate calls blocked by deduplication. If unknown stays above zero for long, the reconciliation logic isn't finished.

Next time I design a long task, I'll draw the side-effect table first, then decide checkpoint granularity. If all you have is a job-level checkpoint, don't rush to add more fields—add effect_ledger first. In your Agent, which action absolutely must not be replayed? Mark it out, and your recovery logic will have a starting point.
