Hierarchical Practices for AI Agent Memory and Document Architecture
This article explains how to restructure an AI agent's execution architecture around three core layers—memory, documents, and skills—instead of treating them as add-ons. It covers extending a conventional three-tier memory system into a four-stage pipeline, setting a master document as the top constraint layer above skill creation, organizing skills into a dynamic pyramid with promotion and demotion thresholds, and defining behavior boundaries with a permitted/forbidden table. A four-step rollout plan and three metrics for evaluating context usage, skill hit rate, and first-pass task success are also included.
1. Starting Point: From “External Assistant” to “Autonomous Execution”
When many teams first build agents, they tend to call a newly introduced execution framework as a plugin alongside existing tools: the main workflow is still driven by the original coding assistant, while the new framework only handles local retrieval or summarization. This “primary-and-auxiliary” structure is efficient during validation, but once task chains grow longer and cross-project reuse increases, problems surface all at once: context is crowded out by large volumes of raw conversation, similar operations are re-explored from scratch each time, output formats drift, and after a failure there is no clear way to roll back.
The real turning point is not swapping out a single component but rebuilding the execution architecture itself—turning memory, documents, and skills from “attachments” into the “skeleton.” This article discusses that rebuild: how to keep an agent usable over the long term within a limited context budget.
2. Memory System: Integrating Three Layers into Four
2.1 Limits of the Native Three-Layer Structure
Common agent memory implementations roughly fall into three layers: a short-term session buffer, mid-term summaries, and long-term vector retrieval. The typical weakness is “remembering a lot but not remembering accurately”: retrieval returns many semantically similar fragments but lacks any abstraction of task success or failure, so the next similar task still starts reasoning from zero.
2.2 The Fourth Layer: Coupling External Memory with Core Documents
Adding a localized memory plugin (connected through a standard interface such as the Model Context Protocol) is only the first step. What produces a qualitative change is linking it with the core documents to form a four-stage pipeline:
Conversation clustering → Execution distillation → Deep understanding (LLM) → Skills
- Clustering layer: split sessions by task intent rather than by time, avoiding mixed topics;
- Distillation layer: keep only the three-part skeleton of “what was done, why it was done that way, and what the result was,” discarding pleasantries and redundant reasoning;
- Understanding layer: the model performs causal induction on the distilled output, annotating applicable preconditions and failure conditions;
- Skill layer: turn repeatedly validated practices into callable skills, writing back into the memory index.
This yields a dual loop between memory and skills: skill invocation produces new memory, and accumulated memory gives rise to new skills. In engineering terms, it is advisable to assign each memory entry a confidence score and a decay factor, letting low-confidence entries participate in prompts but not in decisions; also keep the constraint that “every memory write must be traceable to a specific execution record,” which prevents the model from fabricating experience on its own.
3. Core Documents: A Constraint Layer Ahead of Skills
A counterintuitive but important judgment is that in the execution architecture, core documents take priority over skill creation. Skills solve “whether something can be done”; documents solve “whether it should be done, and under what rules.” When constraints are missing, the model improvises in ways that look reasonable—which is exactly the root cause of unstable output.
3.1 The Primary Document: A Global Hub
It is recommended to maintain one master document that links the others together, covering at least: the execution workflow and phase breakdown, skill orchestration and invocation priority, a unified reporting format, an explanation of the memory system architecture, fault tolerance and rollback mechanisms, and a list of prohibited actions. It should be short and decidable, written as hard rules of “must / must not” rather than as vision statements.
# Execution Rules (excerpt)- Before any write operation, first declare the target file and the expected scope of impact- Stop retrying after two consecutive failures on a single task; output a failure summary and the paths already attempted- Report in a fixed three parts: conclusion / evidence / items to confirm
3.2 The Skill Pyramid: A Dynamic View, Not Static Labels
Skills are layered by invocation frequency and value, forming a pyramid:
- Foundational layer (~55%): file operations, terminal commands, web access—used daily, requiring stability;
- General layer (~30%): troubleshooting and diagnosis, planning, code review—reused across projects;
- Specialized layer (~15%): data modeling, creative design, code hosting platform operations—triggered in specific scenarios;
- Experimental layer (~5%): draft skills and new attempts, where failure is allowed.
The key is that this is a dynamic analytical view: promotion must satisfy quantifiable conditions—for example, an experimental-layer entry that is invoked effectively three times within 30 days with a positive retrospective evaluation can move up to the specialized layer; a specialized-layer entry reused across three different projects and generalizable without modification can move up to the general layer. Demotion needs rules too: two consecutive cycles with zero invocations drop the entry back to the experimental layer or into the archive, preventing skill library bloat from slowing retrieval.
3.3 The Yin-Yang Table: Drawing Behavior Boundaries with Set Thinking
Drawing on the complementary view of traditional philosophy and on set theory, you can build a “permitted / not permitted” table for each type of capability: the left side is the set of allowed actions, the right side is the set of forbidden actions, the intersection is empty, and the union covers the full behavior space of that capability. This expression is easier for a model to parse than natural-language description, and it is also convenient to check item by item during review. When flexibility is needed, a third column, “needs confirmation,” can hand risky decisions explicitly back to a human.
4. Rollout Path and Metrics
A four-step approach is recommended: first write the master document and freeze the reporting format; then connect the external memory capability to complete the four-layer pipeline; next inventory existing practices, layer them according to the pyramid, and set promotion and demotion thresholds; finally fill in the yin-yang table and the fault-tolerance strategy.
For metrics, focus on three: context occupancy rate (the share of tokens taken by memory and documents in the prompt), skill hit rate and reuse span, and first-pass task success rate. If context occupancy stays persistently high while the first-pass rate does not improve, memory distillation is insufficient; if the number of skills grows quickly but the hit rate declines, the promotion threshold is too loose.
When elastic compute and persistent storage are needed, a container platform can host the execution environment, object storage can hold memory snapshots, and logging services plus monitoring and alerting can track the metrics above. These are all general cloud product categories and do not imply dependence on any specific implementation.
5. Conclusion
The source of agent reliability is not a stronger single-point model but the architectural discipline of the three layers working together—memory, documents, and skills: memory converges experience, documents constrain behavior, and skills reuse proven capacity. Only by writing constraints clearly and making the layering a dynamic mechanism can an agent produce stable output over long-cycle tasks.
