Design Principles and Implementation Practices of Agent Conversation Memory

    This article explores how to design reliable memory for AI agents. Starting from the real-world pain point of losing context after a process crash, it introduces a proven design: memory equals an append-only event log combined with three mechanisms—recording, checkpointing, and recovery. The article explains the physical layout of per-session log files (compressed JSONL), the append-only semantics, crash-safety handling, checkpoint placement at semantic boundaries, and how to rebuild conversation state by replaying events. It concludes with practical engineering takeaways, including treating state as a projection of events and combining local logs with cloud storage for long-term retention.

    Design Principles and Implementation Practices of Agent Conversation Memory

    1. Starting from a Real Pain Point: What Does an Agent Remember After a Crash?

    Imagine this scenario: a coding agent is modifying a large codebase. It has already changed three files and is halfway through a tool call when the process suddenly crashes. After the restart, the user asks, “Where did you get to just now?” Can the agent answer?

    The answer depends on whether the execution framework (the harness) implements a memory mechanism. An agent without memory is effectively amnesic: in a multi-turn conversation, when the user says “modify that function again,” it cannot understand what “that” refers to; and when the output looks wrong and someone needs to figure out “what operations did it actually perform,” there is no evidence to inspect.

    An agent’s memory should not be a mysterious black box. It should be a concrete, auditable, persistent artifact. A design approach widely validated in the industry can be summarized as one equation:

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

    Let’s break down these components one by one.

    2. The Physical Form of Memory: One Log File per Session

    In this type of design, each session corresponds to an independent log file on disk. A typical organization scheme starts from a session root directory, isolates different projects by directories named after escaped workspace paths, then distinguishes sessions by session ID, and finally persists to a compressed JSONL event log, such as session.jsonl.zstd. JSONL means each line is an independent JSON event, while zstd provides a high compression ratio with fast decompression. Once decompressed, the file is plain text, making it easy to review manually and process with standard tools.

    Decompressing the log left behind by a real task reveals dozens of event lines. The first line is the session header, which records metadata such as the session ID, creation time, workspace path, and delegation depth. Delegation depth is a field worth noting: a top-level session has depth 0, and when the main agent delegates a task to a sub-agent, the child session’s depth increments, enabling the tracing of hierarchical task decomposition.

    The session header has one important property: immutability. A session’s identity is fixed the moment the first line is written. From then on, only appending events is allowed—rewriting is never permitted. Every subsequent line is a numbered event, such as user input, model output, a tool call request, or a tool result. Events increase strictly in sequence, forming a complete timeline.

    3. Mechanism One: Recording—An Append-Only Event Stream

    “Append-only” is the core semantic of this log, reflected in three aspects.

    First, written lines are never rewritten. Events are appended sequentially, and no event that has been written will be modified or deleted. This is not an implementation detail but a deliberate architectural decision: the log is the single source of truth, and modifying history is equivalent to fabricating memory. Once in-place updates are allowed, the foundation of auditing, replay, and debugging collapses. This is exactly the application of event sourcing in the agent domain—state is not stored directly but derived from the sequence of events.

    Second, compression controls storage costs. Agent sessions contain large amounts of verbose model output and tool return content, so raw JSONL grows quickly. Using a streaming compression format with a high compression ratio (such as zstd) significantly reduces disk usage while preserving append semantics, and decompression is fast enough not to affect recovery efficiency.

    Third, crash safety. The most common log corruption scenario is a process crashing mid-write, leaving the last line as half a JSON object. A robust implementation must tolerate such truncation during recovery: discard the incomplete line, resume appending from the last complete event, and never simply exit with an error.

    4. Mechanism Two: Checkpointing—Forced Persistence at Critical Moments

    Append-only solves the question of “how to record,” but a hidden risk remains: if events are only buffered in memory and flushed in batches, then everything in the buffer is lost the moment a crash occurs. This leads to the second mechanism—the checkpoint policy.

    The core idea of checkpointing is that at certain critical moments—where loss would be unacceptable—the buffered events are forcibly written to disk and persisted (fsync), trading a bit of performance for determinism after a crash. Typical checkpoint timings include:

    • Turn boundaries: when a user message arrives and when the model’s complete response finishes;
    • Before and after tool calls: tool execution may produce side effects, so the request and result must be persisted before the next action;
    • Session lifecycle events: session creation, pause, resume, and termination.

    A checkpoint policy is fundamentally a trade-off between consistency and performance: forcing a flush for every event is the safest but most expensive option, while fully asynchronous flushing is the fastest but leaves a large loss window. A sensible approach places checkpoints at semantic boundaries, so that the granularity of potential loss never exceeds one redoable interaction.

    5. Mechanism Three: Recovery—Rebuilding Conversation State from the Log

    The ultimate value of recording is realized at recovery time. The recovery process typically looks like this:

    1. Locate and decompress the session log file;
    2. Parse events sequentially, skipping any truncated line at the end;
    3. Replay events in order to rebuild the conversation history, tool call records, and workspace state;
    4. Continue execution from the interrupted turn, or support retrospective user questions.

    Because the log is the single source of truth, the recovered state is strictly consistent with the state before the crash—this is exactly the guarantee bought by the append-only plus checkpointing design. In addition, the event log naturally supports auditing and replay: to figure out “what exactly did the agent do,” simply print the log along the timeline, and the input and output of every tool call are fully traceable.

    6. Design Takeaways and Engineering Recommendations

    This approach offers several general lessons for building reliable agent systems:

    • State is a projection of events. Do not treat the in-memory conversation object as the truth; treat it as a materialized view of the event stream that can be rebuilt at any time after a crash.
    • Isolation and session identity come first. Organize files by workspace and session ID with an immutable session header, which naturally supports concurrent multi-project, multi-session workloads.
    • Crash tolerance is mandatory. Append writes must account for half-line truncation, and recovery must be idempotent.
    • Combine with cloud capabilities. In large-scale deployments, local event logs can be asynchronously archived to object storage for long-term retention, paired with log services for search and analysis, and combined with monitoring and alerting to detect anomalous sessions—upgrading single-machine memory into an operable memory infrastructure.

    In summary, an agent’s memory is not mysterious: an append-only event log, combined with the three plain mechanisms of recording, checkpointing, and recovery, is enough to let an agent “remember where it got to” after a crash. Simple primitives with rigorous semantics are precisely the foundation of reliable systems.