Deep Agent Architecture Refactor: A Practice in Memory and Skill Dual Loops
A hands-on guide to restructuring an AI agent's native execution architecture. This article covers extending the memory system from three to four layers, building a skill pyramid with promotion rules, an AGENTS.md constitution, and a Yin-Yang decision table for self-evolving behavior boundaries.
Deep Agent Architecture Refactor: A Practice in Memory and Skill Dual Loops
In the early stages of deploying AI agents, many developers tend to use agents as auxiliary components attached to a host environment — for example, mounting an agent through a common toolchain such as MCP (Model Context Protocol). In this pattern, the external toolchain dominates, and the agent is merely a subordinate module that executes specific actions. However, as business complexity grows, this “master-slave” model reveals clear weaknesses: fragmented context, memory that cannot persist, and poor fault tolerance.
To solve these problems thoroughly, I began decoupling the agent from its external host environment and deeply refactoring its native execution architecture. This article breaks down the core ideas behind the refactor, focusing on the four-layer memory integration and the core documentation constraint system, and explores how to build a “memory + skill” dual-loop architecture that remembers less yet retains more.
Part 1: Memory System Refactor — From a Single Point to Four Layers
An agent’s memory system directly determines its behavioral consistency and capacity for improvement. A typical native memory system uses a simple three-layer structure, but in long-running tasks it tends to suffer from context bloat or loss of critical information. To break through this bottleneck, I introduced a local memory plugin mechanism that expands the architecture to four layers.
The new four-layer memory architecture is not a simple stacking of storage; it forms a pipeline for information refinement:
- Conversation clustering: Vectorize the massive volume of raw interaction records and perform cluster analysis to remove redundant information.
- Execution distillation: Extract key operation paths and results from the clustered records, using the large-model inference capabilities of Baidu AI Cloud for deep semantic compression.
- Deep understanding (LLM): The LLM performs causal analysis and intent induction on the distilled execution segments, turning “what happened” into “what it means.”
- Skill conversion: Solidify the results of deep understanding into reusable skills or rules, feeding them back into the agent’s capability library.
Through this loop, the system achieves a “memory + skill” dual loop. Information is no longer passively piled up in the context window; it is actively digested and converted into structured skills. When facing a new task, the agent no longer needs to trace back through lengthy conversation history — it simply invokes the distilled skills. This is what truly means “remember less, retain more.”
Part 2: Core Document Constraints — The Compass of the Execution Architecture
In an agent architecture, core documents matter no less than code scripts and skill libraries. From an architectural perspective, well-crafted constraint documents do more to determine agent behavior quality than blindly adding skills. Large language models are, by nature, probabilistic generation mechanisms; without rigorous document constraints, they easily hallucinate or drift off target in complex tasks. The role of core documents is to draw clear boundaries of action and decision logic for the agent.
1. The Central Hub: AGENTS.md
AGENTS.md is the “constitution” of the entire agent — the central hub that links all other documents and components. This document must clearly define the following key dimensions:
- Execution flow and rules: Define a standardized SOP from receiving instructions to producing output, including task decomposition and the order of tool calls.
- Skill orchestration: Specify trigger conditions and priorities for different skills to avoid conflicts.
- Reporting format: Enforce output structure (such as a JSON Schema) to ensure downstream systems can parse it.
- Fault tolerance: Define degradation and retry strategies for failed tool calls, network timeouts, or abnormal LLM output.
# AGENTS.md example snippet[Execution_Rule]1. Parse user intent; prioritize matching high-frequency skills in SKILLSPYRAMID.md.2. For irreversible operations (e.g., deleting files), a second confirmation must be triggered.3. Output must follow the {status: "success/error", data: {...}} structure.[Fault_Tolerance]- API Timeout: retry twice with a 5-second interval; if it still fails, log the error and return a degraded fallback message.
2. Dynamic Tiering: SKILLSPYRAMID.md
A skill library should not be a flat list but a dynamically growing pyramid. SKILLSPYRAMID.md tiers skills by call frequency and business value, providing a dynamic analytical perspective.
I divide skills into four tiers with explicit promotion and demotion rules:
- Base tier (55%): High-frequency, stable atomic capabilities such as file operations, terminal commands, and network requests, called routinely every day.
- General tier (30%): Cross-project skills such as code review, planning, and debugging.
- Specialist tier (15%): Domain-specific skills, such as fine-tuning machine learning models or operating on specific codebases, triggered only in particular scenarios.
- Experimental tier (5%): Newly developed draft skills still in validation.
To keep the system alive, I introduced quantified promotion mechanisms:
- Experimental tier → Specialist tier: called more than 3 times within 30 days with a success rate of 95% or higher.
- Specialist tier → General tier: successfully reused in 3 or more different business projects, with logic that generalizes.
This dynamic flow mechanism keeps the skill library from bloating and ensures the system stays focused on high-value capabilities.
3. Decision Evolution: YINYANG.md
For behavioral constraints, I combined Taoist Yin-Yang philosophy with mathematical set theory to build the YINYANG.md decision boundary table. Its core idea: given a user’s intent P (what they want), the system automatically derives non-P (boundary restrictions / what not to do) through reasoning, and records both together.
The Yin-Yang table is not a static set of dogmatic rules but a continuously growing decision record. P and non-P are semantically opposite yet equal in status. When a user corrects the system’s behavior during an interaction, the system automatically abstracts that correction into a new pair of P and non-P and writes it into the table. The next time a similar situation arises, a simple table lookup lets the system immediately understand the boundary and avoid repeating mistakes.
For example, when a user asks for “a concise piece of Python code” (P), the system may initially produce code with extensive comments. The user then says “no comments, just the logic.” The system records: P = “concise code,” non-P = “extensive comments.” If the user’s preference later changes and they ask for comments to be added, the system simply reverses the lookup — turning the original non-P into the new P — without adding redundant entries. This mechanism greatly reduces the cost of manually maintaining rules and gives the agent the ability to evolve its own boundary awareness.
Part 3: Conclusion
By refactoring the four-layer memory system and building a rigorous core document constraint system, the agent completed its transformation from an “external tool add-on” to a self-governing system. Memory clustering and distillation solve the problems of forgetting and context explosion, while the AGENTS hub, the skill pyramid, and the Yin-Yang decision table fundamentally curb the agent’s tendencies to ramble and lose control. This “memory + skill” dual-loop architecture not only improves execution efficiency but also provides a reusable engineering paradigm for building complex, enterprise-grade agents.
