AI Agent Memory Design Principles: Append-Only Logs, Checkpoints, and Recovery
This article breaks down the core design principles behind AI agent memory: an append-only event log combined with recording, checkpointing, and recovery. It explains why persistent memory separates one-off Q&A tools from resumable execution agents, how a session maps to a single compressed JSONL log file, why writes are strictly append-only, when checkpoints force data to disk, and how history is rebuilt after a crash. Engineering practices such as deduplication, lifecycle management, observability, and security are also covered, along with how the pattern can run on general-purpose cloud capabilities.
AI Agent Memory Design Principles: Append-Only Logs, Checkpoints, and Recovery
1. The Starting Problem: Why Agents Need Memory
Picture a real scenario: an AI coding assistant is refactoring a mid-to-large codebase. It has already modified three files and is halfway through the fourth when the process suddenly exits. After a restart, does it still remember where it stopped, why it made those changes, and what to do next?
Without persistent memory, an agent behaves like someone with amnesia:
- When the user asks “where did we get to?”, it cannot answer;
- In a multi-turn conversation, when the user says “adjust that function again”, it has no idea what “that” refers to;
- When something breaks and you want to review “what exactly did it do”, there is no record to check;
- Long tasks are forced to start over, wasting compute and time.
Memory is therefore not a nice-to-have. It is the dividing line between a one-off Q&A tool and a resumable execution agent. A fairly mature industry approach abstracts memory into a single equation:
Memory = an append-only ledger + recording + checkpoints + recovery
This article unpacks that equation layer by layer, covering the design principles, engineering constraints, and practical implementation points. It does not discuss any specific vendor product; it describes a general technical paradigm. The same architecture can be built on general-purpose capabilities such as object storage, log services, and container platforms offered by Baidu AI Cloud.
2. The Memory Carrier: One Session, One Log File
The mainstream implementation maps each session to its own file on disk, with a path typically assembled from three parts:
sessions/<escaped-workspace-path>/<session-id>/session.jsonl.zstd
sessions: the session root directory;- Middle segment: the absolute workspace path is escaped and used as a directory name, for example turning
/Users/Apple/projects/.../workspaceinto--Users-Apple-...--, which keeps sessions from different workspaces from bleeding into each other; - Session ID: the unique identifier for a single task;
session.jsonl.zstd: the log body itself, in JSONL format with zstd compression. Once decompressed it is plain text, with one JSON event per line.
This organization has three benefits. First, isolation by workspace avoids cross-contamination between multiple projects. Second, splitting into one file per session makes deletion, archiving, and migration all operate at session granularity. Third, plain text plus compression means the logs are directly readable by humans while still saving significant space.
The first line of the log is the “session header”, which records metadata such as the session ID, creation time, workspace path, and delegation depth (0 for a top-level session, incrementing level by level for sub-agents). The session header is immutable: identity is fixed in the first line, and from then on only events are appended, never rewritten. Each subsequent line is an event, with seq increasing monotonically.
3. Recording: An Append-Only Event Stream
“Append-only” is the core semantic of the whole design, showing up on three levels.
First, a written line is never rewritten. Events are appended continuously by sequence number, and any event already persisted is never modified or deleted. This is not an implementation detail but a deliberate constraint: the log is the single source of truth, and rewriting history amounts to fabricating memory, which would render both auditing and recovery meaningless.
Second, packing and compression save space. One line does not necessarily equal one event: when a large number of consecutive events of the same type appear (for example, model output split into dozens of small chunks), the backend packs them into a single line for storage, which in practice typically shrinks the log by more than half. They are expanded on demand at read time, transparently to the layer above.
Third, the write path is kept as simple as possible. Append writes are naturally sequential with little lock contention, and combined with fsync semantics this guarantees that “a successful write is truly persisted”. This is easier to reason about for correctness than schemes that randomly rewrite a file.
Illustrative append logic:
def append_event(session, event):event["seq"] = session.next_seq()line = json.dumps(event, ensure_ascii=False) + "\n"with open(session.log_path, "ab") as f: # append-onlyf.write(line.encode("utf-8"))f.flush()os.fsync(f.fileno()) # force persistence at the critical moment
4. Checkpoints: Forcing a Complete Write at Key Moments
Appending alone is not enough; you also need to solve the question of “when must data be persisted”. That is what checkpoints are for. Common strategies include:
- Turn boundaries: from one user input to the end of one complete reply;
- Before and after tool calls: especially before operations with side effects (writing files, sending messages);
- Delegation boundaries: before and after the main agent hands a task to a sub-agent;
- Periodic fallback: force a sync every N events or every T seconds.
The essence of a checkpoint is “converging uncertain in-flight state into a certain, recorded state”. The cost is a small amount of I/O; the benefit is that after a crash you lose at most one unfinished fragment instead of the entire history.
5. Recovery: Rebuilding History and Continuing the Turn
Recovery typically proceeds in four steps:
- Read the log and decompress it into text lines;
- Validate the session header, confirming the workspace and delegation depth;
- Replay events in
seqorder to rebuild the conversation history and tool-call context; - Continue from the last complete checkpoint, discarding or retrying unfinished fragments according to policy.
Pseudocode:
def restore(session_dir):events = [json.loads(l) for l in decompress(session_dir / "session.jsonl.zstd")]head, body = events[0], events[1:]body.sort(key=lambda e: e["seq"])history = replay(body) # rebuild conversation and contextreturn head, history # continue from the checkpoint
6. Engineering Points and Best Practices
- Balancing compression and readability: compression saves space, and decompressing makes logs readable during troubleshooting;
- Idempotence and deduplication: deduplicate by
seqduring replay to avoid repeated side effects; - Lifecycle management: archive and expire logs by session, and push cold/hot tiering down to object storage;
- Observability: feed logs into a unified log service with monitoring and alerting so abnormal exits can be traced;
- Security and compliance: logs may contain sensitive information and require encryption and access control.
7. Conclusion
The essence of agent memory is turning “remembering” into a verifiable engineering fact: an append-only ledger plus three mechanisms: recording, checkpoints, and recovery. It lets an agent resume after a crash, provides evidence for audits, and prevents amnesia during long tasks. Built on general-purpose cloud capabilities such as Baidu AI Cloud, this paradigm can be implemented directly as a reliable, scalable memory foundation.
