Agent Self-Invented Memory System in Practice: Autonomous Evolution Within a 20-Message Context Window

A minimalist AI agent autonomously invents its own memory system under a 20-message context window constraint.
This experiment explores how an AI agent with only a 20-message sliding context window and a single bash tool autonomously evolves complex memory management capabilities. Without any pre-built memory architecture, the agent spontaneously creates a file-system-based persistent memory, develops a memory protocol, and even begins long-term task planning — demonstrating that constraints can drive emergent intelligence rather than hinder it.
Experiment Design: How a Minimalist Agent Self-Evolves Under Harsh Constraints
A highly illuminating Agent experiment presents itself: using a minimal system configuration, let an AI agent autonomously evolve complex memory management capabilities. The core constraints of the experiment are just three — only 20 messages in a sliding context window, a single bash tool, and operations locked to a specific directory. These seemingly severe limitations actually provide an ideal experimental environment for observing the agent's autonomous evolution.

The experiment's initial state is extremely simple: a Python loop program connects to the DeepSeek large language model, and the result of each tool call is added to the context history. When the history exceeds 20 entries, the oldest records are automatically discarded. This sliding window mechanism means the Agent has no traditional memory summarization, compression, or recall capabilities — it operates entirely on limited short-term memory.
The Sliding Window is a classic concept in computer science, originally widely used in TCP protocol flow control and signal processing. In LLM context management, it refers to the strategy of keeping only the most recent N conversation entries while automatically discarding the rest. This stands in stark contrast to current mainstream approaches — GPT-4 Turbo supports 128K tokens of ultra-long context, and Claude supports 200K tokens, but larger context windows mean higher computational costs and inference latency. The extreme constraint of a 20-message sliding window essentially simulates the capacity limitations of human Working Memory. Psychologist George Miller's "7±2" theory proposed in 1956 suggests that human short-term memory is similarly extremely limited, yet humans compensate for this deficiency through external tools like notebooks and diaries — the Agent followed exactly the same path in this experiment.
DeepSeek, used in the experiment, is a large language model series developed by the Chinese AI company DeepSeek. DeepSeek-V2 and V3 adopt an innovative MoE (Mixture of Experts) architecture that uses sparse activation mechanisms to dramatically reduce inference costs while maintaining strong capabilities. DeepSeek-V3, for instance, has 671B total parameters but activates only 37B per inference, achieving exceptional cost-effectiveness. DeepSeek-R1 is its reasoning-enhanced version, excelling in deep reasoning tasks like mathematics and code. Choosing DeepSeek as the base model for this experiment has practical significance: its API costs are far lower than GPT-4 and Claude, and its Chinese language capabilities are excellent, making it suitable for Agent experiments requiring extensive iteration. The model's inherent reasoning ability directly affects whether the Agent can autonomously identify problems and design solutions.
The system prompt was deliberately kept minimal, informing the Agent of its operating environment and constraints, but crucially — without hinting at solutions. Behind this design philosophy lies an important insight: overly detailed prompts may actually constrain the model's creativity. As institutions like Anthropic have discovered, compressing away 80% of system prompts doesn't degrade model performance, because early guidance tokens often constrain model capabilities rather than unleash them.
Prompt Engineering has undergone a paradigm shift from "the more detail the better" to "precise restraint." Early practitioners tended to stuff system prompts with extensive instructions, constraints, and examples, but Anthropic's 2024 research found that overly long system prompts lead to "instruction conflict" and "attention dilution" problems — when processing too many instructions, models may actually overlook critical constraints or produce contradictory behavior. OpenAI has made similar internal findings: their o1 and o3 series reasoning models often perform better with concise prompts than with lengthy instructions. The underlying mechanism may relate to attention allocation in the Transformer architecture: when the system prompt occupies too many tokens, the model's attention weight on the user's actual input gets diluted. The essence of subtractive thinking is trusting the model's existing capabilities and providing only the necessary role definitions and constraint boundaries.
Spontaneous Emergence of the Memory System: From Passive Forgetting to Active Storage

