Building a Self-Evolving Agent Loop: Architecture and Design Constraints
This article breaks down the architecture of a self-evolving AI agent loop through three subsystems: memory, skills, and triggers. It explains why stateless sessions cap agent capability growth, how strict capacity limits improve information density, how to turn troubleshooting experience into reusable skill units, and when to trigger reflection. It also covers common implementation pitfalls and how to make the loop usable for non-developers. The final section outlines a reference setup on Baidu AI Cloud.
1. The Problem: Agents Forget Everything When the Session Ends
Most agents today run under a stateless session paradigm: when a task finishes, the context is released, and the next run starts from zero. This is fine for demos, but the weaknesses are amplified quickly in real production environments. The same environment probing gets repeated, the same errors get diagnosed again, and the same project conventions have to be explained over and over. Developer time is consumed by low-level repetition, while the agent’s capability curve stays flat.
Two approaches have emerged in the industry. The first is manual feeding: a human writes rule files, operating manuals, and knowledge entries, and the agent follows those files, knowing only what has been written. The second is automatic growth: after completing a task, the agent reviews its own work, distills experience from the process into reusable assets, and calls on them directly the next time it faces a similar problem. The difference is not in the feature list but in design philosophy: the capability ceiling of the first is determined by the maintainer’s investment, while that of the second is determined by usage duration and task diversity.
This article takes a general engineering perspective to break down which subsystems a self-evolving loop typically consists of, what the key design constraints of each subsystem are, and which pitfalls are easy to hit in practice. The end discusses how to make this capability available to business users who cannot code.
2. Overall Architecture: Three Subsystems, One Loop
A usable self-evolving loop must solve three problems at once: what to remember, how to reuse it, and when to reflect. This maps to three subsystems.
- Memory subsystem: stores high-density facts about the user and the runtime environment, solving the repeated-explanation problem.
- Skill subsystem: turns a one-off solution process into callable, composable units of operation, solving the repeated-discovery problem.
- Trigger subsystem: initiates reflection proactively at the right moment, solving the problem that nothing gets accumulated unless someone reminds the system.
Strung together, the three form a loop: task execution produces raw traces, the trigger subsystem invokes reflection at defined points, part of the reflection output goes into memory and part is solidified into skills, and skills plus memory in turn improve the efficiency of the next task. The loop only holds if all three paths exist and are low-friction; missing any one of them degrades the system into an ordinary conversational assistant.
3. Memory Subsystem: Trading Capacity for Information Density
3.1 A Minimal Storage Structure
One proven, restrained approach uses only two plain text files to hold all long-term knowledge: one records environment facts, project conventions, and tool quirks; the other records user preferences, communication style, and working habits. Entries are separated by a consistent delimiter so the program can add, delete, update, and query them individually.
A schematic structure looks like this:
<agent_home>/memories/├── MEMORY.md # Environment facts, project conventions, abnormal tool behavior└── USER.md # User preferences, communication style, collaboration habits
3.2 A Hard Limit Is a Feature, Not a Flaw
The key is to set a fairly tight character limit on both files (for example, on the order of a few thousand characters) and to run a budget check before writing. Limited capacity produces three positive outcomes: before writing, you must judge whether a piece of information deserves a slot; outdated information is naturally squeezed out through competition; and read cost stays constant instead of growing linearly with usage duration.
The counter-example is pure append mode: the file only grows, and after a few months it swells to tens of thousands of lines, so retrieving one old conclusion means reading the whole file, which is both slow and prone to reintroducing contradictory stale facts. Limited capacity is essentially an information compression mechanism that replaces “record everything” with “record what matters most right now.”
3.3 Isolating Live State from the Session Snapshot
An easily overlooked engineering detail: during a session you need a writable list of entries, but the system prompt is often already rendered when the session starts. Reusing the same mutable data directly causes the prompt to fall out of sync with the actual state. The common solution is to maintain two parallel sets of state.
class MemoryStore:def __init__(self, memory_char_limit=2200, user_char_limit=1375):self.memory_entries: list[str] = [] # Live, writableself.user_entries: list[str] = [] # Live, writableself.memory_char_limit = memory_char_limitself.user_char_limit = user_char_limit# Snapshot frozen at session start to keep the prompt stableself._system_prompt_snapshot: dict[str, str] = {"memory": "", "user": ""}
Writes go to the live lists and trigger budget trimming; prompt rendering reads from the snapshot. This keeps the context stable and reproducible within a single turn while still allowing cross-turn updates to take effect, and it makes debugging easier when you need to figure out “why didn’t it remember that.”
4. Skill Subsystem: Turning Pitfall Experience into Reusable Assets
Memory handles facts; skills handle methods. A reusable skill unit typically has four elements: applicable scenario, preconditions, operation steps, and failure fallback. The generation flow is best split into four steps.
- Trace collection: record the task goal, the tool call sequence, errors, and the final fix.
- Value judgment: distill only when the task succeeded and the path was non-trivial, so accidental success does not get written up as a standard.
- Structured distillation: compress the process into a step-by-step description, removing temporary parameters tightly bound to this particular task.
- Indexing and invocation: build an index by scenario tags; a new task first does a similarity match, loading the skill on a hit and falling back to general reasoning on a miss.
One boundary needs emphasis: skill entries should be treated as suggested paths rather than hard constraints, and must retain manual override and automatic invalidation mechanisms. Otherwise wrong experience gets amplified repeatedly, which is the most common source of runaway behavior in self-evolving systems.
5. Trigger Subsystem: Making Reflection Happen at the Right Time
The timing design matters more than the reflection algorithm. Too frequent, and it slows the main task and generates noise; too sparse, and too little gets accumulated. In practice, three types of triggers work: the wrap-up phase after a successful task, a threshold of consecutive failures, and periodic review during idle windows. Each trigger should have a budget cap, for example allowing only a limited number of new entries and only one skill candidate, so the context is not flooded by self-generated text.
6. Implementation Advice and Common Pitfalls
- Get the loop first, intelligence later: missing any of the three paths sharply reduces the payoff.
- Design capacity and budgets explicitly: character limits, new entries per turn, and skill library size should all have clear constraints.
- Prioritize observability: log the reason for every write, eviction, and skill hit so capability changes can be traced back.
- Permissions and isolation: memory and skill files involve user data, so apply access control and minimal retention.
- Productize for non-developers: present reflection conclusions as natural-language confirmation cards that business users can accept or reject with one click, so capability accumulation no longer depends on writing code.
7. Conclusion
The core of a self-evolving agent is not a longer context but a constrained accumulation mechanism: limited capacity forces information compression, structured skills turn experience into callable assets, and proactive triggers guarantee that accumulation actually happens. Get these three solid, and the agent genuinely gets stronger the longer it is used. To deploy this on Baidu AI Cloud, you can use object storage to hold memory and skill files, a message queue to decouple reflection tasks, a container platform to host the runtime, and logging plus monitoring and alerting services for observability, assembling a stable and controllable learning loop from general-purpose cloud capabilities.
