M⋆ Framework: Building Task-Specific Memory Systems for AI Agents
Large Language Models (LLMs) often face performance bottlenecks due to their limited context windows and generic memory mechanisms when handling complex tasks. To address this, researchers from Microsoft and the City University of Hong Kong have proposed the M⋆ framework. It aims to tailor the most efficient memory system for each task through automated code generation, advancing AI agents from a ‘general brain’ to a ‘specialized brain’.
Limitations of Current AI Memory Systems
Current mainstream external memory techniques, such as Retrieval-Augmented Generation (RAG), typically use vector similarity-based retrieval mechanisms. While this ‘one-size-fits-all’ approach performs adequately in general scenarios like open-domain question answering, it falls short in specialized tasks requiring precise logic, structured information, or complex reasoning.
For instance, when executing precise instructions in a gaming environment (like the ALFWorld benchmark) or conducting rigorous case analysis in the legal domain (like the PRBench benchmark), vague semantic similarity retrieval can easily lead to confusion of critical information, thereby affecting the final task success rate. The researchers point out that the core issue is that different tasks have vastly different requirements for how memory is organized, stored, and retrieved, and a fixed ‘universal’ memory system cannot adapt to all scenarios.
The Core Mechanism of M⋆: An Automated Framework Based on Reflective Code Evolution

To tackle these challenges, the M⋆ framework introduces an automated process based on evolutionary algorithms, allowing the AI to design and write its own memory system code. Its core mechanism consists of three key parts:
Memory as Code: M⋆ abstracts the memory system into an executable Python program (Memory Program). This program includes three dimensions:
- Schema: Defines the storage format of memory, such as structured tables, natural language logs, or graph relationships.
- Logic: Specifies how data is stored and retrieved, allowing for flexible use of various tools like SQLite (for precise queries), ChromaDB (for vector retrieval), and even calling an LLM for secondary processing.
- Instruction: Provides a clear guide for the task-performing agent (Task Agent) on how to use this memory system.
Reflective Code Evolution: This is the innovative core of M⋆. The system continuously optimizes the memory program through an iterative loop:
- Seed: Starts with a set of basic memory program templates.
- Evaluate: The Task Agent uses the current program to perform a specific task, and its performance and failure cases are recorded.
- Reflect: A more capable ‘Reflector’ LLM is introduced to analyze failure cases, diagnose problems with the current memory program, and propose specific code modification suggestions (Patches).
- Auto-Repair: The system automatically applies the code patches and performs compilation and syntax checks to ensure the newly generated code is executable.
- Select: Based on task performance scores, programs that perform better are given a higher weight in the next round of evolution, achieving survival of the fittest.
Population-based Search: To avoid getting stuck in local optima, M⋆ maintains a ‘population’ of multiple memory programs simultaneously. This strategy preserves population diversity, allowing the system to explore a wider solution space and discover innovative memory architectures that might initially seem mediocre but have great potential.
Experimental Results: Emergence of Task-Specific Memory Architectures and Performance Leap
The research team validated M⋆ on four widely varying benchmarks (LoCoMo, ALFWorld, HealthBench, and PRBench), comparing it against nine existing state-of-the-art memory solutions, including Mem0. The results showed that M⋆ achieved first place in 7 out of 8 sub-tasks, with performance improvements of up to 31% over the previous best methods in complex tasks like dialogue and legal analysis.
Even more enlightening were the final memory architectures that M⋆ evolved for different tasks, which showed significant ‘species diversity’:
ALFWorld (Embodied AI Game): The optimal evolved memory program (97 lines of code) completely abandoned vector databases, relying instead on a pure SQLite relational database. It designed a precise state normalization function and a rule-based weighted scoring system to achieve accurate matching of objects, locations, and states, demonstrating the strong dependence of logical planning tasks on structured memory.
LoCoMo (Long-form Dialogue): A complex hybrid system of 290 lines was evolved. This system utilized both a structured relational table with 7 fields to track entity relationships and a vector database to capture semantics. It also innovatively incorporated advanced retrieval strategies like ‘person-focused boost’ and source diversity constraints, exhibiting complex information management capabilities akin to a social expert.
Cross-experiments further confirmed that applying the optimal memory system evolved for one task to a different type of task resulted in a significant performance drop. This strongly supports the paper’s core thesis: there is no universal ‘general memory,’ and task-specific optimization is a necessary condition for achieving high-performance AI agents.
Impact and Outlook
The introduction of the M⋆ framework marks a significant shift in AI system design philosophy—from relying on large-scale general models to building highly specialized, architecturally optimized intelligent systems. This approach not only provides a new path for enhancing AI capabilities in professional domains but also reveals the importance of architectural innovation in AI development, potentially becoming a key step towards achieving higher levels of artificial intelligence in the future. We might expect that in the future, every AI application will have its own unique ‘digital brain,’ custom-built by itself for its specific tasks.