Agent Self-Evolution Loop Architecture: Memory, Skills, and Triggers Explained

    This article breaks down the architecture of a self-evolving agent loop through three subsystems: memory, skills, and triggers. It explains why session-bound context causes agents to "forget" everything once a conversation ends, and how a closed loop of execute, trigger, reflect, persist, and reuse turns procedural experience into durable assets. You will find the two-file memory design with hard character limits, the snapshot versus live-state split, the criteria for distilling reusable skills from execution traces, and the engineering pitfalls to avoid, along with a practical adoption path and notes on running the loop on Baidu AI Cloud.

    1. The Problem: Session Amnesia Is a Structural Flaw

    Most agent frameworks treat a single session as the entire world. Inside the context window the agent can reason and call tools, but the moment the session ends, every intermediate conclusion evaporates. The next time the user opens the agent, they have to re-explain the project layout, coding conventions, environment quirks, and personal preferences.

    On the surface this looks like a context length problem. It is actually an architectural one: the system has no channel for converting procedural experience into a persistent asset.

    The industry usually responds in one of two ways. One relies on hand-maintained rule files, where developers write behavior instructions by hand; whatever gets written works, whatever does not gets ignored. The other uses append-only log-style memory, which swells to tens of thousands of lines over time, so retrieval means reading the whole thing and the noise far outweighs the signal.

    Both paths share the same flaw: the producer and the consumer of knowledge are split apart. Humans produce, the model only consumes.

    The goal of a self-evolving loop is to hand knowledge production back to the agent itself: after a task completes, it reviews what happened automatically, distills reusable experience, and calls on it directly the next time a similar task appears. The rest of this article covers three subsystems.

    2. Overview: Three Subsystems and One Loop

    A learning loop that actually ships usually has three parts, with clear boundaries of responsibility:

    • Memory subsystem: stores “facts,” such as environment configuration, project conventions, and abnormal tool return formats.
    • Skill subsystem: stores “methods,” meaning reusable step sequences and caveats.
    • Trigger subsystem: reminds the agent at the right moment to go back and organize, so experience does not stay trapped inside a session.

    The loop runs in this order: execute a task, fire a reminder, review and distill, write to memory or skills, then read and reuse in the next task. Miss any link and the system degrades into ordinary question answering. With all three in place, capability becomes cumulative.

    3. Memory Subsystem: Trading Capacity for Information Density

    3.1 Two Files, Separated Responsibilities

    A restrained design uses only two plain text files to carry all long-term knowledge: one records the agent’s understanding of the environment and project, the other records its understanding of the user. Both split entries with a consistent delimiter so they can be added, removed, updated, and queried programmatically.

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

    The value of the split is that read and write strategies differ. Environment facts change slowly and are largely shared; user preferences change fast and are highly personal. Mixed in one file, every update has to deal with unrelated content.

    3.2 Hard Limits: Let Eviction Happen Naturally

    The key design is a character-level hard limit on both files, for example around 2200 characters for environment memory and around 1375 for user memory. This is not a compromise but deliberate scarcity. When capacity is finite, writing a new entry requires weighing its priority, outdated information gets pushed out naturally, and what remains is high-density fact. By contrast, an unlimited append mode always degrades into a low signal-to-noise document, with retrieval cost rising linearly over time.

    3.3 Two Tracks: Snapshot and Live State

    During a session, memory gets rewritten many times. But once the system prompt has been assembled it should not change, otherwise instructions become inconsistent between earlier and later turns in the same session and model behavior drifts. The common engineering answer is to maintain two parallel states: a writable list of entries, and a snapshot frozen at session start.

    1. class MemoryStore:
    2. def __init__(self, memory_char_limit=2200, user_char_limit=1375):
    3. self.memory_entries = [] # Live and writable
    4. self.user_entries = []
    5. self.memory_char_limit = memory_char_limit
    6. self.user_char_limit = user_char_limit
    7. self._snapshot = {"memory": "", "user": ""} # Frozen within the session
    8. def render(self):
    9. return self._snapshot # Inference reads the snapshot only
    10. def commit(self):
    11. self._snapshot = { # Refreshed once at the session boundary
    12. "memory": self._join(self.memory_entries, self.memory_char_limit),
    13. "user": self._join(self.user_entries, self.user_char_limit),
    14. }

    Three points matter: writes go to the live list, reads go to the snapshot; the refresh happens only at session boundaries; and trimming runs once at commit time rather than reordering on every write.

    4. Skill Subsystem: Distilling Reusable Steps from Execution Traces

    Skills differ from memory in abstraction level: memory describes what the world looks like, a skill describes how to do this kind of task. Producing skills automatically takes three pieces.

    First, leave a trace of the task. The tool call sequence, failed retries, errors, and corrections need to be recorded in a structured way, otherwise there is nothing to review.

    Second, distillation criteria. Not every trace is worth keeping. A skill is usually generated only when the task converged successfully; at least one failure or rework occurred along the way; and the steps are transferable rather than dependent on one-off parameters. Criteria that are too loose fill the skill library with noise; criteria that are too narrow defeat the purpose of distilling anything.

    Third, structured writes and retrieval. A skill entry generally has four parts: applicable scenario, prerequisites, step sequence, and pitfalls. Entries are stored with scenario tags; when a new task arrives, semantic matching runs first, a hit loads the skill, and a miss falls back to general reasoning.

    1. Skill entry template
    2. - Scenario: releasing a containerized service
    3. - Prerequisites: image built, artifact address reachable
    4. - Steps: push image -> update deployment spec -> wait for readiness probe -> smoke test
    5. - Pitfall: forgetting to push the new image rolls back to the old version

    5. Trigger Subsystem: Turning Review from Voluntary into Mechanical

    Review is the step most easily skipped, so it has to be mechanism-driven. A common implementation is a lightweight reminder engine: it fires on a fixed interval of turns, a task complexity threshold, or a failure count, and injects a short instruction asking the agent to judge whether the current round contains anything worth distilling. The trigger prompt should be cheap, switchable off, and configurable in frequency, so it neither interrupts the main task constantly nor eats into the context budget itself.

    6. Engineering Practices and Common Pitfalls

    • Separate reads and writes completely: any implementation that changes the system prompt mid-session causes behavior drift that is hard to pin down.
    • Make eviction explicit: capacity limits need priority rules alongside them, for example facts the user has explicitly corrected take precedence.
    • Deduplicate and merge skills: before a new skill is stored, compare it against existing entries; merge duplicates, and resolve conflicts in favor of the most recent successful trace.
    • Observability: log the reason for every memory write, skill generation, and skill hit, so you can trace back why capability changed.
    • Privacy and permissions: user profile information is sensitive data and should support viewing, exporting, and one-click deletion.
    • Failure isolation: a failed distillation must not affect the main task’s response; the loop has to be a side path, not the main chain.

    7. Suggested Adoption Path

    To build the same capability in your own system, move in four steps: first, record session output in a structured trace; then introduce two-file memory with hard limits and the snapshot mechanism; next, complete the trigger and distillation pipeline, starting from a few high-value scenarios; finally, add retrieval and evaluation, using a fixed task set to regression-test whether the system genuinely gets stronger with use.

    In practice at Baidu AI Cloud, this kind of loop typically works alongside the general components above: object storage holds skill and memory archives, a message queue decouples distillation jobs, a container platform runs the review jobs, and log and monitoring services handle tracing and anomaly detection. The components are replaceable while the loop structure stays the same, which is the fundamental reason self-evolving capability is portable and reusable.