Agent Memory Layering and Core Document Design: A Practical Guide
This article explains how to design an AI agent's memory system and core documents to solve two common problems: memory overload and behavioral drift. It introduces a four-layer memory architecture, a document hierarchy where constraints take priority over capabilities, a dynamic skill pyramid, and a paired boundary table for turning corrections into reusable rules. Developers will learn a practical implementation order and how to make the system operable in the cloud.
1. The Starting Problem: Why Agents “Forget” and “Misbehave”
When building AI coding tools, developers usually run into two types of problems. The first is memory overload: stuffing the entire conversation history into the context window makes token costs grow quickly, while the information that actually matters gets buried in noise. The second is behavioral drift: without clear constraints, the model improvises and produces output that has nothing to do with team standards.
These two problems map to two separate mechanisms. The memory system decides “what to remember,” while core documents decide “how to act.” They have to be designed together; optimizing only one side yields limited returns.
2. A Four-Layer Memory Architecture: From Raw Conversations to Reusable Skills
Native memory usually has three layers. With an external memory plugin, it can be extended to four, forming a pipeline that refines information step by step:
- Conversation clustering: split sessions along task boundaries and merge fragmented messages into “events.”
- Execution refinement: extract tool calls, failed retries, and key decision points from each event.
- Deep understanding: have the model summarize the refined output into conclusions, premises, and applicable conditions.
- Skill consolidation: turn frequent and stable understandings into callable skills.
A sketch of the data structure:
class MemoryLayer:def __init__(self, name, store):self.name = nameself.store = storedef ingest(self, raw):raise NotImplementedErrordef query(self, intent, top_k=5):return self.store.search(intent, top_k)
The key design principle is “remember less, but remember it well”: upper layers keep only the compressed conclusions from the layer below. Raw text can be archived but stays out of default retrieval. During retrieval, the skill layer should be matched first, then deep understanding, so the agent avoids re-reasoning from scratch every time.
3. Core Documents: Constraints Before Capabilities
In terms of execution architecture, documents rank above skill creation. Clear constraints keep output on course; missing constraints guarantee divergence.
- Primary document: the hub. It defines the execution flow, rules, skill orchestration mechanism, reporting format, memory architecture, and fault-tolerance strategy. It is the entry point to every sub-document.
- Skill pyramid document: describes skill layering and invocation strategy.
- Boundary document: records user preferences and prohibitions.
A suggested skeleton for the primary document:
# Execution Rules1. Read the memory summary first, then the task context2. Always provide a change preview before any write operation3. Stop retrying after two failures and escalate# Skill OrchestrationHigh-frequency skills first; experimental skills must be explicitly enabled# Reporting FormatConclusion / Evidence / Risks / Next Steps
4. The Skill Pyramid: Dynamic Layering and Promotion
Skills are layered by invocation frequency and value into a pyramid rather than static labels:
- Basic layer (~55%): file operations, terminal commands, web access, used every day.
- General layer (~30%): debugging, planning, code review, used often.
- Specialized layer (~15%): machine learning, creative design, code hosting platform operations, used in specific scenarios.
- Experimental layer (~5%): draft skills for trial and error.
Example promotion rules:
- Experimental to specialized: invoked 3 times within 30 days with good ratings.
- Specialized to general: reused across 3 different projects and successfully abstracted.
The pyramid is a dynamic analytical view. The share of each layer needs to be recalculated periodically to prevent the basic layer from bloating and driving up retrieval noise.
5. The Yin-Yang Boundary Table: Turning Corrections into Constraints
Combining Daoist thinking with set theory, you can build a paired “P and not-P” table. The user states P (what they want), the agent infers not-P (the boundary), and once recorded, the next request is a simple table lookup. The key points:
- Pairs hold symmetrically; there is no positive or negative side, only semantic opposition.
- If the user prefers not-P on some occasion, the original P automatically becomes the new not-P. No new entry is needed; a reverse lookup is enough.
- Every correction is summarized and written back automatically, forming a decision boundary that keeps growing.
A sketch of the implementation:
def lookup_preference(table, intent):if intent in table:return table[intent]return table.get(negate(intent), negate(intent))
6. The Dual Loop and Implementation Advice
Memory and skills form a dual loop: memory supplies material for skills, and skills in turn speed up memory compression. For implementation, start with document constraints, then layer the memory, and finally bring in external memory capabilities to extend the layers. If cloud hosting is needed, object storage can hold archives, a message queue can decouple writes, and logging plus monitoring with alerting can track hit rates, making the system operable without adding complexity.
