Agent Memory Systems: A Four-Layer Architecture and Offline Consolidation Design
This article explains how to give AI agents persistent memory across sessions using a four-layer architecture, from raw conversation logs to context injection. It covers how offline consolidation (sometimes called "Dreaming") turns raw experience into reusable knowledge through correction, summarization, maintenance, and restructuring, and why storing more memories does not necessarily improve task performance. Readers will find practical guidance on storage choices, permissions and compliance, and building an evaluation loop to balance injection length, hit rate, and cost.
1. Why Agents Need Cross-Session Memory
Within a single session, an agent is essentially stateless: once the context window closes, the information used for that round of reasoning disappears. Real business tasks, however, are often continuous, multi-turn, and span multiple days—following a support ticket over time, maintaining an evolving technical document, or reusing prior conclusions in a long-running project. Starting from scratch every time forces the agent to repeat work and makes it prone to conflicting with established facts.
The industry is converging on a shared view: memory is not about stuffing the entire history back into the context. Instead, it requires a layered storage and consolidation mechanism that loads important information in the right form at the right time. A four-layer memory system with an offline consolidation process (internally referred to as Dreaming by one cloud vendor’s engineering team) is a representative abstraction. This article generalizes that approach.
2. The Four-Layer Memory Architecture: From Raw Records to Context Injection
From the bottom up, the memory system has four layers, connected by a “searchable → summarizable → injectable” relationship.
Searchable conversation records (the bottom layer)
Stores the raw trace of every interaction: user input, tool calls, intermediate results, failures, and retries. It prioritizes completeness and traceability, applies no early trimming, and serves as the evidence source for all downstream consolidation.Memory notes
Structured key points written by the model during or after a task, such as “this project uses a message queue for asynchronous decoupling” or “this API requires an idempotency key under concurrency.” Notes are a filtered output that emphasizes reusability and low noise.Memory representation (such as a wiki-style knowledge base)
Organizes multiple notes into a cross-document, cross-topic knowledge structure that supports reasoning about entities, concepts, and relationships. It fits scenarios where answering a question requires synthesizing multiple sources.Context injection (the top layer)
The minimal necessary set loaded automatically when a new session starts: stable preferences, key constraints of the current task, and the most recent progress. It must be extremely short and precise, representing the balance point between cost and effectiveness.
Information does not flow in only one direction. The offline consolidation process can read bottom-layer records to generate or correct upper-layer notes, and can also move information among notes, the knowledge representation, and the injected context. The design principle is to move important and stable information upward, push detailed material downward, and retrieve it on demand.
3. Offline Consolidation (Dreaming): Turning Experience into Knowledge
Humans form memories quickly as experiences happen, and some memories are consolidated during sleep and quiet rest, then integrated into long-term knowledge. Agents can follow a similar division of labor: the online phase focuses on execution and writes the process to logs, while the idle phase uses additional “sleep-state” computation for consolidation.
Offline consolidation typically involves four types of operations:
- Correction: Fixing erroneous conclusions in notes against the raw records and flagging uncertain items.
- Summarization: Compressing recurring patterns into rules or checklists.
- Maintenance: Deduplicating, merging conflicts, and retiring outdated information.
- Restructuring: Organizing notes into searchable structures (indexes, tags, entity relationships) as needed by tasks.
Illustrative pseudocode:
def dreaming(records, notes, wiki, injected):for r in records:candidates = extract_claims(r) # Extract candidate factsfor c in candidates:if not supported_by_evidence(c, r): # Evidence checkcontinueupsert(notes, c) # Write or update notesconflicts = detect_conflicts(notes)resolve_by_recency_and_evidence(conflicts) # Correction and recency arbitrationwiki.rebuild_index(notes) # Restructure into a searchable forminjected.set(pick_stable(notes)) # Only promote stable items
4. Key Finding: Writing More Does Not Mean Better Results
Evaluations across general scenarios—such as long-horizon conversation benchmarks, gamified tasks, question answering over massive internal documents, and legal document analysis—repeatedly show that the amount of memory is not positively correlated with task performance. Effective memory must satisfy four conditions:
- Filter for important information: Keep only conclusions that add incremental value to future decisions.
- Preserve evidence: Every memory must be traceable to a raw record to avoid “hallucination accumulation.”
- Correct promptly: When new evidence appears, it can override old conclusions, with the reason for the change recorded.
- Keep the structure searchable: Organize by task dimension rather than stacking entries in chronological order.
Offline consolidation can improve performance but consumes additional compute. A knowledge representation excels at cross-document reasoning but is not necessarily better than direct search for precise retrieval within a single document. A “cost–benefit” tiering is therefore needed: high-frequency, stable information goes into the injection layer, while long-tail material stays at the bottom layer for on-demand retrieval.
5. Implementation Guidance and Boundaries
- Define the boundaries: Memory is not the same as updating model weights, nor is it in-session context learning or history-summary compression. It is external context persisted across sessions.
- Storage choices: Raw records can use object storage or a logging service, indexes can use a retrieval service, and notes can be saved as files or structured records, with monitoring and alerting to observe the health of consolidation tasks.
- Permissions and compliance: Memory may contain sensitive information, so masking, access control, and retention-period policies are required.
- Evaluation loop: Use a quantifiable task set to validate the “injection length–hit rate–cost” curve and continuously adjust the promotion threshold.
In summary, the core of an agent memory system is not “remembering everything,” but building a layered, correctable, searchable, and cost-controlled consolidation pipeline that turns every experience into a reliable starting point for the next task.
