AI Agent Memory System Design: Closed-Loop Learning and Capacity Compression in Practice

    This article examines a closed-loop learning architecture for AI agent memory that combines long-term memory, reusable skill deposition, and periodic self-reflection. It explains how capacity-limited plain-text memory and frozen session snapshots improve determinism and reduce context bloat, how skills capture tool quirks and standard operating procedures for reuse, and how scheduled reflection triggers memory and skill updates. It also discusses production considerations for deploying the approach with lightweight state management and standard cloud infrastructure.

    In current large language model application practice, most agents fall into an “amnesia” state after a single session ends. Although the model itself has strong in-context understanding, once the session window closes or reaches its length limit, the previous interaction experience disappears. To overcome this limitation, the industry has explored an architecture based on an internal learning loop. The architecture is supported by three core subsystems: memory, skill deposition, and a reflection engine. Together, these form a complete cognitive loop that enables an agent to evolve continuously through long-term interaction.

    1. The Three Pillars of the Closed Loop

    To understand this loop, we can use an analogy based on an office assistant. The memory subsystem is like a notebook the assistant carries to record objective facts and preferences, such as “the boss prefers Americano coffee.” The skill deposition module is like an operations manual the assistant builds during daily work, for example, a standard operating procedure (SOP) learned through practice: “in a container cluster deployment, the second step must push the image first.” The reflection engine is like an alarm that goes off at specific times, reminding the assistant to review recent work and decide whether any lessons should be written into the notebook or the operations manual.

    These three subsystems complement one another: memory provides foundational facts, skills provide action guidance, and the reflection engine drives the entire system from passive response toward active learning.

    2. Memory Subsystem: High-Density Information Retention Under Capacity Limits

    In the overall agent architecture, the memory subsystem is designed with particular restraint and precision. Conventional agent memory schemes often rely on complex vector databases or unbounded append-only text files. The design discussed here takes the opposite approach: it uses only two plain-text files to carry all knowledge about the environment and the user.

    1. Plain Text and Structured Delimiters

    The memory system separates information into two categories: environment and project facts, and user preferences and understanding. The two files separate entries with a specific symbol, such as “§.” The advantage of the plain-text design is that it can be read directly and completely by the system prompt, without an additional retrieval step such as RAG. This ensures deterministic memory injection and low latency.

    2. Strict Capacity Limits and Information Compression

    The most important innovation of this design is the strict character limit on each file. For example, the environment facts file is limited to 2,200 characters, and the user understanding file is limited to 1,375 characters. This deliberate capacity constraint is essentially a mechanism that forces the agent to compress information.

    In a conventional unrestricted append model, such as some early agent memory schemes, the memory file keeps growing over time and may become a huge artifact tens of thousands of lines long after a few months. When the agent needs to find a piece of information from several weeks earlier, it can only read through the entire file, which consumes a large number of tokens, distracts attention, and may even trigger hallucinations. In contrast, a strict capacity limit forces the agent to evaluate and compress old information whenever it writes a new memory. Outdated, low-value information is naturally displaced, and what remains is high-density core facts. This selection mechanism keeps memory fresh and efficient.

    3. Parallel States of Real-Time Read/Write and Frozen Snapshots

    In the engineering implementation, to ensure stable system operation, the memory management module maintains two parallel states: a real-time writable list of entries and a snapshot frozen at the start of the session.

    1. class MemoryStore:
    2. def __init__(self, memory_char_limit=2200, user_char_limit=1375):
    3. # Real-time writable entry lists
    4. self.memory_entries: List[str] = []
    5. self.user_entries: List[str] = []
    6. # Capacity limit configuration
    7. self.memory_char_limit = memory_char_limit
    8. self.user_char_limit = user_char_limit
    9. # System prompt snapshot frozen at session start
    10. self._system_prompt_snapshot: Dict[str, str] = {
    11. "memory": "",
    12. "user": ""
    13. }

    The snapshot mechanism is introduced for logical consistency. During a single session, an agent’s behavior should be based on a fixed set of background knowledge. If memory were allowed to change dynamically in the middle of a session and take effect immediately, the agent could contradict itself within the same context. Therefore, the system loads a read-only snapshot as part of the system prompt when the session initializes, while newly generated memories first enter the real-time writable list and wait to be merged and compressed in the next session or when triggered by the reflection engine.

    3. Skill Deposition Module: From Single Execution to Reusable Experience

    If the memory subsystem is responsible for “what is,” the skill deposition module is responsible for “how to.” When an agent executes tasks in practice, it often encounters tool quirks, environment limitations, or special preconditions. For example, when calling a container deployment service, it may discover that an image must be pushed before the next configuration step can proceed. Or, when querying a specific log service, it may need to specify a particular time range first.

    The role of the skill deposition module is to abstract this experience, gained through trial and error and interaction, into rules or procedural text. When the agent encounters a similar task again, it can directly use these skill modules as guidance, avoiding the cost of repeated trial and error. This data also needs to follow capacity-control principles: through periodic summarization, frequently used skills are retained, while low-frequency or invalid skills are eliminated.

    4. Reflection Engine: The Timer That Drives the Closed Loop

    The reflection engine is the source of power for the entire learning loop. An agent should not only receive instructions passively; it also needs the ability to reflect proactively. The reflection engine can be understood as a scheduled background task or an event-triggered hook.

    After a complex task is completed, or after a certain amount of interaction has occurred, the reflection engine is awakened. It asks the large model to review recent session history and answer several key questions:

    1. Have any new facts appeared in the environment that need to be remembered long term?
    2. Are there new characteristics in the user’s communication style or preferences?
    3. Did the task execution encounter pitfalls that can be distilled into new skills?

    If the answer is yes, the reflection engine calls the update interfaces of the memory and skill modules and triggers the information rewriting and compression logic. This mechanism gives the agent genuine continuous learning capability across sessions.

    5. Production Deployment and Infrastructure Recommendations

    When moving this closed-loop agent architecture into production, developers need to rely on reliable cloud infrastructure. For example, the agent’s core runtime can be deployed on a cloud container platform such as Baidu AI Cloud for elastic scaling. Plain-text memory files can be mounted directly on object storage or file storage to ensure state persistence and high availability. Scheduled triggers for the reflection engine can be implemented with a message queue or serverless functions.

    This “light state, logic-heavy” design allows the agent to achieve high-quality long-term memory without relying on an expensive vector retrieval cluster, greatly reducing operational costs and system complexity.

    Conclusion

    The key to building agents with long-term memory and continuous learning is not unlimited storage, but the creation of a scientific mechanism for information filtering and compression. Through the three-in-one loop of memory, skills, and the reflection engine, together with strict capacity limits and snapshot state management, an agent can accumulate high-density knowledge. This design approach not only effectively governs context bloat, but also provides a valuable engineering reference for the next generation of enterprise agents.