How AI Coding Agent Memory Works: Append-Only Logs, Checkpoints, and Recovery
This article breaks down how memory is designed in AI coding agents, using an append-only JSONL event log as the single source of truth. It explains why memory is a prerequisite for recoverable, auditable, and resumable sessions, how session logs are stored outside the workspace, and how three mechanisms—recording, checkpointing, and replay-based recovery—work together. It also covers practical engineering guidance on storage isolation, immutability, tiered compression, auditability, and cloud integration.
1. Where the Problem Starts: Why Agents “Forget”
When an AI coding tool is editing multiple files in a large codebase in one continuous run, the process can be interrupted by an abnormal exit, resource reclamation, or fluctuations in the runtime environment. Whether it still knows “which step it had reached” after a restart depends on whether the underlying harness supports persistent memory.
An agent without memory shows three typical symptoms: it cannot answer when the user asks “where did we leave off”; it cannot resolve references such as “adjust that function we just touched” across multiple turns; and when something goes wrong, it cannot replay which actions it actually performed. Memory, then, is not a nice-to-have feature but a baseline capability that makes a session recoverable, auditable, and resumable.
This kind of design can be abstracted as an equation:
memory = an append-only ledger + recording + checkpointing + recovery
The ledger is the single source of truth, and the three mechanisms are responsible for writing, persisting, and rebuilding.
2. What Memory Looks Like on Disk: One Session, One Log File
In this design, memory is not an opaque record inside a database. It is a file that lives outside the workspace, and its path is assembled from three parts:
sessions/<escaped-workspace-path>/<session-id>/session.jsonl.zstd
sessions: the session root directory, isolated from the workspace so it does not pollute business code;- the middle segment: the absolute workspace path, escaped and used as a directory name, so different projects never bleed into each other;
- session ID: the unique identifier of a task; sub-agent sessions are further distinguished by delegation depth (0 at the top level, increasing one level at a time);
session.jsonl.zstd: the log itself, JSONL compressed with a general-purpose lossless algorithm, where each line is one JSON event after decompression.
The first line of the file is the session header, which records metadata such as the session ID, creation time, workspace path, and delegation depth. The header is immutable: identity is fixed on the first line, and everything after it is append-only. This order of “establish identity first, then write facts” guarantees that reading the log at any moment makes it unambiguous whose memory it is.
3. Recording: Why an Append-Only Event Stream Is Deliberate
Append-only is the core of all the semantics, and it shows up on three levels.
First, lines that have been written are never rewritten. Events are appended continuously with monotonically increasing sequence numbers, and no historical record is ever modified or deleted. This is not an implementation detail but a principle: the log is the fact, and rewriting history is equivalent to fabricating memory, which strips both auditing and recovery of their meaning.
Second, compression and packing save space. Log lines do not necessarily map one-to-one to events: when a large number of consecutive events of the same type appear (for example, model output split into many small chunks), the backend packs and aggregates them before writing them to disk, which reduces both the number of I/O operations and the compressed size.
Third, write order is causal order. Events are arranged in the order they occur, naturally forming a replayable timeline and providing a deterministic basis for later recovery and problem diagnosis.
The write logic can be sketched as:
on_event(e):e.seq = last_seq + 1buffer.append(e)if buffer.size >= threshold or e.type in critical_types:flush(buffer) # write in order, never go back and modify
4. Checkpoints: Shrinking the Window That Could Be Lost
Append-only solves “no tampering,” but it cannot solve “a crash before the buffer is flushed.” The checkpoint strategy is responsible for forcing a complete write at critical moments, narrowing the potential loss window to an acceptable range.
Common trigger points include: critical session state transitions (such as the start or end of a tool call), the buffer reaching its capacity threshold, a fixed time interval, and task completion. In implementation terms, this is usually a standalone module that answers only two questions: when a flush is mandatory, and whether an integrity marker is needed before flushing.
should_checkpoint(buffer, last_checkpoint_time):return buffer.full()or now - last_checkpoint_time > intervalor pending_critical_event(buffer)
It is worth stressing that a checkpoint is not a “scheduled backup” but a “forced sync based on semantic importance.” The goal is that after a crash and recovery, the agent falls back at most to the most recent semantically complete state, rather than to an arbitrary half-finished action.
5. Recovery: Rebuilding Context from the Log and Continuing the Turn
Recovery is essentially a replay: read the log, verify its integrity, rebuild the conversation history and tool call state, and then continue from the previous turn. The steps are:
- Locate the session directory and decompress the log;
- Read the session header on the first line and verify session identity and workspace consistency;
- Replay events in order to rebuild the message sequence, tool call results, and delegation levels;
- If there is an unclosed event at the end, roll back to the most recent complete checkpoint;
- Hand the rebuilt context to the inference loop and continue with the next turn.
restore(session_dir):log = decompress(session_dir + "/session.jsonl.zstd")header = log.first()assert header.workspace == current_workspacestate = replay(log.events())return state.last_consistent()
6. Engineering Practices and Best Practices
- Isolated storage: keep the session directory outside the workspace to avoid mixing it with version control or build artifacts;
- Immutability first: any need to “correct history” should be expressed by appending a new event, not by rewriting an old line;
- Tiered compression: aggregate and pack high-frequency small events, and flush critical events promptly, balancing space and reliability;
- Auditability: because the log is a plain-text timeline, troubleshooting can be done by decompressing it and reading it line by line;
- Working with general cloud capabilities: in production, logs can be archived to object storage, and log services plus monitoring and alerting can track flush latency and failure rates.
7. Conclusion
Memory in an AI coding agent is not mysterious: an append-only JSONL event log, plus the three mechanisms of recording, checkpointing, and recovery, form the minimal closed loop that makes a session recoverable, auditable, and resumable. Understanding this chain helps developers judge whether a harness’s memory is reliable, and guides the right trade-offs when designing persistence in a system of your own.
