Agent Memory and Skills: A Practical Guide to the Dual-Loop Architecture
This guide explains how to build a native execution agent by pairing a four-layer memory system with a dynamic skill pyramid. It covers the shift from plugin-style assistance to an agent as the primary executor, the role of constraint documents, skill tiering and promotion rules, and a P / not-P decision boundary table that turns user corrections into reusable governance. Readers will find practical structures, YAML and Markdown examples, and implementation advice for keeping memory retrieval efficient and skills evolving within controlled boundaries.
1. Background: From “Bolt-On Assistance” to a Native Execution Body
In early practice, many teams connected an agent to an existing AI coding tool or runtime platform and let it provide assistance as a plugin or component. This approach is quick to set up, but it soon exposes three structural problems. First, the main scheduling authority stays with the external platform, so the agent’s execution path is fragmented and hard to optimize end to end. Second, memory and skills are scattered across different systems, so context cannot form a closed loop. Third, constraints live only inside prompts, with no durable, versionable artifact for governance.
Once the execution architecture becomes more cohesive, a more reasonable form is to treat the agent itself as the primary execution body and let external tools degrade into optional capability providers. The core of this article is exactly how the memory system and the skill system cooperate under this “native execution body” architecture.
2. The Four-Layer Memory System: From Dialog Clustering to Skill Distillation
A native memory usually has only three layers: raw dialog records, session-level summaries, and long-term preferences. The problem is that it “remembers a lot but not firmly”: the noise ratio is high, retrieval hit rate is low, and experience cannot be turned into reusable capability. Adding a local memory plugin fills in the fourth layer and forms this chain:
- Dialog record clustering: aggregate by task intent, time window, and project identifier, removing small talk and repeated confirmations.
- Execution process distillation: abstract “what was done” into steps, preconditions, and failure points.
- Deep understanding: the model attributes the distilled results and outputs conclusions that are decidable and verifiable.
- Skills generation: turn stable conclusions into skill entries or script templates.
A schematic data structure can be expressed like this:
memory_layers:raw_dialog: {ttl: 7d, indexed: true}session_summary: {granularity: task}long_term_preference: {source: user_correction}distilled_skill: {trigger: intent_match, versioned: true}
The key benefit of this design is a compressed retrieval space: at execution time, query the fourth layer first and fall back to the third layer only on a miss, instead of stuffing the full history into the context.
3. Core Constraint Documents: The Skeleton of the Execution Architecture
Below skills sit constraint documents. Skills answer “do I know how to do it”; constraint documents answer “by what rules do I do it”. Maintain at least three:
- Main entry document: links all document relationships and states the execution flow, skill orchestration mechanism, reporting format, memory layering, and fault-tolerance strategy. It is the single authoritative entry, and no other document may conflict with it.
- Skill tiering document: describes skills from a dynamic perspective, by invocation frequency and value distribution, rather than static labels.
- Decision boundary document: records the user’s positive demands P and negative boundaries not-P, forming a lookupable structure.
The main entry document can contain a pseudo-structure like this:
# Execution Flow1. Parse intent -> 2. Query decision boundaries -> 3. Select skill tier -> 4. Execute -> 5. Report -> 6. Write back to memory# Fault ToleranceDegrade after two failures: switch skill tier / narrow the scope / request confirmation
4. The Skill Pyramid: Dynamic Tiering and Promotion
Skills form a pyramid from “high-frequency stable” to “low-frequency experimental”. The ratios are only a starting point, not dogma:
- Base tier, about 55%: file operations, terminal commands, web access.
- General tier, about 30%: debugging and localization, planning, code review.
- Specialized tier, about 15%: data processing, creative design, code hosting platform operations.
- Experimental tier, about 5%: draft skills and validation of new approaches.
Make the promotion mechanism quantitative: for the experimental tier to enter the specialized tier, a skill must be invoked effectively 3 times within 30 days with positive human evaluation; for the specialized tier to enter the general tier, it must be reused across 3 different projects without changes to core logic. Demotion matters just as much: two consecutive cycles with zero hits push a skill back down, avoiding a bloated skill library that slows selection.
5. The Yin-Yang Table: Turning Corrections into Reusable Boundaries
Combining Taoist thinking with set theory, you can build a paired “P and not-P” table. The user gives a positive demand P, and the agent infers not-P as the boundary; once the user’s preference reverses, there is no need to add entries, just look up the table in reverse. Every time a correction happens, the system summarizes it automatically and writes it back to the table, forming a decision boundary that keeps growing.
P: output must include a complete runnable example not-P: do not give pseudocode onlyP: explain the impact scope before making changes not-P: do not silently modify configuration
6. Implementation Advice and Common Pitfalls
For implementation, do three things first: unify the main entry document, fix the memory retrieval order as “skill tier first”, and build an automatic write-back channel for every user correction. Common pitfalls include treating the tiering ratios as hard metrics, letting constraints scatter across prompts, letting skills only grow and never shrink, and treating memory as a full-log repository. If you need to carry large-scale memory and skill metadata, you can use the object storage, message queue, and logging services commonly offered by cloud vendors for persistence and auditing, keeping the architecture neutral and portable.
The final goal is only one: let the agent remember less but firmly, with clear constraints and evolvable skills, producing stable output within controlled boundaries.
