AI Agent Architecture: Four-Layer Memory and Core Documentation for Controllable Execution
Many developers adopting general-purpose AI agent frameworks encounter a common problem: the agent appears capable after installing many plugins and skills, but in practice it often drifts off course, answers the wrong question, repeats mistakes, and manages memory poorly. Based on a real architecture refactor, this article presents a practical approach built around two drivers: a memory system and core documentation. It explains how to move from framework-centric orchestration to a controllable self-built architecture, how a four-layer memory funnel supports a double loop between memory and skills, and how three core documents define execution behavior, skill layering, and user preference boundaries.
Many developers using general-purpose AI agent frameworks encounter a common problem: after installing many plugins and skills, the agent may appear capable, but in practice it often goes off track—answering the wrong question, repeating mistakes, and confusing its memory. Based on a real architecture refactor, this article summarizes a dual-driver optimization method based on a “memory system + core documentation” approach, with the hope of providing a useful reference for developers working on agents.
1. From Framework-Centric to Self-Built Architecture
When first using open-source agent frameworks, many people mount various capabilities as components onto the main framework: the framework handles scheduling, and the components handle execution. This model is quick to start with, but its problems are obvious:
- Black-box scheduling logic: the main flow is not under your control, making issues hard to locate.
- Separated memory and skills: valuable execution experience is not retained after a session ends.
- Unpredictable behavior: the same task may produce completely different results across two runs.
A more sustainable approach is therefore to take back the agent’s core execution control and build a lightweight but controllable architecture around “memory” and “document constraints.” Frameworks and plugins should serve only as capability providers, not as decision makers.
2. Four-Layer Memory System: “Remember Less, Remember Better”
Native agent memory usually has three layers: short-term context, session summaries, and long-term storage. This is sufficient for a single task, but knowledge evolution across tasks and weeks is lost. An improved approach introduces a fourth layer to form a complete information funnel:
Conversation record clustering → Execution process distillation → Deep understanding (LLM) → Skills (reusable capabilities)
The responsibilities of each layer are as follows:
- Conversation record clustering layer: cluster and archive raw conversations by topic and task type, avoiding indiscriminate full storage that creates retrieval noise.
- Execution process distillation layer: extract structured execution traces from “what was done, where it failed, and how it was fixed.”
- Deep understanding layer: use a large model to perform a second round of synthesis on the distilled results, forming conclusions that can transfer across tasks.
- Skills layer: turn repeatedly validated conclusions into executable skills or scripts for direct use next time.
The essence of this design is that memory and skills form a double loop: memory provides raw material for skills, and skill execution generates new memory. Information is compressed and abstracted layer by layer. The goal can be summarized as “remember less, remember better.” Storage volume decreases, while the reuse rate of key experience increases.
For engineering implementation, long-term memory can be handled with local memory plugins or vector databases, or placed in object storage with a retrieval service. The choice depends on data scale and privacy requirements; specific tools are not covered here.
3. Core Documentation: The Invisible Code That Matters More Than Skills
A counterintuitive but important conclusion is that writing core documentation well matters more than creating skills. Skills determine what an agent can do; documentation determines how the agent should do it. With sufficient constraints, it moves in the direction you expect. Without them, it improvises and produces nonsense.
In practice, the following three documents form the skeleton of the architecture.
1. AGENTS.md: The Global Execution Hub
This is the primary document, equivalent to the agent system’s “operations manual + organization chart.” It should clearly define:
- Execution processes and rules: how tasks are decomposed, in what order they execute, and when the agent must pause and ask.
- Skill orchestration mechanism: which tasks invoke which skills, along with dependencies and priorities among skills.
- Reporting format: a structured template for output so results are readable and auditable.
- Memory system architecture: rules for writing to and reading from the four-layer memory.
- Fault tolerance mechanism: retry strategies, fallback plans, and conditions for human intervention when execution fails.
This document is the indexing hub for all other documents. Maintaining it well gives agent behavior a baseline.
2. SKILLSPYRAMID.md: A Pyramid Structure for Skills
Skills should not be managed as a flat list. They should be layered by call frequency and value, forming a pyramid structure—stable and high-frequency at the bottom, low-frequency and experimental at the top:
| Layer | Share | Content | Usage scenario |
|---|---|---|---|
| Base layer | 55% | File operations, terminal commands, web access | Daily use |
| General layer | 30% | Problem debugging, planning, code review | Frequent use |
| Specialized layer | 15% | Machine learning, creative design, code hosting operations | Specific situations |
| Experimental layer | 5% | New skills, draft skills | Trial use |
Note that the pyramid is a dynamic analysis view, not a static classification label. Skills should flow between layers, so an upgrade mechanism is needed:
- Experimental layer → Specialized layer: called more than 3 times within 30 days and meets the actual effectiveness standard.
- Specialized layer → General layer: validated in 3 different projects and meets the conditions for generalization.
This mechanism prevents the skill library from expanding indefinitely: unqualified skills naturally sink and are eliminated, while excellent skills rise level by level and gain higher call weight.
3. YINYANG.md: Recording Decision Boundaries with a Yin-Yang Table
Drawing on Daoist thinking about the unity of opposites and ideas from set theory, a “Yin-Yang table” can be established for the agent to manage user preferences:
Core logic: the user gives P (what they want) → the AI infers not-P (what they do not want / where the boundary is) → the pair is recorded → the next query is understood immediately.
Several key design points:
- P and not-P form a pair. The two are only semantically opposed; there is no positive-versus-negative hierarchy.
- Preferences are reversible: if a user’s preference reverses on a certain occasion (toward the original not-P), no new entry is needed; a reverse query is sufficient.
- Continuous growth: the Yin-Yang table is an accumulating record of decision boundaries, not an unchanging doctrine.
The practical effect is that every correction you make to the agent is summarized into a pair of entries and added to the table, so similar errors can be avoided next time. This effectively turns “user tuning” into automated preference learning.
4. Conclusion
Building a useful AI agent is not mainly about piling up features. It is about self-constraint and experience accumulation at the architectural level:
- Take back execution leadership; let frameworks provide capabilities only.
- Use a four-layer memory funnel to achieve a double loop of “memory + skills.”
- Use three core documents—the execution hub, the skill pyramid, and the Yin-Yang decision table—to define behavioral boundaries.
- Keep all layers dynamically flowing, using usage data to drive upgrades and elimination.
This methodology does not depend on any specific tool. Its core idea is general: constraints determine the lower bound, and memory determines the upper bound. If your agent also often “does not listen,” start by rewriting the core documentation.
