Forget Freely, Grep Later: Lossless, Index-Free Memory for Long-Horizon LLM Agents

Ziyan Yang, Taiwei Shi, Jieyu ZhaoarXiv 2026

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:

  1. 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.
  2. Masking: Simply deleting old tool outputs to make room.
  3. 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_history Tool: 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).
Figure 2: GREPMEM Overview. (a) The basic agent loop generates responses and tool calls from its active context, while GREPMEM additionally appends these interactions and tool results to a persistent history log. (b) Context compaction generates a summary when the working context reaches the compaction limit, allowing the agent to resume execution with a compacted context. GREPMEM builds on SUPO’s summarization loop. Extending this framework, GREPMEM enables (c) on-demand retrieval through grep_history and (d) a lossless, append-only full history log containing user messages, tool calls, tool results, summaries, and agent responses. By combining context summarization with a log preserving every message and tool output verbatim, GREPMEM enables grep_history to retrieve matching log lines after the first compaction without embeddings, a retrieval index, or an additional model.

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.
Figure 1: Mean Accuracy vs. Token Cost After RL. We compare four context management strategies by their mean accuracy and token usage across five evaluation tasks. SUPO compresses context through summariza- tion, while Masking discards older tool outputs to free context space. MemGPT maintains editable working memory, archival memory, and searchable message his- tory. Our method, GREPMEM, combines summariza- tion with a lossless, searchable history log, enabling on-demand retrieval through grep without a retrieval in- dex. Token usage includes prompt and generated tokens across all model calls. Figure 5 reports per-task results.

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.

Performance across stream lengths. GrepMem (red) maintains high accuracy as stream length increases, while SUPO (blue) and Masking (green) fail.

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

Abstract

Long-horizon LLM agents often exceed their context windows, forcing them to discard information that may be needed later. Existing context management strategies rely on summarization, masking, or structured memory, requiring decisions about what to preserve or how to retrieve it before future needs are known. However, these methods can introduce lossy compression, potentially discarding details needed for future tasks. To address this limitation, we introduce GrepMem, a simple, lossless, and index-free memory mechanism built on two components: an append-only plain-text log of the agent's complete history and a regular-expression search tool for on-demand recall. Unlike structured memory systems, GrepMem requires no embeddings, retrieval index, memory extraction, or additional models. We train Qwen3.5-9B with reinforcement learning under five context management strategies and evaluate on multi-question research sessions, long-stream recall, and agent-trajectory question answering. GrepMem achieves 95-97% accuracy on ContextBench, versus at most 21% for lossy baselines, and outperforms SUPO's summarization by 8.8 percentage points on BCP-Multi. GrepMem scales effectively to increasingly long input streams while remaining on the accuracy-token-cost Pareto frontier across all five evaluation tasks. Our results show that a plain-text log and grep provide a simple but competitive approach to long-horizon agent memory, without requiring complex structured memory infrastructure.
BibTeX
@misc{yang2026forget,
  author = {Ziyan Yang and Taiwei Shi and Jieyu Zhao},
  title = {{Forget Freely, Grep Later: Lossless, Index-Free Memory for Long-Horizon LLM Agents}},
  year = {2026},
  eprint = {2611.25123},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2611.25123},
}
Figure