TL;DR
GrepMem is a simple, lossless, and index-free memory mechanism for LLM agents that replaces complex vector databases with a plain-text log and a regular expression search tool (grep). By training agents to search their own history, GrepMem achieves near-perfect recall (95–97%) on long-horizon tasks while remaining significantly more token-efficient than sophisticated memory systems like MemGPT.
What Problem Does This Paper Address?
As LLM agents tackle increasingly complex, "long-horizon" tasks—like deep research or multi-step coding—their interaction histories quickly outgrow their context windows. When the window fills up, something has to go.
Current solutions generally fall into three buckets:
- Summarization: Condensing history into a shorter text. This is inherently "lossy"; a detail that seems irrelevant now might be the key to a question asked 50 steps later.
- Masking: Simply deleting old tool outputs to make room.
- Structured Memory (RAG): Using vector databases and embeddings to retrieve "relevant" chunks. While powerful, these systems are complex to manage, require extra models for embeddings, and can struggle with retrieving exact identifiers or specific log lines.
The authors identify a critical gap: How can an agent maintain a compact working context without permanently losing the "long tail" of verbatim details?
Methods
The researchers propose GrepMem, a minimalist architecture that separates reasoning from storage. It consists of two main parts:
- A Lossless, Append-Only Log: Every message, tool call, and result is automatically appended to a persistent plain-text file. Nothing is ever truly deleted.
- The
grep_historyTool: When the agent's active context reaches a limit, it performs "Context Compaction"—summarizing the history to keep the "vibe" of the task alive in the prompt. However, if it needs a specific detail later, it can call a tool to search the full log using regular expressions (regex).
Crucially, the authors don't just give the agent a tool; they use Reinforcement Learning (RL) (specifically the GRPO algorithm) to train a Qwen3.5-9B model. This teaches the agent when it’s worth spending tokens to search the log and how to write effective regex queries to find what it needs.
Datasets
The model was evaluated across five diverse tasks designed to test different memory requirements:
- BCP-Multi: A multi-question research benchmark where the agent must retain findings from early searches to answer later questions.
- AMA-Bench (File & Stream): Tests the ability to answer questions about long agent trajectories.
- ContextBench (KV Store & Log Triage): Synthetic tasks requiring the retrieval of exact key-value pairs or counting specific entries in massive service logs (up to ~1M tokens).
Key Insights and Ideas
- Verbatim is King: For many agentic tasks, "semantic similarity" (the heart of vector RAG) is less useful than "exact match." If you need a specific UUID or a timestamp from a log, a simple regex search is more reliable than a vector search that might return a "similar" but incorrect entry.
- RL Bridges the Gap: A "grep" tool is only as good as the query. The "aha moment" here is that RL can effectively teach an agent to treat its own history as a searchable database, learning to formulate precise queries to recover details it previously "forgot" during compaction.
- The Pareto Frontier: GrepMem isn't just better; it's more efficient. It consistently sits on the "Pareto frontier," meaning no other tested method was both more accurate and cheaper in terms of token usage.
Key Results
The quantitative results are striking, particularly on tasks requiring high precision:
- Near-Perfect Recall: On ContextBench, GrepMem achieved 95–97% accuracy, while lossy baselines (summarization and masking) plummeted to at most 21%.
- Outperforming Complex Systems: On the BCP-Multi research task, GrepMem outperformed the summarization-based SUPO by 8.8 percentage points and the more complex MemGPT by 7.0 points.
- Token Efficiency: Despite the cost of calling a search tool, GrepMem is remarkably lean. On ContextBench, it used only 29% as many tokens as MemGPT while delivering substantially higher accuracy.
As shown in the scaling analysis, GrepMem's performance remains stable even as the input stream grows to 30x the model's context window, whereas summarization-only methods (like SUPO) degrade rapidly.
Limitations and Future Work
- Lexical vs. Semantic: GrepMem relies on lexical search. If an agent needs to find a concept that was discussed but doesn't remember the specific words used, regex might fail where vector search would succeed.
- Search Overhead: While generally efficient, frequent or overly broad grep calls can still incur significant token costs.
- Selective Retrieval: The authors suggest future work should focus on helping agents determine when simple, lossless retrieval is most valuable to avoid unnecessary overhead.
- Generalization: The study focused on a single model (Qwen3.5-9B); testing how this strategy scales to smaller or larger models remains an open question.
Summary updated