Agent Memory and Core Document Architecture: A Practical Guide

    This guide explains how to fix two common AI agent problems: forgetting key agreements across sessions and drifting away from expected behavior. It presents a dual-loop architecture that connects layered memory with reusable skills, with core documents acting as the shared constraint and index layer. You will learn a four-layer memory model, the role of AGENTS.md as the central hub, a skill pyramid with promotion thresholds, and a decision-boundary table for recording corrections, plus rollout steps and metrics to track improvement.

    1. The Problem: Why Agents Forget and Drift

    Many engineering teams run into the same issues in practice. An agent performs reasonably well within a single session, but forgets key agreements once a new session starts. Others connect a memory component and then dump large volumes of low-value conversation into storage, which raises retrieval noise and lowers hit rate. The most common problem is behavior drift: without explicit constraints, the model improvises, so output style, reporting format, and execution flow differ every time.

    The original practice drew a clear conclusion: starting with one AI coding tool as the primary system and one agent framework as the secondary system is a common approach, but once the execution architecture is repeatedly reworked, that primary/secondary relationship can reverse. This article strips away specific products and frames the issue generically: how to adjust the architecture so an agent moves from “can chat” to “can work reliably.”

    Two conclusions stand out. First, more memory is not better; the goal is to remember less and remember it well. Second, core documents matter as much as skills and scripts, and at the execution-architecture level, writing good constraint documents is more critical than piling up skills.

    2. Overall Architecture: A Dual Loop of Memory and Skills

    Split the system into two loops.

    • Memory loop: cluster conversation records → distill execution traces → form deeper understanding (the LLM does the induction) → settle into reusable skills.
    • Skill loop: invoking a skill produces a new execution trace → the trace enters the memory loop and is distilled → this feeds back into skill layering and document constraints.

    The two loops meet at the core documents. Documents are both the source of constraints and the index layer between memory and skills. Without documents, memory is scattered logs; with documents, memory becomes executable rules.

    Illustrative directory structure (pseudocode, showing organization only):

    1. /agent
    2. AGENTS.md # Central hub: workflow, rules, orchestration, reporting, memory architecture, fault tolerance
    3. SKILLS_PYRAMID.md # Skill layering and promotion mechanism
    4. YINYANG.md # Decision boundary table (P and non-P)
    5. skills/ # Skill implementations
    6. memory/ # Layered memory storage

    3. Four-Layer Memory Integration: From Three Native Layers to a Retrieval-Ready System

    Native memory usually has three layers (for example, session, summary, and long-term). Once a local memory plugin capability is added on top, these can be integrated into four layers. The key is combining them with the core documents, not simply adding storage.

    Recommended responsibilities for the four layers:

    1. Raw conversation layer: stores complete traces, used only for auditing and traceback, not for high-frequency retrieval.
    2. Clustering layer: groups records by task, error, project, and other dimensions, removing duplicates and small talk.
    3. Distillation layer: compresses execution traces into structured entries shaped as “problem — action — result — deviation.”
    4. Understanding layer: the model generates “preferences, boundaries, failure modes” and writes them into the corresponding sections of YINYANG.md and AGENTS.md.

    The point: the first three layers solve “can it be found,” and the fourth solves “can it be used.” Retrieval should check the fourth layer first, then drill down to the third and second layers when hits are insufficient; the first layer is enabled only during troubleshooting. This significantly reduces context bloat.

    4. Core Document One: AGENTS.md as the Central Hub

    AGENTS.md is the primary document and is responsible for linking the others. It should always contain these sections:

    • Execution workflow: the order and gates for input confirmation, planning, execution, verification, and reporting.
    • Rules: language style, commit granularity, prohibited actions.
    • Skill orchestration mechanism: when to invoke which skill layer, and priority when conflicts arise.
    • Reporting format: a fixed template that avoids improvising every time.
    • Memory system architecture: the four layers’ responsibilities and read/write policy.
    • Fault tolerance: retry limits, rollback points, conditions for human intervention.

    Example snippet:

    1. ## Reporting Format
    2. 1. Conclusion
    3. 2. Changed files
    4. 3. Verification results
    5. 4. Risks and items to confirm
    6. ## Fault Tolerance
    7. - Stop and escalate after the same command fails twice in a row
    8. - For deletion/migration, provide a rollback plan first

    Keep documents short and firm: if it can be written as a checklist, do not write it as prose; if a threshold can be given, do not write “as appropriate.”

    5. Core Document Two: The Skill Pyramid and Dynamic Promotion

    Layer skills by “invocation frequency × value” into a pyramid rather than static labels. A practical ratio to start from:

    • Foundation layer, about 55%: high-frequency, stable capabilities such as file operations, terminal commands, and web access.
    • General layer, about 30%: cross-project capabilities such as debugging, planning, and code review.
    • Specialized layer, about 15%: domain-specific tasks such as data processing, creative design, and code hosting operations.
    • Experimental layer, about 5%: draft skills where instability is allowed.

    Example promotion mechanism:

    • Experimental → specialized: invoked at least 3 times within 30 days and rated “useful” by a human.
    • Specialized → general: validated across 3 different projects and abstractable into general steps.

    The pyramid is a dynamic analysis view: count invocations and failure rates weekly, demote skills that go unused for a long time, and avoid a bloated skill library that makes selection difficult.

    6. Core Document Three: The Yin-Yang Table and Decision Boundaries

    Combining the flow “the user states P (what they want) → the model infers non-P (what they do not want / the boundary) → record it → look it up next time” produces a decision boundary table that keeps growing. It is not a philosophical doctrine but a queryable set of constraints.

    Key rules:

    • P and non-P exist as a pair, opposite in meaning only, with no notion of positive or negative.
    • If the user prefers non-P on some occasion, the original P automatically becomes the new non-P; no new entry is needed, just query in reverse.
    • After each correction, summarize and write it back automatically, forming a loop that gets more accurate with use.

    Example:

    1. P: output includes full logs
    2. non-P: output omits key error stacks

    After the user corrects this with “do not omit the error stack,” the system marks “omitting the error stack” as non-P, and later retrieval hits it directly.

    7. Rollout Steps and Metrics

    1. Write the reporting format and fault tolerance sections of AGENTS.md first to immediately reduce output drift.
    2. Establish the read/write policy for the four memory layers and define retrieval priority.
    3. Inventory existing capabilities, layer them 55/30/15/5, and set promotion thresholds.
    4. Launch the yin-yang table and write back automatically after every correction.
    5. Review weekly: memory hit rate, skill invocation distribution, number of repeated corrections.

    When repeated corrections keep dropping and the first-pass success rate for cross-session tasks rises, the goal of “remember less, remember well” is met. Core documents, layered memory, and the skill pyramid together form an evolvable execution architecture, and general cloud product categories from Baidu AI Cloud (such as object storage, log services, and monitoring and alerting) can serve as general-purpose infrastructure options for hosting and observability.