How AI Coding Agent Memory Works: Append-Only Event Logs, Checkpoints, and Replay
This article explains the core design principles behind memory in AI coding agents, focusing on a simple engineering model: an append-only event log combined with three mechanisms—recording, checkpointing, and recovery (replay). It covers why agents lose state when a process exits, how memory is stored as a readable JSONL file with compression and a three-tier directory layout, and the practical constraints that make recovery reliable. The discussion stays at the general engineering level and does not reference any vendor-specific implementation. Readers will find a practical implementation checklist for building recoverable, auditable, and operable agent memory.
1. Where the Problem Starts: Why Agents “Forget”
Picture an ordinary scenario: a coding agent is running a refactoring task in a large codebase. It has already modified three files and is about to start on the fourth when the host process exits unexpectedly. After a restart, the user asks, “Where did we leave off?” and the agent has no answer. The user then says, “Adjust that function again,” and the agent cannot resolve what “that function” refers to. Later, when someone wants to review what the agent actually did, there is no record to fall back on.
The root cause is not model capability. It is whether the agent runtime (the harness) has a reliable memory layer. An agent without memory is effectively stateless: every round of reasoning starts from zero, the context lives only in process memory, and when the process disappears, so does the agent’s awareness.
The industry has gradually converged on a simple equation:
Agent memory = an append-only event ledger + three mechanisms: record / checkpoint / recover
This article is built around that equation. The entire discussion stays at the general engineering level and does not touch on any specific vendor’s implementation details.
2. The Physical Form of Memory: A File, Not a Black Box
Mature designs materialize “memory” as a real file on disk: each session maps to a log file, using a combination of “JSONL text + general-purpose lossless compression.” The file name carries a .jsonl extension plus a compression suffix. Once decompressed, it is plain text with one JSON event per line—human-readable and easy to inspect line by line with command-line tools.
The directory layout typically carries three layers of information:
- Session root directory: the single entry point for all sessions;
- Workspace-escaped directory: path separators in the project root path are replaced with safe characters, so historical sessions for the same project stay grouped together and different projects do not pollute each other;
- Session identifier directory: one directory per task, holding that session’s log and any attached artifacts.
The payoff is operability: locating every trace of a given task takes a single path, and backup, migration, and auditing all happen at directory granularity, with no dependency on any proprietary storage service.
The first line of the log is the “session header,” which records metadata such as the session identifier, creation time, workspace path, and delegation depth (0 for a top-level session, incrementing level by level for sub-agent sessions). Once written, the session header is immutable—identity is fixed in the first line, and only appends are allowed afterward, never rewrites.
3. Mechanism One: Recording (Append-Only Event Stream)
“Append-only” is the core semantic of the whole design, and it shows up on three levels.
First, lines that are already written are never rewritten. Events are appended continuously with a monotonically increasing sequence number, and no historical event is ever modified or deleted. This is not an accidental implementation detail but a deliberate constraint: the log is the single source of truth, and modifying history amounts to forging memory, which would undermine the credibility of all downstream analysis.
Second, consecutive events of the same type are batched and compressed. Log lines and events are not strictly one-to-one: when a large run of the same event type appears (for example, model output split into dozens of streaming chunks), the backend merges and packs them, then compresses them together, which significantly reduces size and write amplification.
Third, the write path is kept as simple as possible. Append writes are naturally sequential, can be flushed in batches, and do not corrupt existing content after a crash—at most the un-flushed tail is lost. This leaves a clear boundary for “recoverability.”
A schematic event stream looks like this (fields are generic illustrations):
{"seq":1,"type":"session_header","sessionId":"demo-002","workspace":"/.../workspace","delegationDepth":0}{"seq":2,"type":"user_message","text":"Fix the login timeout issue"}{"seq":3,"type":"assistant_delta","text":"Let me locate it first"}{"seq":4,"type":"tool_call","name":"read_file","args":{"path":"..."}}{"seq":5,"type":"tool_result","status":"ok","summary":"..."}
4. Mechanism Two: Checkpoints
Append-only solves “don’t lose existing data,” but it does not answer “when is a point safe?” The checkpoint strategy addresses exactly that question: at key moments, buffered content is forcibly written out and flushed to disk, forming a confirmable boundary.
Common trigger points include: after the session header is written, after each round of user input ends, after a tool call returns, before and after delegating a subtask, and when the process receives an exit signal. The policy layer usually allows the strength to be configured: a more conservative setting flushes more frequently, so less is lost in a crash but write overhead is higher; a looser setting gives better throughput but, in extreme cases, may roll back to the previous checkpoint.
Two engineering points deserve attention. First, checkpoints must align with sequence numbers and compression block boundaries; otherwise recovery will hit half an event. Second, the flush action should be observable, making it easy to count how many forced writes actually happened during a task.
5. Mechanism Three: Recovery (State Reconstruction via Replay)
Recovery is essentially “replay the log and rebuild state”: read the session header to confirm identity and workspace, parse events in sequence order, rebuild the conversation history, tool call records, and the round that still needs to continue, then resume execution from after the last complete checkpoint.
Three constraints are critical. Parsing must be fault-tolerant—a truncated tail line should be discarded rather than causing an error exit. The reconstruction result needs a consistency check against the in-memory state; for example, tool calls and their results must be paired. And after recovery, the model should be explicitly told to “continue from the interruption point,” so it does not repeat modifications that were already completed.
6. Observability and Governance
Since the log is readable text, it becomes a first-class citizen for troubleshooting and auditing: analyzing the distribution of event types, pinpointing abnormal tool calls, and reconstructing the full trace of a failed task can all be done offline. If centralized search and alerting are needed, the session logs can be connected as a data source to general-purpose logging services and monitoring/alerting capabilities, without changing the agent itself.
7. Implementation Checklist
- Define the event model and the immutable session header first, then talk about compression and policy;
- Keep sequence numbers monotonically increasing, and allow a truncated tail to be discarded;
- Align checkpoint boundaries with compression blocks;
- The recovery flow must include a consistency check and an explicit “continue from the interruption point” prompt;
- Isolate session directories by workspace to make backup and auditing easier.
Only when memory is built as a verifiable ledger does an agent gain the three basic reliability properties—recoverable, traceable, and operable—which is also the prerequisite for complex, long-running tasks to stay stable.
