Agent Self-Evolution Loop Architecture: Memory, Skills, and Proactive Reflection

    This article breaks down the architecture of a self-evolution loop for AI agents, a pattern designed to fix the "amnesia after each session" problem that keeps agent capabilities flat. It explains how three cooperating subsystems—memory, skills, and proactive reflection—work together, including capacity-limited structured memory files, automatic skill extraction from execution traces, and scheduled lightweight review. Readers will find design constraints, sample code, governance practices, and practical pitfalls for building a loop that gets stronger with use.

    1. Background: Session Amnesia Is the Root Cause of Stagnant Agent Capability

    Most agent frameworks are stateless by default: when a session ends, the context is discarded, and the next run starts from a blank slate. This design is simple to implement and safe for isolation, but it pushes the entire burden of accumulating experience onto the user. As a result, users have to repeat the same background explanations, the same environment conventions, and the same lessons learned over and over. Interaction cost grows linearly over time, while the agent’s capability curve stays flat.

    A clear technical demand has emerged in the industry: after completing a task, an agent should persist its procedural experience and reuse it directly in similar tasks later. This article discusses the general architecture of such a “self-evolution loop.” It is built from three cooperating subsystems: a memory subsystem, a skill subsystem, and a proactive reflection subsystem. Note that this is a discussion of architecture patterns and engineering constraints, not a reference to any specific product implementation.

    An analogy helps clarify the division of labor. Memory is like an assistant’s pocket notebook, recording “environment facts” and “human preferences.” Skills are like an operations manual the assistant builds up over time, recording “the correct steps and preconditions for a certain class of task.” Proactive reflection is like an alarm clock that goes off at the right moment, prompting the assistant to look back and decide whether anything from this round of interaction is worth retaining. All three are essential: memory without skills means the agent “knows” but cannot “do”; skills without memory lack the context needed to apply them; without a reflection mechanism, the first two degrade into purely passive writes that no one maintains over the long term.

    2. Memory Subsystem: Using a Capacity Limit to Force Information Compression

    Memory is the part of a self-evolving system that most easily spirals out of control. A common naive approach is pure append: new information keeps getting written into the same file, and after a few months the file balloons to tens of thousands of lines. Retrieval then requires reading the whole thing, noise drowns out the signal, and the cost of injecting it into context rises sharply.

    A more robust design is “limited capacity + structured entries.” This can be implemented as two plain-text files, with entries split by a consistent delimiter:

    1. ~/.agent/memories/
    2. ├── MEMORY.md # Environment facts, project conventions, tool quirks
    3. └── USER.md # User preferences, communication style, collaboration habits

    The key constraint is that the character limits should be set “relatively tight”—for example, around 2200 characters for environment memory and around 1375 characters for the user profile. These are not arbitrary numbers; they are a governance mechanism. Once capacity becomes a scarce resource, every write must involve a tradeoff, low-value information is naturally squeezed out, and what remains is high-density facts. This turns “memory governance” from something that depends on the user periodically cleaning up into automatic eviction at the architecture level.

    For implementation, it is recommended to maintain two parallel sets of state: one is a real-time writable list of entries, and the other is a snapshot frozen at the start of a session. Sample code:

    1. class MemoryStore:
    2. def __init__(self, memory_char_limit=2200, user_char_limit=1375):
    3. self.memory_entries = [] # Real-time writable
    4. self.user_entries = []
    5. self.memory_char_limit = memory_char_limit
    6. self.user_char_limit = user_char_limit
    7. # Snapshot frozen within the session to keep the prompt stable
    8. self._snapshot = {"memory": "", "user": ""}

    The snapshot mechanism solves an easily overlooked problem: if memory is reassembled and injected into the prompt on every turn, the system prompt within the same session keeps drifting, model behavior jitters accordingly, and caching becomes hard to hit. A frozen snapshot keeps the context constant within a single session, so writes only affect the next session. This decouples “learning” from “execution” in time.

    Three additional engineering points are worth noting. First, normalize and deduplicate before writing to avoid synonymous entries taking up space repeatedly. Second, keep metadata such as source and timestamp for each entry, so conflicts can be resolved by recency. Third, when the limit is reached, use an “importance-first” eviction strategy rather than simply discarding the oldest entries.

    3. Skill Subsystem: Automatically Distilling Reusable Steps from Execution Traces

    The second type of experience is “how to do it.” The traditional approach relies on manually written skill files, which are essentially hand-written documentation: you can only do what has been written down, and coverage is entirely limited by the maintainer’s investment.

    The self-evolving architecture differs by handing skill production to the loop as well: after a task completes, the agent reviews the execution trace, extracts reusable steps, preconditions, and failure lessons, and generates structured skill entries for direct invocation in similar tasks later. A qualified skill typically contains four elements: applicable scenario, preconditions, key steps, and known pitfalls. For example, a conclusion like “when deploying a container orchestration cluster, you must push the images before step 2, otherwise the pull fails” is a typical “pitfall-type” piece of knowledge, often more valuable than a description of the happy path.

    To keep the skill library from rotting, supporting governance is needed: run a similarity search before adding a new skill, and merge it with existing entries rather than piling it on top; annotate skills with a successful invocation count and last-used time, and downgrade or archive entries with zero calls over the long term; keep versions when skill content changes, so a correct experience overwritten by a wrong one can be rolled back.

    4. Proactive Reflection Subsystem: Turning Retention from Passive to Active

    The first two subsystems are passive: if nothing triggers them, nothing gets written. So the third subsystem is responsible for “scheduled wake-up for review.” Its responsibility is narrow: at moments such as task completion, a cycle expiring, or an abnormal retry being detected, it triggers a lightweight reflection to judge whether this round contains facts worth remembering or steps worth solidifying, and then calls the corresponding write interface.

    The design focus is controlling frequency and cost: reflection runs only when necessary, its input is a compressed trace summary rather than the full log, and its output must be structured—specifying clearly “what to write, which file to write to, and at what priority”—to avoid the noise of open-ended generation.

    5. How the Loop Turns

    A complete cycle can be summarized as follows: at session start, freeze the memory snapshot and inject it into context; during execution, retrieve skills on demand; after the task ends, the reflection subsystem triggers a review; the review results are written into memory and skills respectively; the new content takes effect in the next session. As the number of cycles grows, the cost of repeated explanation drops and the first-attempt success rate rises—this is the source of “getting stronger the more you use it.”

    6. Implementation Advice and Common Pitfalls

    To build similar capabilities in your own system, start from a minimal loop: first implement only the two memory files with capacity eviction, verify the benefits, and then introduce skill extraction and proactive reflection. Common pitfalls include: memory without a limit causing context to balloon, recomputing the prompt every turn causing behavior drift, skills that only grow and never get deleted causing retrieval noise, and reflection running too frequently and driving up call costs.

    When hosting on the cloud, you can use object storage to hold the memory and skill files, a message queue to decouple reflection tasks, a container platform to host the runtime, and logging services with monitoring and alerting to observe hit rates and call costs. For users who do not write code, the same loop can be packaged as a default capability of an upper-layer product, hiding details such as file paths, capacity thresholds, and reflection timing behind configuration, so that experience retention becomes an out-of-the-box basic feature rather than a burden that requires manual maintenance.