Dual-Mode Memory Architecture: Engineering Long-Term Context Beyond the Limits of Summarization
Summary: This article explores a dual-mode memory architecture that combines rolling summaries with file-based long-term memory to address the capacity limits, topic contamination, and governance gaps of summary-only approaches in long-cycle AI assistants. You will learn how to structure memory across session, summary, and file layers, build a write pipeline for filtering, extraction, normalization, and indexing, and retrieve only the most relevant entries through hybrid search. The article also covers versioning, auditing, rollback, and practical implementation recommendations.
1. Why a Single Summary Layer Is Not Enough
In long-running AI assistant systems, the context window is a hard constraint. The most common industry practice is to compress the conversation history into a single summary and prepend that summary to the prompt on the next turn. This approach is cheap to implement, but it has three structural weaknesses.
First, there is a capacity bottleneck. Summary length is usually tied to the prompt budget. When conversations span weeks and cover multiple topics, information is compressed repeatedly, and early details are lost layer by layer.
Second, summaries cause topic contamination. A summary is a single continuous block of text with no way to distinguish “coding preferences” from “travel preferences.” When answering a technical question, the model can be distracted by unrelated historical context, and its attention becomes diluted.
Third, summaries are difficult to govern. They are generated by the model, so users cannot inspect individual entries or precisely delete a specific incorrect memory. Correcting an error means regenerating the entire summary.
The core idea of dual-mode memory addresses exactly these three problems: keep the summary as “working memory,” and add a file-based layer as “long-term memory” that retrieves entries on demand instead of injecting everything.
2. Layered Design of the Dual-Mode Architecture
Memory can be divided into three layers with clear responsibilities:
- Session layer: Stores the raw context of the current task. It has a short lifecycle and is reclaimed when the session ends.
- Summary layer (classic memory): Applies rolling compression to recent dialogue to provide a low-latency “rough impression,” used to maintain coherent tone and short-term references.
- File layer (file memory): Stores facts, preferences, decisions, and agreements that are worth retaining long term as individual structured documents. It can grow indefinitely and recall entries only when needed.
A sample directory structure:
/memory/user_profilecoding_preferences.mdtravel_preferences.md/projectsproject_alpha_decisions.md/indexmanifest.json # entry metadata: path, tags, updated timestamp, summary sentence
The key point is that the file layer is not a “longer summary.” It is a set of minimal memory units that can be addressed and updated individually.
3. Write Path: From Dialogue to Structured Documents
Writing to memory does not mean dumping entire chat transcripts. It is a pipeline of filtering, extraction, normalization, and persistence.
- Candidate filtering: Determine whether the current turn contains information that remains valuable across sessions, such as long-term preferences, explicit agreements, or key conclusions. Casual chat and one-off context are discarded to prevent memory bloat.
- Information extraction: Break candidate content into atomic facts. One document should express one topic, avoiding a mix of multiple topics in a single file.
- Normalization: Standardize the field structure to support later retrieval and deduplication.
{"id": "mem_0421","type": "preference","topic": "coding","content": "Unit tests and static checks must be run before every commit","tags": ["quality", "workflow"],"confidence": 0.9,"updated_at": "2026-08-26T20:00:00Z"}
- Conflict resolution: When a new conclusion appears for the same topic, overwrite it using versioning rather than appending. Keep older versions for traceability.
- Index update: Write each entry’s summary sentence and tags into the index file, forming a lightweight “table of contents.”
4. Read Path: On-Demand Retrieval Instead of Full Injection
The read stage determines whether the system actually saves context. A hybrid retrieval strategy is recommended:
- Keyword/tag exact matching: Suitable for strong identifiers such as “tech stack” or “project code name.”
- Vector semantic recall: Suitable for vague phrasing, such as “I previously said I don’t like overly verbose responses.”
- Time decay and confidence ranking: Prioritize recent, high-confidence entries.
Pseudocode example:
def build_context(query, budget=1500):hits = []hits += keyword_match(query, limit=5)hits += vector_search(query, limit=5)hits = dedupe_and_rank(hits, decay=0.05)picked, used = [], 0for h in hits:if used + h.tokens > budget:breakpicked.append(h.render())used += h.tokensreturn "\n\n".join(picked)
From an engineering perspective, memory injection should have its own budget limit. It is also advisable to retain a fallback path of “inject no memory this turn” to prevent retrieval noise from undermining the main task.
5. Editable, Auditable, and Reversible
The biggest benefit of file-based memory is governability. Users can browse all memory entries like maintaining a personal knowledge base, edit wording line by line, correct facts, and delete entries directly. At the system level, this naturally provides three capabilities:
- Auditing: Every write includes a path and timestamp.
- Rollback: Versioned files can be restored to any historical state.
- Permission isolation: Memory directories for different projects and different users remain independent, preventing cross-contamination.
6. Common Pitfalls and Implementation Recommendations
- Pitfall 1: Writing every conversation into memory. This increases retrieval noise and cost. Follow the principle of “persist only high-value information.”
- Pitfall 2: Relying solely on semantic retrieval. Pure vector recall tends to drift on proper nouns. Keywords and tags must not be omitted.
- Pitfall 3: Ignoring privacy and compliance. Sensitive information should support one-click deletion and automatic expiration.
- Implementation recommendation: Start with a minimal viable version using the file system, then gradually introduce indexing and vector capabilities. In cloud environments, you can use object storage to hold memory files, message queues to decouple writes, and logging/monitoring services to track retrieval hit rates and injection costs. Baidu AI Cloud offers composable foundational components for these general capabilities, allowing teams to focus on the memory strategy itself.
7. Conclusion
The value of dual-mode memory lies not in “remembering more,” but in “retrieving more accurately.” Summaries ensure coherence; files ensure accuracy and governability. Together, they transform long-term memory from a slogan into an engineering system that is measurable, operable, and correctable.
