Building Efficient AI Agents: A Four-Layer Memory System and Core Documentation Design
This article explains how to move AI agents from tool invocation within existing orchestration frameworks to a self-built execution architecture. It presents a four-layer memory system—conversation clustering, execution distillation, deep understanding, and skills—and describes the double loop between memory and skills. It then outlines three core documents: AGENTS.md for global execution rules, SKILLS.md for a dynamic skills pyramid, and YINYANG.md for recording preference boundaries. Practical recommendations cover documentation-first design, memory compression, feedback loops, and periodic skills review.
1. From Tool Invocation to Architectural Autonomy
In practice, many developers building AI agents follow a similar path. In the early stage, they rely on an existing orchestration framework or AI coding tool and treat the agent as one functional component invoked within it. As their understanding of the execution chain deepens, they gradually find that the framework’s built-in scheduling, memory, and constraint capabilities cannot meet customized requirements. They then move toward building their own execution architecture.
The essence of this shift is moving from “running your own tasks through someone else’s workflow” to “driving model behavior with your own rules.” Whether a self-built architecture succeeds depends on two things: how the memory system is designed and how the core documentation is written. These are discussed below.
2. Four-Layer Memory System: Remember Less, Remember Well
The memory challenge for agents comes from a contradiction. If the agent remembers too much, the context is flooded with noise, reasoning quality drops, and costs rise. If it remembers too little, it loses key preferences and experience. A common industry approach is layered memory. The typical three-layer structure consists of raw conversation records, session summaries, and a long-term fact base. On this basis, the system can be further extended into a four-layer architecture:
- Conversation record clustering layer: Cluster and merge historical sessions by topic, organizing fragmented interactions into topic-based record blocks and reducing noise during linear retrieval.
- Execution distillation layer: Record not only “what was said” but also “what was accomplished, where the process got stuck, and how it was finally resolved,” forming reusable execution experience.
- Deep understanding layer: Use an LLM to reprocess the first two layers, abstract the user’s real intent, stable preferences, and implicit constraints, and distill them into knowledge entries with high signal density.
- Skills layer: Solidify repeatedly validated solutions into skills—executable scripts or standard procedures—so that “remembered experience” directly becomes “reusable capability.”
These four layers form a double-loop system: memory is transformed into skills as experience is distilled into capability, and skills generate new memory during use as execution results flow back as experience. Information is continuously compressed and refined through the loop, moving toward the goal of “remembering less while remembering well.” In implementation, local memory plugins or a self-built vector store plus summarization pipeline can be used. The key is that each layer must have clear compression rules and eviction mechanisms to prevent low-value information from propagating into lower layers.
3. Core Documentation: Constraints as Direction
If memory determines what an agent knows, core documentation determines how it does things. From the perspective of execution architecture, writing core documentation well is no less important than—and sometimes even more important than—creating skills. With proper constraints, the model moves in the direction you expect. Without them, it will improvise and produce plausible-sounding output that drifts off target. The following three documents form a minimal set validated in practice.
3.1 AGENTS.md: The Global Hub Document
This is the primary document, responsible for connecting all other documents. It should clearly define:
- Execution flow and rules: The standard path from task receipt, planning, and execution to reporting, along with the entry and exit conditions for each step.
- Skill orchestration mechanism: Which types of skills are invoked in which scenarios, and how skills coordinate and degrade.
- Reporting format: A unified output structure that is easy for humans to review and machines to parse.
- Memory system architecture: The read/write timing and update rules for each of the four memory layers.
- Fault tolerance mechanism: How to retry when execution fails, when to stop, and how to clarify with the user.
The principle for this document is “explicit over comprehensive.” Rather than listing every possibility, define the behavior on critical paths explicitly, then provide fallback strategies for cases outside those boundaries.
3.2 SKILLS.md: Dynamic Layered Skill Governance
Organize skills into a pyramid based on invocation frequency and value, with high-frequency stable skills at the bottom and low-frequency experimental skills at the top. A reference distribution is:
- Base layer (about 55%): File operations, terminal commands, and web access—used every day.
- General layer (about 30%): Problem debugging, plan creation, and code review—used frequently.
- Specialized layer (about 15%): Machine learning, creative design, and code hosting platform operations—used in specific scenarios.
- Experimental layer (about 5%): New or draft skills—used in small-scale trials.
It is important to emphasize that the pyramid is a dynamic analytical view, not a static classification label. It must be paired with upgrade and downgrade mechanisms, for example:
- Experimental layer to specialized layer: Invoked more than 3 times within 30 days and manually confirmed to be reliable.
- Specialized layer to general layer: Validated in 3 different projects and with logic that can be generalized.
- The reverse applies as well. Skills with long-term zero invocations or high failure rates should be downgraded or archived to keep the system renewing itself.
3.3 YINYANG.md: Letting the Agent Remember Your Preference Boundaries
This is a thoughtful document that combines unity-of-opposites thinking with set theory. The core mechanism is:
- The user provides P (what they want), and the agent infers not-P (what they do not want and where the boundaries are).
- P and not-P are recorded in pairs and define each other.
- If a user preference reverses at some point, no new entry is needed; a reverse lookup is sufficient.
For example, if the user says “keep answers concise” (P), the agent should infer “do not use lengthy setup, do not repeat known information” (not-P) and record both in the table. The next time a similar request appears, it checks the table before answering. In essence, this is a continuously growing record of decision boundaries, not static philosophical dogma. Each time the user corrects the agent, the agent should summarize the correction and write it into the table so that the same mistake is not made twice.
4. Implementation Recommendations
- Documentation before skills: Establish the processes and constraints in AGENTS.md first, then gradually fill the skill library. Otherwise, the more skills there are, the more chaotic the system becomes.
- Memory must be compressed: Set capacity limits and decay rules for each memory layer. Prefer less over more.
- Build a feedback loop: Every user correction is a high-value training signal and should be captured in the Yin-Yang table and memory layers.
- Review the pyramid regularly: Review skill invocation data monthly, execute upgrades and downgrades, and let the system evolve with requirements.
Whether an AI agent is useful never depends on a single tool. It depends on whether you have built clear rules, refined memory, and an evolvable skill system for it. Do these three things well, and an agent on any foundation can move steadily in the direction you want.