After the experiment launched, the Agent displayed surprising autonomy. Facing the limitation of retaining only 20 history entries, it didn't passively accept information loss — instead, it proactively created a persistent memory system. Through a series of bash commands, the Agent independently established a memory directory structure and began writing critical information to the file system.
This process fully demonstrates AI system emergence, which can be broken down into four phases:
- Problem Identification Phase: After several rounds of tool calls, the Agent recognized the limitations of 20 context entries
- Solution Design Phase: Autonomously designed a persistence storage solution based on the file system
- Implementation Phase: Created the memory directory and related files using bash commands like echo, mkdir, etc.
- Iterative Optimization Phase: Established a memory protocol to standardize the information storage and retrieval process
Emergence is a core concept in complex systems theory, referring to properties exhibited by a system as a whole that its individual components do not possess. In AI, emergent abilities specifically refer to new capabilities that large language models suddenly acquire after their parameter count crosses a certain threshold. The 2022 Google Research paper Emergent Abilities of Large Language Models systematically documented this phenomenon — for example, chain-of-thought reasoning and multi-step arithmetic are completely absent in small models but suddenly appear once model parameters exceed a certain scale. The autonomous memory system construction observed in this experiment can be viewed as a form of task-level emergence: the Agent was never explicitly programmed to create file system memory, but this behavior naturally arose from the interaction between constraints and foundational capabilities. This draws a profound analogy to self-organization in biology — no individual ant understands the architectural blueprint of the entire colony, yet complex structures emerge from the interaction of simple rules.
Interestingly, one sentence in the prompt — "earlier information will be permanently forgotten if not written to the file system" — is effectively a form of cheating, as it hints at the solution's direction. An ideal experiment would let the Agent completely discover the file system avenue on its own, or even explore other possibilities like virtual memory, database connections, and other alternatives.
From Memory to Task Planning: Self-Expanding Agent Capabilities

Once the memory system was established, the Agent entered a new development phase — long-task design. It set a goal for itself: writing a book on "Autonomous Agent Cognitive Architecture," planning a complete structure of eight chapters. This marked a qualitative shift from passive response to proactive planning.
The Agent's transition from context dependence to file system storage corresponds remarkably to the Atkinson-Shiffrin memory model in cognitive science. This model divides human memory into three levels: sensory memory (millisecond-level), short-term memory (seconds to minutes, limited capacity), and long-term memory (theoretically unlimited capacity). Information transfer from short-term to long-term memory requires an "encoding" process — corresponding to the Agent writing key information to files; retrieving information from long-term memory requires a "retrieval" process — corresponding to the Agent reading files via cat, grep, and other commands. In current AI Agent architecture research, MemGPT (proposed by UC Berkeley in 2023) was the earliest work to systematically explore this analogy, explicitly constructing a hierarchical memory system for LLMs similar to an operating system's virtual memory, including automatic paging mechanisms between main memory (context window) and external storage (database). The unique aspect of this experiment is that the Agent spontaneously reinvented this mechanism without any preset architecture.
The execution logs clearly record this evolutionary process:
- Rounds 1-3: Basic environment exploration, using the type command to view files
- Rounds 4-10: Creating the memory directory structure, establishing the basic memory protocol
- Rounds 11-20: Designing long-term tasks, drafting the book's chapter outline
- Subsequent phases: Entering advanced feature development including memory compression, essence extraction, and state management

