Agent Memory and Core Document Architecture: A Practical Guide

    This guide explains how to move an AI agent from an external add-on to a self-contained execution system by redesigning its memory and core documents. It covers a four-layer memory pipeline, the dual memory and skill loops, and three core documents: the hub execution contract, the dynamic skill pyramid, and the Yin-Yang boundary table. It also shares practical rollout advice and common pitfalls for building agents that remember less but retain more, stay controllable, and keep evolving.

    1. Starting Point: From an External Add-On to a Self-Contained Execution System

    Many teams initially call an agent as an extension of a main coding assistant: the main assistant handles interaction and orchestration, while the agent handles local tasks. This structure works well in demos, but problems surface quickly in real scenarios with long cycles and multiple parallel projects: the longer the context, the less stable it becomes; slightly vague instructions produce large amounts of irrelevant output; the same errors recur; and experience never turns into reusable capability.

    The root conflict comes down to two points. First, memory and execution are disconnected, so the agent remembers things but cannot use them. Second, constraints are missing, so the model can only improvise. In practice, I gradually shifted the focus from the external main assistant to the agent’s own execution architecture, with two core changes: rebuilding the memory system and strengthening core documents. Each is described below.

    2. Memory System: Four-Layer Integration and Dual Loops

    1. From Three Layers to Four

    Native memory usually has three layers. I added a localized memory plugin in front of it (using a common local memory service pattern, read and written through a standardized interface, with no proprietary product involved), forming a four-layer pipeline:

    1. Conversation clustering → Execution distillation → Deep understanding (LLM) → Skills
    • Layer 1, conversation clustering: denoise, segment, and merge raw sessions by topic, compressing fragmented information into “episode cards.”
    • Layer 2, execution distillation: extract “what was done, success or failure, key parameters and pitfalls” from task traces, forming structured experience entries.
    • Layer 3, deep understanding: the model attributes and abstracts the experience entries, producing transferable conclusions rather than a running log.
    • Layer 4, skill consolidation: turn frequent and stable conclusions into skills for direct reuse later.

    2. The Dual Loop of Memory and Skills

    The real payoff comes from two closed loops:

    • Memory loop: new conversation → clustering → distillation → understanding → write back to the memory store;
    • Skill loop: high-value conclusions in memory → generate or upgrade a skill → invoke it during execution → produce a new trace → flow back into memory.

    This achieves a key goal: remember less, and remember it well. Less, because only abstracted conclusions are kept rather than full text; well, because each conclusion has a source, an attribution, and can be consolidated into reusable skills.

    A schematic structure is shown below (it only expresses the layering idea, not a specific interface):

    1. memory:
    2. raw_session: short-term, rolling eviction
    3. clustered_episode: mid-term, aggregated by topic
    4. distilled_lesson: long-term, with attributed conclusions
    5. skills:
    6. source: distilled_lesson
    7. promotion_rule: frequency + cross-project reuse

    3. Core Documents: Constraints Before Capability Stacking

    A counterintuitive but important judgment is that, at the execution architecture level, writing good core documents matters more than creating skills. With constraints in place, behavior converges; without them, the model will make things up. I split the core documents into three, each with its own role.

    1. Hub Document: The Global Execution Contract

    The hub document is the primary document that ties all other documents together. It must clearly define the execution flow and rules, the skill orchestration mechanism, the reporting format, the memory system architecture, and the fault tolerance and fallback mechanism. It acts as the system’s “constitution”: no skill or memory may conflict with it. Use fixed sections, for example:

    1. # Execution Contract
    2. - Flow: understand → plan → execute → self-check → report
    3. - Orchestration: general layer first, then specialized layer; experimental layer requires explicit authorization
    4. - Reporting: conclusion first + evidence + risks + next steps
    5. - Fault tolerance: fall back and request clarification after two failures

    2. Skill Pyramid: Dynamic Layering, Not Static Labels

    Skills form a pyramid by “invocation frequency × value.” Note that this is a dynamic analytical view, not a one-time classification:

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

    A promotion mechanism keeps things flowing:

    • Experimental → specialized: used 3 times within 30 days with good subjective evaluation;
    • Specialized → general: validated in 3 different projects and generalizable.

    The value is controlling cognitive load: high-frequency skills stay minimal and stable, while low-frequency skills do not interfere with the main path.

    3. Yin-Yang Table: A Growing Decision Boundary

    Combining Daoist thinking with set theory, I built a “Yin-Yang table” to record boundaries. The core logic is: the user gives P (what they want), the model reasons about non-P (what they do not want and the boundaries), records it in a table, and looks it up next time to understand instantly. It is not a philosophical dogma but a continuously growing decision boundary library:

    Scenario P (want) non-P (do not want / boundary)
    Output style Conclusion first No long preamble
    Code changes Minimal changes Do not refactor unrelated modules
    Dependency introduction Standard library first Do not add heavy dependencies

    4. Rollout Advice and Common Pitfalls

    • Documents before skills: stacking skills without a contract only amplifies chaos;
    • Memory must be traceable: keep the source entry for each long-term conclusion to make correction easier;
    • Layering must flow: run promotions and demotions regularly to avoid a rigid pyramid;
    • Fault tolerance must be explicit: define the retry limit and the conditions that trigger clarification;
    • Reporting must be unified: a fixed format significantly reduces communication cost.

    5. Conclusion

    Whether an agent is useful does not depend on how strong any single capability is, but on whether memory forms a closed loop and whether documents form constraints. Get the four-layer memory and dual loops running, and put the three core documents in place (the hub document, the skill pyramid, and the Yin-Yang table), and you get an execution system that remembers less but remembers it well, stays controllable, and keeps evolving. To host the memory store and logs, you can use general-purpose capabilities such as object storage, logging services, and monitoring and alerting, keeping the architecture neutral and portable.