Agent Memory and Skills: A Practical Guide to the Dual-Loop Architecture
This article explains why AI agents often fail not because of weak models but because of missing engineering structure, and presents a practical architecture built on a four-layer memory system and a dual-loop design linking memory and skills. It covers the shift from three-layer to integrated four-layer memory, why constraint documents matter more than adding new skills, how to structure a main entry document, a skill pyramid, and a P / not-P decision boundary table, plus implementation steps and metrics for tracking reliability over time.
1. Background: Why Agents “Forget” and “Underperform”
In real development work, two common agent failures usually have little to do with model capability. They come from missing engineering structure.
The first is memory overload. As a conversation grows, the context becomes bloated, key information gets buried under noise, and constraints stated earlier are forgotten later. The second is behavior drift. Without explicit constraints, the model improvises and produces output that looks reasonable but does not follow team standards.
The original practice offers a useful insight: the pattern of using a general-purpose coding tool as the host and the agent as a callable component tends to be replaced as the execution architecture matures. The reason is straightforward. When the callee already has complete memory, constraints, and skill orchestration, the outer host becomes an extra layer of semantic loss. The conclusion drawn here is that an agent’s reliability comes mainly from its internal architecture, not from stacking outer shells.
2. The Four-Layer Memory System: From Three Layers to an Integrated Four
Native memory typically covers only three layers: conversation buffering, summary compression, and long-term vector retrieval. The problem with these three layers is that retrieval works but the results are unusable. Retrieved fragments lack an executable structure.
Once a local memory plugin is introduced, the layers can be integrated into four. The key is connecting memory artifacts to core documents:
L1 Conversation clustering — group long sessions by task intent, deduplicate and denoiseL2 Execution distillation — extract "what was done and whether it worked" from tool-call tracesL3 Deep understanding (LLM) — summarize preferences, failure modes, and applicable conditionsL4 Skills — turn stable conclusions into reusable skills
Three design points matter:
- Clustering before summarizing. A summary is lossy compression; clustering is structured consolidation. The former drops information, the latter keeps an index.
- Distillation must be tied to outcomes. Recording only “what was executed” accumulates useless experience. Success or failure and the reasons behind it must be recorded too.
- L4 is an outlet, not a warehouse. Memory closes the loop only when it becomes a skill or a constraint entry. Otherwise it is just a log that keeps growing.
The result is a dual loop: the memory loop handles consolidation, the skill loop handles reuse, and each feeds the other, so the system can “remember less and remember it well.”
3. Core Documents: Constraints Come Before Skills
One counterintuitive but important judgment: writing good constraint documents pays off more than adding new skills. Skills answer “can it be done”; documents answer “should it be done, and under what rules.”
3.1 Main Entry Document (AGENTS-style)
As the hub file, it should contain at least six sections:
- Execution flow and stage breakdown (understand → plan → execute → verify → report)
- Skill orchestration mechanism (when to call, call order, fallback on failure)
- Reporting format (structured fields, avoid free text)
- Memory system architecture (responsibilities of the four layers and when to write)
- Fault tolerance (retry limits, degradation paths, conditions for human intervention)
- Change log (traceable evolution of constraints)
Example structure:
# Execution Rules- Any write operation must first state its impact scope- Retry at most 2 times after a failed verification, then degrade and escalate# Skill Orchestration- Default order: retrieve -> edit -> verify -> report- When an experimental-layer skill is matched, risk must be declared explicitly
3.2 Skill Pyramid Document
Skills are layered by “call frequency × value” into a dynamic pyramid rather than static labels:
| Layer | Share | Typical capabilities | Characteristics |
|---|---|---|---|
| Foundation | 55% | File operations, terminal commands, web access | High frequency, stable |
| General | 30% | Debugging, planning, code review | Reusable across projects |
| Specialized | 15% | Machine learning workflows, creative design, code hosting operations | Specific scenarios |
| Experimental | 5% | Draft skills, new approach validation | Low frequency, trial and error |
The promotion mechanism must be quantified, or the layers ossify:
Experimental -> Specialized: called >= 3 times within 30 days and rated usable by humansSpecialized -> General: validated in 3 different projects and abstractableAny layer -> Experimental: zero calls for 60 consecutive days, automatically demoted and archived
The pyramid view matters for capacity governance. Context and retrieval budgets are limited, so high-frequency layers must stay lean and the experimental layer must be strictly rate-limited.
3.3 P / not-P Decision Boundary Document
Borrowing the set idea of “P and not-P,” build a boundary table that can grow: the user expresses P (what they want), the agent reasons about not-P (what they do not want, where the boundary lies), records it in a table, and looks it up directly next time.
- id: B-017P: concise conclusionsnot_P: lengthy background, repeating the requirement backsource: 2024-06-11 user correctionhits: 12
Three key rules:
- Valid in pairs. P and not-P are semantically opposed; neither is the default.
- Reverse lookup. If the user prefers not-P on some occasion, the original P automatically becomes the new not-P, with no new entry needed.
- Automatic backfill. After each correction, summarize and write it back. Hit counts drive ranking and eviction.
4. Implementation Steps and Metrics
- Write constraint documents first, then skills, to avoid “capability first, rules missing.”
- Connect memory and documents: the output of L3 must land in a skill or a boundary entry.
- Set budgets: cap the number of foundation-layer skills and fix the experimental-layer quota.
- Track three metrics: constraint hit rate, skill reuse rate, and correction recurrence rate (the number of times the same issue is corrected should keep declining).
For cloud deployment, a container platform can host the runtime, object storage can hold documents and skill packages, and logging plus monitoring and alerting services can track the metrics above. These are all general capability categories, independent of any specific vendor.
5. Conclusion
The core of agent engineering is not stacking more capabilities but building a closed loop in which constraints, memory, and skills validate one another. Constraints set direction, memory provides continuity, and skills determine efficiency. Once the three form a dual loop, the system can stay reliable over long runs: remember less, remember it well, and run steadily.