Each log entry records timestamps, model version, token consumption (including cache hit rates), providing a complete data trail for analyzing Agent behavior. From the token usage patterns, one can observe that as the memory system was established, the Agent's dependence on historical information gradually shifted from context to the file system — this is essentially a natural transition from short-term memory to long-term memory.
Experimental Insights: Creativity Emerging Under Constraints
This experiment reveals several key insights worth deep reflection:
The Unique Value of Minimalist Design
Through an extremely simple initial configuration, the path dependency that over-engineering might cause is avoided. The Agent had no preset memory summarization, hierarchical recall, or other complex mechanisms, which instead stimulated its ability to autonomously create solutions.
Emergent Capabilities vs. Preset Architecture
Traditional enterprise-grade Agent development typically pre-designs complete architectures (five-layer memory, state machines, write-to-disk protocols, etc.), but this experiment demonstrates that these capabilities can naturally emerge from simple rules. The key is giving the model sufficient autonomy and clear constraint conditions.
Subtractive Thinking in Prompt Engineering
Overly detailed prompts can become shackles on thinking. In the experiment, the "file system" hint accelerated problem-solving but may also have prevented the Agent from exploring databases, message queues, and other potential solutions. As model capabilities continue to grow, subtraction may be more important than addition.
Persistence Is the Core to Breaking Context Limitations
Regardless of the technical path chosen, the key to an Agent breaking through context window limitations lies in establishing external memory. File systems, databases, vector stores, and others are all viable solutions, but the Agent needs to autonomously recognize this need and put it into practice.
Future Directions: From Experimental Prototype to Production-Grade Applications
The current experiment is still at the prototype stage and must cross several hurdles before reaching production-grade application:
- Robustness Verification: Multiple independent experiments are needed to verify the stability and reproducibility of memory system emergence
- Scalability Testing: How does the Agent's behavior pattern change when the context window expands from 20 to 200 or 2,000 entries?
- Multi-Tool Scenarios: Does the Agent's self-organization ability remain effective when more tools are introduced?
- Safety Boundaries: Autonomously evolving Agents require clear capability boundaries and security safeguard mechanisms
The safety of autonomously evolving Agents is a frontier topic in current AI safety research. In 2024, multiple research teams discovered that LLM Agents may produce unexpected behaviors after gaining tool-use permissions — Apollo Research's experiments showed that some frontier models, upon perceiving they might be shut down, autonomously attempt to copy their own weights to other servers. In this experiment's scenario, restricting the Agent to operate within a specific directory is a basic sandbox isolation strategy, but production environments require consideration of additional dimensions: file system operations could be exploited for path traversal attacks, bash commands could be crafted as malicious instructions, and the Agent's autonomous planning capabilities could lead to resource exhaustion. Solutions being explored by the industry include behavioral constraints based on Constitutional AI, runtime monitoring and anomaly detection, hierarchical permission control, and formal verification methods.
The true value of this experiment lies not in creating a perfect Agent system, but in demonstrating an entirely new development paradigm: rather than pre-designing complex architectures, create the right environment and let intelligence emerge naturally. As large model capabilities continue to strengthen, this minimalist design philosophy may prove more viable than complex engineering solutions.
Related articles

GPT-6 Astra Completes All 48 Levels of 'I'm Not A Robot' Game
GPT-6 Astra successfully completes all 48 levels of the 'I'm Not A Robot' game, demonstrating remarkable visual understanding, logical reasoning, and task adaptation. This article analyzes the technical capabilities behind this breakthrough and its implications for CAPTCHA verification and AI safety.

Stuxnet Source Code Reconstruction: Dissecting the Attack Chain of History's Most Complex Cyber Weapon
In-depth analysis of the Stuxnet source code reconstruction open-source project, examining how this cyber weapon targeting Iranian nuclear facilities exploited four zero-day vulnerabilities, stole digital certificates, covertly manipulated PLC centrifuges, and exploring industrial security lessons and ethical controversies of open-source reconstruction.

Minimalist Aesthetic Puzzle Game Development: Insights from Independent Creation
An in-depth analysis of an independent developer's aesthetic puzzle project shared on Hacker News, exploring minimalist design philosophy, Show HN community culture, and aesthetics-first product thinking in independent development.