AI-Powered Self-Hosted Blog
← All articles

Persisting AI Agent Conversation State: Checkpoints and Resume Patterns Across Restarts and Failures

6 min read

Table of contents
  1. Why Conversation State Is Difficult to Save
  2. Record the Conversation as Events
  3. Use Checkpoints to Make Recovery Fast
  4. Record Intent Before Calling a Tool
  5. Reconcile Before Resuming
  6. Handle Overlap, Change, and Retention

An AI agent rarely fails at a convenient moment. A process can crash after submitting a payment but before recording the response. A worker can lose its connection while writing a report. A deployment can replace a machine while the agent waits for a user’s approval. For a ten-minute task, starting over may be annoying. For an agent that has spent two hours processing 120 documents, it can mean repeating costly work or, worse, repeating an action that should happen only once. Reliable recovery requires more than saving the latest chat messages. The agent must know what it intended to do, what it recorded as complete, and what may have happened outside its process.

Why Conversation State Is Difficult to Save#

A conventional application often has an authoritative database row: update it, then read it after a restart. An agent’s working state is spread across several places. It may include the conversation, a task plan, model output, tool results, files, API requests, approval decisions, and a summary of earlier context. Those pieces can disagree. Suppose an agent calls an API to create a support ticket, and the API succeeds. The process crashes before the tool response is saved. On restart, the conversation says nothing about a completed ticket, but the ticket exists. Replaying the call could create a duplicate. A checkpoint therefore cannot mean “serialize the in-memory object every few minutes.” Time-based snapshots are useful for performance, but safe recovery depends on recording the boundaries around actions that affect the outside world.

Record the Conversation as Events#

An append-only event log gives the agent a history it can reconstruct. Instead of repeatedly overwriting one conversation blob, store events such as:

  • UserMessageReceived
  • AssistantMessageCommitted
  • ToolCallPlanned
  • ToolCallStarted
  • ToolResultRecorded
  • ApprovalGranted
  • CheckpointCreated Each event should carry a conversation ID, a monotonically increasing sequence number, a timestamp, and a schema version. Tool events also need a stable operation ID. The sequence number establishes order; the operation ID lets the agent recognize the same intended action after a retry. For example, an agent processing 120 documents might record that documents 1–40 are complete, document 41 is in progress, and the remaining documents have not started. A restart can reconstruct that position without asking the model to infer progress from a long transcript. The log should preserve the original user request and consequential decisions. A compact summary can help the model continue within its context window, but it should not become the only record. Summaries can omit details; the event history provides the evidence needed to check them.

Use Checkpoints to Make Recovery Fast#

Replaying thousands of events on every restart is unnecessary. A checkpoint stores a materialized view of the agent at a known event sequence. It might contain the current task stage, completed work items, pending tool operations, the latest approved plan, and a conversation summary. A simple checkpoint record could look like this:

{
  "conversation_id": "conv-7f2",
  "through_sequence": 184,
  "schema_version": 3,
  "stage": "process_documents",
  "completed_document_count": 40,
  "pending_operation_id": "doc-41-extract-v1"
}

On restart, load the latest valid checkpoint and replay events after sequence 184. If the checkpoint is missing or corrupt, rebuild from the event log. Create checkpoints at meaningful boundaries: after a batch of documents, after a human approval, or after a tool result has been durably recorded. A checkpoint every 20 documents limits routine replay to roughly one batch. It does not replace the event recorded around each external action. The checkpoint and its recorded sequence must agree. Store them in one transaction when the storage system allows it, or write a new immutable checkpoint and mark it current only after the write succeeds. A partial checkpoint must never appear to be the latest valid one.

Record Intent Before Calling a Tool#

The most important recovery boundary surrounds a side effect. Before sending an email, charging a card, or creating a ticket, persist a ToolCallPlanned event with the operation ID and request details. Then make the call and persist its result. That ordering creates three useful recovery cases:

  1. No planned event exists. The action was not started by this workflow.
  2. A planned event exists, but no result is recorded. The outcome is uncertain and requires reconciliation.
  3. A result is recorded. Continue from the recorded outcome. The second case is where careless retries cause damage. If the external service accepts idempotency keys, send the operation ID with the request. Retrying the same operation can then return the original outcome or avoid creating a second resource, according to that service’s contract. If the service has no such feature, query it using a stable reference, such as a ticket’s client reference ID. If neither a safe query nor an idempotent retry is possible, mark the operation as uncertain and require review. A local event log cannot prove that an external action did not happen during a network failure.

Reconcile Before Resuming#

Recovery should be an explicit workflow, not a prompt that says “continue where you left off.” A practical sequence is:

  1. Load the latest checkpoint and replay subsequent events.
  2. Identify operations that were planned or started without a recorded result.
  3. Check each operation against the external system.
  4. Record the confirmed outcome or an unresolved status.
  5. Rebuild the model’s working context and continue from the next safe step. Consider the document agent again. It has extracted text from 40 files and started document 41 when its worker dies. If extraction only reads a file, it can usually retry. If the next step publishes a summary to a shared workspace, the agent should first check whether that summary already exists under its stable operation ID. The same task contains both repeatable work and work that needs reconciliation. Human decisions deserve the same care. If a user approved a draft before the crash, persist that approval as an event tied to the exact draft version. After recovery, the agent can proceed with that version without asking again. If the draft changed, the earlier approval should not silently apply.

Handle Overlap, Change, and Retention#

Restarts can briefly leave two workers believing they own the same conversation. Give each active worker a lease or generation number, and reject writes from an older generation. Sequence checks prevent two workers from appending conflicting next events. External operations still need their own idempotency or reconciliation strategy because a lease cannot undo a call already sent. Stored state also outlives code versions. Version event and checkpoint schemas, and test that a new release can read older records. If a checkpoint cannot be migrated, replaying versioned events may still recover the task. Finally, decide how long to retain each layer. A recent checkpoint makes startup fast; older events support audit and recovery; sensitive tool payloads may need a shorter retention period or separate protected storage. Keep enough information to establish what happened without copying secrets into every summary. A resilient agent does not need to predict when it will fail. It needs a durable record of intent and outcome, checkpoints that speed reconstruction, and a recovery path that checks uncertain actions before moving forward. With those pieces in place, a restart becomes a controlled continuation rather than a guess.