Self-Improving AI Agent Design: A Closed Loop of Memory, Skills, and Reflection

    Most agent frameworks lose their runtime state after each session, forcing the agent to rediscover the same solutions and preventing it from learning user or project context. This article presents a practical closed-loop architecture for self-improving agents built from three interlocking parts: a deliberately constrained memory system for environment facts and user preferences, a skill system that converts task retrospectives into reusable procedures, and a reflection engine that decides what should persist. It also covers implementation details such as frozen prompt snapshots, capacity limits, write deduplication, skill lifecycle management, privacy guardrails, and a hosted path that brings the same loop to non-developers.

    1. The Starting Problem: Session-Level Amnesia Is a Common Agent Flaw

    Most current agent frameworks share an architectural weakness: the session is the boundary of memory. When a task ends, all runtime state is discarded. The next time the agent faces a similar problem, it must start from scratch, repeating the same failed attempts and rediscovering the same pitfalls.

    This pattern has two consequences:

    1. Marginal cost does not decline. The cost of executing similar tasks remains constant and cannot be amortized through accumulated experience.
    2. Personalization is effectively zero. The agent can never learn contextual facts such as “this project requires pushing the image before deployment” or “this user prefers concise responses.”

    The engineering community increasingly agrees that agents need persistent learning mechanisms at the architecture level to produce compounding effects. A mature self-improving agent is usually built from three interlocking subsystems that form a closed loop:

    • Memory system: Records environment facts and user preferences. It is the agent’s notebook.
    • Skill system: Captures reusable operational experience. It is the agent’s playbook.
    • Reflection engine (Nudge Engine): Triggers periodic retrospectives and decides which information is worth persisting.

    A useful analogy clarifies the division of labor: memory is a notebook that records factual knowledge such as “the manager prefers Americanos”; skills are an operational manual containing procedural knowledge such as “the second step of deploying to the container cluster is to push the image first”; the reflection engine is an alarm clock that regularly reminds the agent to look back and consider whether anything is worth recording.

    2. Memory System: A Restrained Design Beats Uncontrolled Accumulation

    Two Files Hold the Agent’s Entire Model of the World

    One minimalist design worth studying uses two plain-text files to hold all memory, with entries separated by delimiters:

    1. ~/.agent/memories/
    2. ├── MEMORY.md # Agent notes: environment facts, project conventions, tool quirks
    3. └── USER.md # Knowledge about the user: preferences, communication style, work habits

    The reason to split memory into two files is that the two kinds of memory have very different lifecycles. Environment facts are relatively stable but may become invalid after a project migration. User preferences are highly personalized and remain useful across tasks. Separate files keep retrieval paths clear and allow each type of memory to be governed independently.

    Capacity Limits: The Most Important Counterintuitive Design Choice

    The key design decision is to set deliberately tight character limits: about 2,200 characters for environment memory and about 1,300 characters for user knowledge. The sample implementation below uses 1,375 as the user limit. This looks like a restriction, but it is actually a forcing function:

    • Because capacity is limited, the agent must filter information when writing and retain only high-value facts.
    • If a new entry exceeds the limit, the agent must compress, merge, or evict stale entries.
    • Whatever survives over time is naturally high-density information that has been repeatedly validated.

    Compare this with a common alternative: an append-only memory file. Its failure mode is predictable. After a few months of unrestrained appends, it grows into a massive file with tens of thousands of lines. Retrieval degrades into reading the entire file, the signal-to-noise ratio approaches zero, and the memory system exists in name only. A bounded capacity combined with mandatory compression embeds information lifecycle management directly into the memory write path.

    Implementation Notes: Maintaining Parallel Live and Snapshot States

    In implementation, the memory store needs to maintain two parallel sets of state:

    1. class MemoryStore:
    2. def __init__(self, memory_char_limit=2200, user_char_limit=1375):
    3. self.memory_entries: List[str] = []
    4. self.user_entries: List[str] = []
    5. self.memory_char_limit = memory_char_limit
    6. self.user_char_limit = user_char_limit
    7. self._system_prompt_snapshot: Dict[str, str] = {"memory": "", "user": ""}
    • Live, writable entry lists: These can be appended to or modified at any point during a session.
    • A snapshot frozen at session start: This is the copy of memory injected into the system prompt, ensuring consistency of context within a single session.

    Why freeze a snapshot? If the memory in the system prompt changes halfway through a session, the agent’s understanding may become inconsistent or even self-contradictory. The snapshot mechanism creates isolation: reads remain stable within a session, while writes can evolve across sessions.

    3. Skill System: The Key Leap from Hand-Authored to Self-Generated

    Memory answers “what to know”; skills answer “how to do it.” The mainstream approach today is manually maintained skill files, essentially a set of human-written Markdown documents describing the steps for a class of tasks. This model has a clear ceiling: the agent only has the skills someone wrote for it, and the growth rate of the skill library is limited by human effort.

    A self-improving design changes skill production from being fed by humans to growing on its own. After the agent completes a task, the reflection engine reviews the entire execution trace:

    1. Which attempts failed during this task, and why did they fail?
    2. Can the final successful path be abstracted into reusable steps?
    3. Does the abstraction overlap with existing skills? Should it be merged or created as a new skill?

    Through these three steps, lessons learned from mistakes are automatically distilled into structured skill entries. The next time a similar problem appears, the agent can invoke the skill directly and skip the exploration phase. The longer it is used, the thicker the skill library becomes and the more efficient execution gets. This is the source of capability compounding.

    It is worth emphasizing that this is not merely a feature difference. It is a philosophical divide in design. One model depends on external input, so its scale ceiling equals the ceiling of human effort. The other has an endogenous growth capability, so its scale ceiling equals usage frequency.

    4. Practical Recommendations for Production

    Several engineering considerations matter when moving this closed loop into production:

    • Memory writes should be idempotent and deduplicated. The reflection engine may extract the same lesson repeatedly, so the agent should perform similarity comparison before writing.
    • Skills need versioning and retirement mechanisms. An outdated skill is more dangerous than a missing one. Each skill should record the task it came from and be retired when it becomes invalid.
    • Capacity parameters should be configurable. Memory density varies greatly across scenarios, so limits should be configuration options rather than hard-coded values.
    • Privacy boundaries must be established up front. The collection and storage of user-preference memories should follow the principle of least necessary data.

    5. Extension: Bringing the Loop to Non-Coders

    One promising extension is to expose this capability to non-developers through a hosted platform. If memory, skills, and reflection are packaged as out-of-the-box cloud services, using common cloud building blocks such as LLM inference services, object storage, and logging services, then operations, marketing, and administrative staff can obtain an assistant that understands them better over time without writing any code.

    The democratization of self-improving capabilities may become the next competitive focus in the agent market after foundation model capabilities.