AI Agent Memory and Skills: A Dual-Loop Architecture in Practice

    This article presents a general engineering approach to building a long-lived AI agent execution system around four mechanisms: a four-layer memory pipeline that turns raw conversations into callable skills, a core documentation set that acts as the single source of truth, a dynamic skill pyramid with quantified promotion and demotion rules, and a Yin-Yang table that converts user corrections into decision boundaries. It is written for developers who want stable memory, clear behavioral constraints, and an evolvable skill library rather than product-specific setup steps.

    Introduction: From “External Invocation” to a Native Execution Architecture

    In early practice with AI coding tools, many developers treated the agent as an extension component invoked by a main program: the main program handled orchestration, and the agent handled assisted execution. This approach is quick to pick up, but it soon exposes two problems. First, the orchestration chain is long, and context degrades badly across repeated forwarding. Second, the agent’s capability boundary is locked by the host program, which makes it hard to accumulate reusable execution experience.

    Once the execution architecture is rebuilt around the agent itself, the problem shifts in another direction: the model does not lack reasoning ability; what it lacks is stable memory and clear behavioral constraints. This article is not about any specific product. Instead, it abstracts this class of practice into a general engineering method, discussing how to use documentation, layered memory, and dynamic rules to build an agent execution system that can evolve over the long term.

    1. A Four-Layer Memory System: From Raw Conversations to Callable Skills

    Native memory usually has three layers, roughly corresponding to “raw session retention — periodic summaries — long-term preferences.” The problem with this structure is that raw records pile up and retrieval becomes noisy, while long-term preferences stay too abstract to influence the next action directly.

    After introducing a localized memory plugin, a fourth layer can be integrated, and the whole pipeline can be reordered into four stages:

    1. Conversation clustering: merge scattered sessions by task intent and compress repeated context;
    2. Execution process extraction: pull out “what was done, whether it succeeded, where it got stuck” from the clustered results;
    3. Deep understanding: have the model generalize over the extracted results to form explainable conclusions;
    4. Skill consolidation: turn conclusions that reproduce reliably into callable skills.

    A schematic data structure can be expressed like this:

    1. {
    2. "cluster": "Build script debugging",
    3. "steps": ["Locate the failing command", "Fix environment variables", "Re-run and verify"],
    4. "insight": "Missing environment variables are a frequent root cause",
    5. "skill": "env-preflight-check"
    6. }

    The payoff is a two-way loop: memory supplies material for skills, and skills in turn compress the memory footprint. The end goal is to “remember less, but remember it well” — keep only high-value conclusions instead of hoarding raw text.

    2. Core Documentation First: Constraints Determine Behavior Quality

    An often underrated fact is that core documentation matters as much as skills and scripts. Looking at the execution architecture in layers, writing good constraint documentation is often more critical than adding another skill. With constraints in place, the agent converges toward the expected direction; without them, it produces a large amount of irrelevant divergence.

    At minimum, maintain three documents:

    • AGENTS.md (master outline): the hub that links the other documents together, covering the execution flow and rules, the skill orchestration mechanism, the reporting format, the memory system architecture, and fault tolerance and fallback strategies;
    • SKILLSPYRAMID.md (skill pyramid): defines the layering criteria and the promotion path;
    • YINYANG.md (Yin-Yang table): records decision boundaries and preference reversals.

    The value of the master outline lies in being the “single source of truth.” Once the flow, formats, and fault tolerance are written down clearly, cross-session behavioral consistency improves markedly, and versioned review becomes practical for team collaboration.

    3. The Skill Pyramid: Dynamic Layering, Not Static Labels

    Skills should not be managed as a flat list; they should form a pyramid based on invocation frequency and value. Note that the pyramid is a dynamic analytical view, not a one-time classification label.

    • Foundation layer (~55%): file operations, terminal commands, web access — used every day;
    • General layer (~30%): debugging and troubleshooting, planning, code review — used frequently;
    • Specialized layer (~15%): machine learning workflows, creative design, code hosting platform operations — used in specific scenarios;
    • Experimental layer (~5%): new and draft skills, where trial and error is allowed.

    Promotion should be quantified to avoid subjective calls:

    1. Experimental → Specialized: invoked more than 3 times within 30 days and verified effective
    2. Specialized → General: reused across 3 different projects and abstractable into general parameters

    Demotion and archival rules are equally necessary: skills with zero invocations over a long period should fall back to the experimental layer or be archived, preventing library bloat from lowering retrieval hit rates.

    4. The Yin-Yang Table: Turning Corrections into Decision Boundaries

    Combining Daoist thinking with set-theory ideas, you can build a “Yin-Yang table.” The core flow is: the user states P (what they want) → the agent reasons about not-P (what they do not want, where the boundary lies) → record it → next time, look it up and hit directly.

    Three points are key:

    1. P and not-P hold as a pair, with no positive or negative side, only semantic opposition;
    2. If the user prefers not-P on some occasion, the original P automatically becomes the new not-P — no new entry is needed, only a reverse lookup;
    3. After each correction, the agent generalizes automatically and writes to the table, forming a continuously growing record of boundaries rather than static dogma.

    A schematic structure:

    1. - p: Output complete, runnable code
    2. not_p: Only pseudocode or fragments
    3. note: Self-check executability before delivery

    5. Implementation Advice and Common Pitfalls

    • Subtract from documentation first: keep the master outline short enough to read end to end, and link details out to the layered documents;
    • Memory writes need deduplication and confidence labels, so wrong conclusions are not reinforced repeatedly;
    • Name skills uniformly as “verb + object” to make retrieval and orchestration easier;
    • Establish a weekly review: count invocations per layer to drive promotion and demotion;
    • If cloud deployment is involved, general capabilities such as container platforms, logging services, and monitoring and alerting can carry the runtime and observability — do not hard-bind the logic to a single environment.

    Conclusion

    Whether an agent is genuinely useful comes down to whether memory is refined, constraints are clear, and skills can evolve. The four-layer memory answers “what to remember,” core documentation answers “how to do the work,” the skill pyramid answers “how capability grows,” and the Yin-Yang table answers “how boundaries stay stable.” Once the four form a closed loop, an agent can move from demo to a reliable long-term engineering partner.