AI Agent Memory and Skills: A Practical Guide to the Dual-Loop Architecture

    This guide explains how to build a dual-loop architecture for AI agents, connecting a four-layer memory system with a tiered skill system. It covers why generic agent entry points often fail to retain critical constraints, how to structure long-term memory from raw conversations into reusable skills, why core documentation acts as the highest-priority constraint layer, how to manage skills through dynamic tiering, and how to use a decision boundary table instead of growing rule lists. Readers will find practical steps, sample document structures, promotion and demotion criteria, and metrics for evaluating whether the architecture is working.

    1. Background: Why Agents “Forget” and “Get It Wrong”

    Many teams building AI agents go through a similar phase. They start with a general-purpose coding tool as the main entry point, mount their in-house execution components into it through an extension protocol, and let the main tool handle task dispatch while the custom components add capabilities. This approach is quick to set up, but it soon exposes two structural problems.

    First, the main entry point organizes context in a generic way that is not tailored to your business. It truncates, summarizes, and retrieves based on general logic, which dilutes the constraints that actually matter. Second, the capability boundaries of the custom components become coupled with the scheduling strategy of the main entry point. Any change to the execution architecture requires modifying both sides, and iteration costs rise quickly. The natural next step is to take the execution core back in-house and demote external tools to optional auxiliary channels.

    This article focuses on the two pieces that must be added after taking the core back: the memory system and the skill system, and how to stitch them together with core documentation.

    2. Four-Layer Memory Structure: From Raw Conversations to Reusable Skills

    Most built-in agent memory mechanisms have roughly three layers: session cache, summary compression, and vector retrieval. The common weakness is “remembering a lot but retaining little”: what gets retrieved are similar fragments, not executable conclusions.

    Adding a localized memory governance plugin on top can fill in a layer of “structured long-term memory.” A common industry practice is to deploy a multi-layer memory service that runs locally and exposes read and write interfaces through a standard protocol. This forms a four-layer chain:

    1. Conversation clustering: group raw interactions by task intent and remove small talk and repeated confirmations.
    2. Execution distillation: extract “what actions were taken and what results were hit” from the grouped records.
    3. Deep understanding: have the model perform causal induction on the distilled results and output experience entries that can be stated explicitly.
    4. Skill consolidation: when a type of experience recurs and the steps remain stable, solidify it into a skill.

    A sample data shape looks like this:

    1. {
    2. "cluster_id": "c_20240612_03",
    3. "intent": "Batch fix configuration file encoding",
    4. "actions": ["scan directory", "detect encoding", "batch convert", "validate"],
    5. "outcome": "success",
    6. "generalization": "For encoding issues, detect first and then batch process to avoid secondary corruption",
    7. "promote_to_skill": true
    8. }

    The key is the handoff from step 3 to step 4: memory is not only there to “answer more like a human,” but to “take fewer detours next time.” When distilled results recur stably, they are promoted to skills. When skills are called repeatedly, they generate new execution records that flow back into new memory. This is the dual loop of memory and skills.

    3. Core Documentation: The Highest-Priority Constraint Layer in the Execution Architecture

    A counterintuitive but important conclusion: at the architecture level, writing good core documentation matters more than adding new skills. Skills determine “what can be done”; documentation determines “what rules to follow.” When constraints are missing, the model improvises and produces a large amount of content that looks reasonable but is unusable.

    Maintain at least three documents with strictly separated responsibilities:

    • Master document: the hub of the entire system. It specifies the execution flow and phase division, skill orchestration and invocation priority, reporting format, memory layer structure, and failure fallback and retry strategy. Any new rule enters the master document first, and then it is decided whether to push it down.
    • Skill tiering document: records tiering criteria, the proportion of each tier, and promotion and demotion conditions.
    • Decision boundary document: records the user’s explicit preferences and implicit boundaries.

    A sample fragment of the master document:

    1. ## Execution Flow
    2. 1. Parse intent -> 2. Retrieve memory -> 3. Select skill -> 4. Execute -> 5. Validate -> 6. Report
    3. ## Orchestration Rules
    4. - Prioritize specialized-tier skills; fall back to the general tier if no match
    5. - No more than 3 skill invocations for the same task; explain trade-offs if exceeded
    6. ## Reporting Format
    7. Conclusion / Change list / Validation method / Open risks

    4. Skill Pyramid: A Dynamic Analysis View, Not Static Labels

    The most common mistake in skill governance is classifying once and never maintaining it. A more reasonable approach is to treat tiering as the result of dynamic analysis: recalculate placement based on recent invocation frequency and actual value.

    • Basic tier (about 55%): high-frequency, stable capabilities such as file read and write, command execution, and network access.
    • General tier (about 30%): cross-scenario capabilities such as troubleshooting, planning, and code review.
    • Specialized tier (about 15%): scenario-specific capabilities such as data modeling, creative generation, and operations on specific platforms.
    • Experimental tier (about 5%): draft skills and newly validated ideas.

    Promotion conditions should be decidable. For example, an experimental-tier skill that is effectively invoked more than 3 times within 30 days with adopted results can be promoted to the specialized tier. A specialized-tier skill reused across more than 3 different projects without modification can be promoted to the general tier. Reverse rules are equally necessary: skills with zero invocations for two consecutive cycles are demoted or archived, preventing skill library bloat from slowing down selection.

    5. Decision Boundary Table: Semantic Opposition Sets Instead of Piled-Up Rules

    The traditional approach keeps appending “do not do this” entries, resulting in a rule table that grows longer and conflicts with itself. A set-based expression can be introduced: the user gives a positive requirement P, and the system derives its boundary (non-P) at the same time, recording them as a pair. The two are only semantically opposed, with no positive or negative distinction. If a user preference falls on the non-P side, simply swap the direction of P and non-P without adding new entries.

    1. P: Output must include complete runnable code
    2. non-P: Do not give only ideas and pseudocode
    3. -- after the user once prefers "see the plan first and then decide", it automatically flips to --
    4. P: Give the plan and trade-off analysis first
    5. non-P: Do not directly throw out large blocks of implementation

    The benefit of this structure is a constant record size, the shortest query path, and every correction is automatically absorbed as the default boundary for the next time.

    6. Rollout Order and Metrics

    A four-step rollout is recommended: first write the master document and freeze the flow; then connect the local memory service to complete the four-layer chain; next organize skill tiering and promotion and demotion rules; finally introduce the decision boundary table and keep flipping and maintaining it in real tasks.

    For metrics, focus on three indicators: average skill invocations per task (should decrease), repeated mistake rate (should decrease), and cross-project skill reuse rate without modification (should increase). If these three do not improve, it is usually not a model capability problem, but rather that the document constraints or tiering rules are still vague.