AI Agent Memory and Skills: A Dual-Loop Architecture in Practice
This article presents a vendor-neutral architecture for building AI agents around two coupled loops: a layered memory system and a skill pyramid. It explains why moving memory, constraints, and skills from external add-ons to first-class components matters, how to refine raw conversations into reusable skills, which core documents to maintain, how to govern skills through dynamic promotion and archival rules, and how to capture user corrections as a queryable boundary table. Readers will find practical document structures, promotion criteria, and metrics for evaluating whether the dual loop is working.
1. Background: From External Add-On to a Self-Contained Execution Entity
In the early stages of agent engineering, a common approach is to attach the agent to a general-purpose coding tool and expose memory and retrieval as plugins or tool calls. The master–subordinate relationship is clear: the host handles orchestration, and the agent only performs local reasoning. But as the execution chain grows longer and tasks shift from single-file edits to cross-module refactoring, the bottlenecks of this model surface quickly. The context window gets crowded out by raw conversation, historical information is “stored but unusable,” the number of skills expands without any calling order, and the user’s corrections for the same class of errors are repeatedly forgotten.
The turning point came from restructuring the execution architecture—promoting memory, constraints, and skills from “external components” to first-class citizens. This article abstracts away the specific implementation载体 and discusses a general, vendor-neutral architecture.
2. A Four-Layer Memory System: From Raw Records to Reusable Skills
The key to memory is not capacity but the level of abstraction. Raw conversations are the cheapest and noisiest information, and backfilling them directly into context only adds noise. A sound layering refines information step by step:
- Conversation clustering layer: Slice sessions by task intent and time window, remove small talk and ineffective back-and-forth, and keep goals, constraints, and conclusions.
- Execution trace distillation layer: Compress tool-call sequences into structured traces of “what was done—why it was done this way—what the result was,” forming searchable experience entries.
- Deep understanding layer: Have the model summarize the distilled results, extracting user preferences, project conventions, and failure patterns, and produce semantic summaries rather than verbatim excerpts.
- Skill consolidation layer: When a class of experience is repeatedly hit and the steps are stable, solidify it into an invokable skill, turning “memory” into “capability.”
The goal of this structure can be summarized in a few words: remember less, remember firmly. Retrieval prioritizes high-level abstractions and only drills down to raw records when necessary, controlling both context cost and recall quality at the same time.
Raw sessions → clustered slices → trace distillation → semantic summarization → skill consolidationlow signal-to-noise ratio high signal-to-noise ratio, low token usage
3. Core Documents: Constraints Before Capabilities
An often underestimated fact is that constraint documents matter as much as skills and scripts, and sit even higher in the architecture hierarchy. Capabilities decide “what can be done”; constraints decide “how it will be done.” An agent without constraints tends to produce output that looks reasonable but drifts from intent, showing up as over-engineering, unrequested scope expansion, and ignored existing conventions.
It is recommended to maintain at least three types of documents and make their reference relationships explicit:
- AGENTS.md (master outline): The single entry point that links the other documents. It covers the execution flow and phase breakdown, skill orchestration and calling priority, reporting format, the layered structure of the memory system, and failure fallback and fault-tolerance strategies. It should be short enough to serve only as an index and a set of hard rules, avoiding becoming a dumping ground for logs.
- SKILLSPYRAMID.md (skill governance): Defines skill layering and promotion/demotion rules, detailed in the next section.
- YINYANG.md (decision boundaries): Records paired constraints of “what is wanted” and “what is not wanted,” detailed in Section 5.
Three practical points for writing these documents: use decidable phrasing (“must/forbidden/prioritize” rather than “try to”); annotate each rule with its scope and activation conditions; and periodically clean up expired entries to avoid conflicting rules that cause behavior drift.
4. The Skill Pyramid: Layered Governance Through a Dynamic Lens
Skills should not be a flat list but a pyramid shaped by calling frequency and value. The layering here is the result of dynamic analysis, not a one-time static label:
- Foundational layer (~55%): high-frequency, stable capabilities such as file read/write, terminal commands, and web access;
- General layer (~30%): cross-scenario capabilities such as troubleshooting and localization, solution planning, and code review;
- Specialized layer (~15%): scenario-specific capabilities such as data modeling, creative design, and code hosting platform operations;
- Experimental layer (~5%): draft skills and newly validated ideas.
A supporting promotion mechanism keeps the layers fluid:
Experimental → Specialized: effectively invoked ≥3 times within 30 days, with positive human evaluationSpecialized → General: successfully reused across 3 different projects, and parameterized/generalizedAny layer → Archived: zero hits for two consecutive cycles, or already covered by a higher-layer capability
Three engineering points also need attention: skill descriptions must state “when not to use” to avoid false triggers; skills should avoid overlapping responsibilities, and overlapping parts should be lifted into shared dependencies; every promotion must synchronize the orchestration priority in the master outline to prevent documentation from drifting away from the implementation.
5. The Yin-Yang Table: Turning Corrections into Queryable Boundaries
When users express needs, they often give only a positive description P, while the real boundary hides in “not P.” Drawing on the ideas of semantic opposition and set complementarity, you can build a continuously growing boundary table:
User provides P (what is wanted)→ Agent derives not-P (what is not wanted, where the boundary is)→ Check the table before execution→ Write back after user correction, forming a new entry
The key design is paired and mutually inverse, with no inherent priority for either side: P and not-P are only semantically opposed, and neither side is naturally preferred. If the user explicitly prefers not-P on some occasion, the original P automatically becomes the new not-P; no new entry is needed, only a reverse lookup. This keeps the table from growing linearly with the number of corrections and keeps retrieval cost under control.
The write-back timing is best bound to the task wrap-up phase: the summarization layer judges whether a preference conflict occurred in this round, and if so generates candidate entries that are added to the store after user confirmation or automatically with high confidence, recording the source task for traceability.
6. The Dual-Loop Cycle and Implementation Advice
The memory loop handles “understanding better with use,” and the skill loop handles “getting stronger with use.” The two are coupled through the master outline document: memory summarization produces new skill candidates, and skill execution traces become new memory material. Implementation can proceed in four steps: first write the master outline to establish hard constraints, then connect the memory layering and open up the retrieval chain, then build skill layering and promotion/demotion reviews, and finally introduce the boundary table to automatically consolidate manual corrections.
Three metrics can measure the effect: the number of repeated same-class errors, the average number of rework rounds per task, and the context consumption per task. When all three decline together, the dual loop is truly working. If elastic scaling is needed, memory summaries and skill metadata can be placed in object storage, summarization tasks can be decoupled with a message queue, and log services and monitoring alerts can observe hit rates and drift.
