Designing Agent Memory Systems: Append-Only Logs and Checkpoint-Based Recovery

    AI agents need reliable external memory to maintain multi-turn context, recover from crashes, and support auditability. This article explains a memory design built on an append-only session log, immutable event streams, and checkpoint-based recovery. It covers the physical log file layout, the session header and event sequence structure, append-only semantics with storage-level batching, checkpointing to guard against data loss, and a four-step recovery process that replays events to reconstruct state. It concludes with design trade-offs and best practices for building a dependable, auditable memory layer for any agent framework.

    Designing Agent Memory Systems: Append-Only Logs and Checkpoint-Based Recovery

    Why Agents Need External Memory

    Large language models are inherently stateless: each inference call sees only the context passed in the current request. Once a session ends, the model has no recollection of anything that happened before. For an agent executing complex tasks, this causes three typical problems:

    1. Broken multi-turn collaboration: In turn three, a user says “modify that function we just worked on.” The agent must know which modification “just” refers to and which file is involved.
    2. No continuation after crashes: If an agent crashes while modifying the third file of a large codebase, without memory it cannot answer “where did I leave off”—let alone safely continue the remaining steps.
    3. Non-auditable behavior: When something goes wrong, operators and developers need to replay exactly what actions the agent took and why. Without records, there is no evidence chain.

    Therefore, a reliable agent framework must implement memory as a real, external file, not a hidden in-memory black box. The core design can be summarized as an equation:

    Memory = an append-only event log + three mechanisms: recording, checkpointing, and recovery.

    The Physical Form of Memory: The Session Log File

    In a concrete implementation, each session corresponds to a log file outside the workspace, with a typical path like:

    1. sessions/<workspace-path-escaped-directory>/<session-id>/session.jsonl.zstd

    Key points:

    • sessions is the root directory for all session logs;
    • The intermediate directory name is derived by escaping the workspace path (e.g., replacing path separators with hyphens), so that memory for different tasks and workspaces never gets mixed up;
    • The session id uniquely identifies a task;
    • session.jsonl.zstd is the log itself: internally it is JSONL (one JSON event per line), compressed as a whole with zstd. Once decompressed, it is plain text that can be inspected and analyzed with ordinary text tools.

    This “one file per session” layout brings three benefits:

    1. Isolation: Contexts of different tasks are naturally isolated with no cross-talk.
    2. Portability: A single file can be copied, archived, and transferred wholesale, making it easy to reproduce issues.
    3. Observability: Once decompressed, it is standard JSON lines that any generic tool can consume.

    Event Stream Structure: The Session Header and Immutable Identity

    The first line of the log is the session header, which differs from the events that follow. It records:

    • The session id;
    • Creation time;
    • The workspace path;
    • The delegation depth: 0 for a top-level session, incremented by one for each sub-agent session spawned by a parent agent, expressing the task-decomposition hierarchy.

    The session header has a critical constraint: it is immutable. The session’s identity is fixed the moment the first line is written; afterwards, only events may be appended, never rewritten. This is effectively a birth certificate for the memory, guaranteeing a stable and traceable identity.

    Subsequent events are appended with monotonically increasing seq numbers. A real task may produce dozens of events, covering user input, model output, tool calls, tool results, checkpoint markers, and other types. Each event is a self-describing JSON object containing the sequence number, type, timestamp, and payload.

    Recording: Append-Only Semantics

    “Append-only” is the cornerstone of the entire memory design, with rigor at three levels.

    1. Written lines are never rewritten

    Events are appended in increasing seq order; any event already written can never be modified or deleted. This is not an incidental implementation detail but a deliberate design choice: the log is the single source of truth. Allowing history to be rewritten would allow memory to be forged—rendering audit, replay, and debugging meaningless.

    2. Batching and compression save space

    The number of events and the size of raw content are often disproportionate. For example, when a model’s streaming output is split into dozens of small chunks, having one chunk per line makes the log very sparse. A common engineering approach is to merge consecutive same-type events into a single line at the storage layer (batched storage)—for instance, aggregating a batch of chunks into one “block” event. In practice, this optimization can shrink the log by roughly 60%.

    Note that this compression is a storage-layer optimization; the logical layer (reading, replaying) still sees individual events, and upper-level code is unaware of the batching.

    3. Order encodes causality

    Because the log is append-only, the natural order of events encodes both causal and temporal order: a tool call always precedes its result, and a checkpoint always appears after the events it covers. During recovery, replaying in order reconstructs the full state—no extra sorting fields or topological processing needed (seq is mainly used to verify continuity and detect truncation).

    Checkpoints: Forcing Durability at Critical Moments

    Appended writes usually go through a buffer; if the process crashes suddenly, buffered events that have not been flushed to disk are lost. The checkpoint mechanism’s purpose is to force all events produced so far to be fully written to disk at critical moments, and record a stable recovery point.

    Typical trigger points include:

    • Completion of a round of user input processing;
    • A tool call and its result being persisted;
    • Sub-task delegation boundaries;
    • Milestones of long-running tasks.

    Once a checkpoint is on disk, the recovery logic has a clear anchor: on restart after a crash, state is rebuilt starting from the most recent valid checkpoint, and any partial writes after it (if present) can be truncated after seq validation, ensuring the recovered state is complete and self-consistent. This mirrors the classic combination of a database write-ahead log (WAL) and checkpoints: the log guarantees no data loss, and the checkpoint guarantees speed.

    Recovery: Rebuilding the Conversation by Replay

    The recovery process can be summarized in four steps:

    1. Locate the session file: Find the corresponding log based on the workspace and session id.
    2. Load and decompress: Read the JSONL, decompressing and unpacking into logical events as needed.
    3. Validate and truncate: Check seq continuity; if incomplete transactions/events remain at the tail (crash residue), roll back to the last complete checkpoint.
    4. Rebuild history: Replay events in order to restore the conversation context and task state, then continue with the previous turn.

    From the user’s perspective, the experience is that after a process restart, the agent “remembers” what it did and can naturally continue. From the developer’s perspective, any session can be fully reproduced from the same log.

    Design Trade-offs and Best Practices

    • Simplicity over sophistication: A single JSONL file plus compression avoids heavy dependencies such as databases while retaining structured queryability.
    • Immutability in exchange for trust: Prefer a somewhat longer log over rewriting history; if a “correction” is needed, append a correcting event rather than editing an old one.
    • Layered abstraction: Recording (writing), checkpointing (flushing), and recovery (replay) are independent, so the storage backend can be swapped (e.g., for object storage or a log service) without touching upper-level logic.
    • Persist delegation depth: In multi-agent scenarios, the relationship between child and parent sessions must be written into the session header; otherwise the task hierarchy cannot be rebuilt after recovery.
    • Test crash recovery: Tests should simulate the process crashing after arbitrary events and verify that recovery matches in-order replay.

    Summary

    An agent’s memory does not need to be a complex vector store or database. An append-only, compressible, checkpointed session log is sufficient to support the three essentials: multi-turn context, crash recovery, and behavioral audit. The essence of the design is an immutable event stream as the single source of truth, checkpoints for crash safety, and in-order replay to rebuild state. Once you understand the “record–checkpoint–recover” trio, you can build a reliable, auditable, and maintainable memory layer for any agent framework.