Building a Self-Evolving Agent Loop: Architecture Breakdown

    This article breaks down the architecture of a self-evolving agent loop built from three subsystems: memory, skills, and proactive reminders. It explains why session-scoped context prevents agents from accumulating capability, how strict character limits force information compression, why freezing a per-session snapshot keeps reasoning consistent, and how reusable skills turn one-off experience into repeatable procedures. Engineering pitfalls such as cold start, concurrent writes, observability, privacy, and evaluation are also covered.

    1. The Problem: Agents Forget Everything When the Session Ends

    Most agent frameworks separate reasoning from state. Within a single session, an agent can call tools, read and write files, and complete multi-step tasks. But once the session ends, the context is reclaimed, and the next run starts from a blank slate. Teams end up spending significant effort repeatedly restating background: project directory structure, build commands, code conventions, and the quirks of the production environment. Human effort is consumed by low-value repetition, and the agent’s actual output is capped at the baseline of generic capability.

    The common industry workaround is an append-only notes file: every observation is written to the end of the same document. It works in the short term, but after a few months it degrades into a giant file tens of thousands of lines long. Retrieval depends on reading the whole thing, noise far outweighs signal, and outdated information never leaves. The real problem is not whether the agent can remember, but how to continuously retain high-density information under a fixed capacity, and how to turn one-off experience into capabilities that can be invoked repeatedly. These two points are the starting point for designing a self-evolving loop.

    2. Overall Architecture: Three Subsystems Forming a Loop

    A working self-evolving system typically consists of three parts, none of which can be missing:

    • Memory subsystem: stores facts and preferences, answering “what do I need to know about this user and this environment.”
    • Skill subsystem: stores reusable procedures, answering “how should this kind of task be done step by step next time.”
    • Proactive reminder subsystem: triggers review at the right moment, answering “what in that last interaction is worth capturing.”

    Together they form a loop: the reminder subsystem discovers candidates, memory and skills consolidate and invoke them, and the new observations produced during invocation flow back as candidates for the next round. Without the reminder step, the system degrades into passive recording. Without capacity constraints, it degrades into noise accumulation. Without the skill layer, it can remember facts but not how to do things.

    3. Memory Subsystem: Using Hard Capacity to Force Compression

    3.1 Two-File Structure with Separated Responsibilities

    A restrained approach that has been proven to work uses only 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

    Separating “environment awareness” from “user awareness” helps because the two have completely different update frequencies and lifecycles: project conventions may change with each iteration, while communication preferences are relatively stable. Mixing them in one file means high-frequency changes drown out low-frequency but critical information.

    3.2 Character Limits Are a Feature, Not a Constraint

    Setting a fairly tight character limit on both files (for example, a few thousand characters) turns “what to remember” into a decision that requires trade-offs. When capacity runs short, the write logic must make choices: as new entries arrive, low-value or outdated entries are pushed out. This contrasts sharply with append-only notes, where information density declines monotonically over time, whereas here density is constrained to a stable range.

    Two engineering points matter. First, the eviction policy must be explainable to avoid accidentally deleting critical conventions; a common approach is to annotate entries with importance or recency. Second, compression should happen at write time, not as a remedy after the file has already bloated.

    3.3 Isolating Live State from Session Snapshots

    An easy trap: memory is updated while a session is in progress, but the context used for the current round of reasoning should not drift with it, or earlier and later instructions may contradict each other. A common solution is to maintain two parallel sets of state — one writable list of entries, and one 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. # Frozen at session start; read-only for this reasoning round
    8. self._prompt_snapshot = {"memory": "", "user": ""}

    The snapshot guarantees consistency within a single round, and writes only affect later sessions, achieving “learnable but not jittery.”

    4. Skill Subsystem: From One-Off Experience to Reusable Procedures

    Fact memory solves “knowing”; skills solve “being able to do.” The skill layer typically crystallizes a successful experience into a structured document: applicable conditions, pre-checks, step-by-step operations, failure fallbacks, and verification. Four key design points:

    1. Explicit trigger conditions: a skill must declare when it applies, or it will be reused incorrectly.
    2. Verifiable: each step carries a checkpoint, so a failure can be traced to a specific stage.
    3. Elidable: skills need capacity governance too; entries that go unmatched for a long time should be down-weighted or archived.
    4. Restrained generation timing: distill at successful task completion, rather than interrupting frequently during execution.

    Compared with the path of manually authoring all capabilities in advance, automatic distillation has value because coverage expands naturally with usage, and the captured content carries real environment details — less general, but with a higher hit rate.

    5. Proactive Reminder Subsystem: Making Capture Active Instead of Passive

    The reminder module does not need to be complex. The core is “do a lightweight review at low-frequency, low-interruption moments”: task completion, session idle, reaching a call-count threshold, and so on. The review produces only two kinds of output — candidate memory entries and candidate skills — and both must pass validation (duplicate? outdated? specific enough?) before being written to disk. Skip validation, and the loop is quickly polluted by self-contradictory entries.

    6. Engineering Implementation and Common Pitfalls

    • Cold start: with no accumulation early on, allow a small number of high-value entries to be pre-seeded manually, and loosen capacity gradually from tight to loose.
    • Concurrent writes: when multiple sessions run in parallel, lock the memory files or serialize writes to avoid losing entries.
    • Observability: log the reason for every eviction and write, so it is possible to trace back “why it forgot this.”
    • Privacy and compliance: memory files are sensitive data; support per-project isolation, export, and one-click clearing.
    • Evaluation: regress against a fixed task set, tracking success rate and average number of interaction rounds, to verify whether “better with use” actually happens rather than trusting a feeling.
    • Cloud deployment: if team sharing is needed, put memory and skills in object storage or a configuration center, serialize updates with a message queue, and use logging and monitoring with alerting for auditing, with a container platform hosting the stateless reasoning layer.

    7. Conclusion

    The essence of self-evolution is not making the model smarter, but accumulating context and procedures in a structured way. Memory compresses, skills enable reuse, and reminders drive the process; once the three form a loop, the agent’s capability curve rises with usage time. For developers, the most underestimated parts in practice are precisely the two “small designs” of capacity constraints and snapshot isolation — they determine whether the loop yields long-term gains or long-term noise.