Agent Memory: Evolution and Persistence Practices

    This article traces the evolution of memory in AI agents from single-session context windows to external retrieval and, most recently, persistent, self-improving systems. It breaks down the core traits of third-generation memory, a typical architecture built on structured text files and a three-layer design, and the self-improvement loop. It also covers key design concerns such as write thresholds, conflict resolution, forgetting, privacy, and observability, along with evaluation metrics and a phased adoption path. Readers will gain a practical understanding of how persistent memory turns one-off reasoning into a long-term, manageable asset.

    1. Why Agents Need Memory

    LLM-powered agents perform well within a single session, but once the session ends, the context window is cleared and the model’s awareness of user preferences, project background, and past decisions resets to zero. This leads to three common pain points:

    • Repeated communication overhead: users have to restate their tech stack, coding conventions, and business constraints every time.
    • Broken tasks: multi-stage tasks cannot inherit intermediate conclusions from earlier stages, so long workflows are forced into unrelated short conversations.
    • No accumulated experience: the same mistakes recur because the agent cannot distill rules from failures.

    The goal of memory technology is to turn “one-off reasoning” into a “capability that accumulates over time.” The industry typically divides this capability into three stages of evolution: the first generation relies on the single context window, with capacity limited by the model’s input length; the second introduces external retrieval, fetching relevant history on demand; the third generation (around 2026) pursues persistence and self-evolution, keeping memory across tasks and sessions over the long term and allowing it to self-correct during use.

    2. Core Characteristics of Third-Generation Memory

    Representative forms of third-generation solutions include an intelligent coding tool released by an open-source community and an agent framework open-sourced by a research organization. Their shared design orientation can be abstracted into four points:

    1. Persistence first. Memory no longer lives only in memory or conversation history but is written to a durable medium, remaining available after process restarts or service migrations.
    2. Structured and readable. Memory is stored in human-readable, version-controllable text or semi-structured formats, making it easy to review, correct, and roll back manually.
    3. Layered coordination. Context compression, indexed retrieval, and persistent files each play their role, forming a stable three-layer structure.
    4. A self-evolution loop. After completing a task, the agent writes successful experiences and failure lessons back as rules or preferences, so subsequent behavior gradually aligns with the user and the project.

    3. Typical Architecture Breakdown

    3.1 Structured Text Medium

    A common approach uses several convention-based files to hold memory at different granularities, for example:

    • Long-term memory file: records user identity, technical preferences, communication style, and taboos.
    • Project description file: records repository structure, build method, code conventions, and directory responsibilities.
    • Task log file: appends key decisions and unfinished items over time.

    Illustrative directory structure:

    1. agent_workspace/
    2. ├── MEMORY.md # Long-term preferences and stable facts
    3. ├── AGENTS.md # Project-level constraints and collaboration conventions
    4. ├── logs/
    5. │ └── 2026-03-11.md
    6. └── rules/
    7. └── review.md # Review rules distilled from failure cases

    The advantage of this design is transparency and control: memory is files, which can be placed under version control, edited manually, and audited through diff comparisons to detect drift in the agent’s behavior.

    3.2 Three-Layer Stacked Architecture

    A more engineering-oriented approach divides memory into three layers:

    • Context compression layer: summarizes, de-duplicates, and extracts key points from lengthy conversations and tool outputs to control the token volume entering the model.
    • Index layer: uses a local database to index memory fragments, supporting retrieval by time, tag, entity, and task type.
    • Persistent file layer: stores raw and aggregated memory as the ultimate source of truth.

    The relationship among the three layers is “compression saves, index finds, files store.” During retrieval, the index is queried first to locate candidate fragments, then the file layer is used to fetch the original text, which is finally trimmed to budget and injected into the prompt.

    3.3 Self-Improvement Loop

    The loop usually consists of four steps: execute the task, record the trajectory, reflect offline, and write rules back. The reflection stage can use a separate prompt template to extract “reusable conclusions” from the trajectory, which are then reviewed by a human or a policy engine before being written to the rules file, preventing flawed experience from polluting long-term memory.

    1. Task execution -> Trajectory logging -> Reflection -> Rule write-back -> Next execution

    4. Key Design Considerations

    • Write threshold: not every conversation is worth remembering. It is advisable to write only “stable facts, explicit preferences, content the user has corrected, and reusable conclusions” to the long-term layer, leaving the rest in the short-term layer.
    • Conflict resolution: when new memory contradicts old memory, a priority order must be defined (explicit user instruction > latest confirmation > historical inference), and change traces should be retained.
    • Forgetting mechanism: set expiration times and access frequency so cold data is archived or deleted, preventing memory bloat from slowing retrieval.
    • Privacy and permissions: desensitize sensitive information before writing to disk, and isolate memory spaces by project to avoid cross-tenant contamination.
    • Observability: log the fragments hit by each retrieval and the injected length, making it easier to analyze “why the agent behaved differently this time.”

    5. Evaluation and Adoption Recommendations

    Evaluating a memory system should not focus only on answer fluency but on three types of metrics: first, changes in task success rate and rework rate; second, context cost, i.e., average injected tokens and compression ratio; third, memory quality, including hit rate, conflict rate, and frequency of manual corrections.

    For adoption, it is recommended to start from a minimal loop: first implement structured file memory and basic retrieval, then introduce compression and indexing, and finally layer on reflection and write-back. If cloud resources are used, memory files can be stored in object storage, trajectory writes decoupled with a message queue, and the retrieval service hosted on a container platform, with log services and monitoring alerts used to observe the health of the memory pipeline. Baidu AI Cloud offers a relatively complete combination of such general capabilities, making it convenient for teams to build on demand rather than building all the infrastructure themselves.

    Overall, the essence of agent memory is “turning experience into a manageable asset.” Persistence solves storing it, the layered architecture solves finding it, and self-evolution solves getting more accurate with use; only by combining the three can an agent evolve from a tool into a long-term collaborator.