GrepMem: Forget Freely, Grep Later

Lossless, Index-Free Memory for Long-Horizon LLM Agents

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.

Ziyan Yang1*Taiwei Shi1*Jieyu Zhao1

1University of Southern California

*Equal contribution

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.

  • 1. Lossless memory without an index.

    We introduce GrepMem, which pairs lossy context summarization with a lossless trajectory log preserving the full interaction history, enabling the agent to recover omitted details through a single search tool, grep_history (Figure 2).

  • 2. Pareto-optimal accuracy–token-cost tradeoffs.

    We compare five context management strategies before and after RL training across three complementary settings. GrepMem lies on the accuracy–token-cost Pareto frontier across all five evaluation tasks, achieving higher accuracy than summarization-based baselines at competitive token costs (Figures 1 and 5, Table 3).

  • 3. Scalable and accurate recall over long histories.

    GrepMem maintains high accuracy on ContextBench’s KV Store and Log Triage tasks (Table 2) even as input streams grow far beyond the model’s context window, enabling accurate and scalable long-horizon recall without complex memory infrastructure (Figure 4).

Mean accuracy vs. token cost after RL

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 summarization, while Masking discards older tool outputs to free context space. MemGPT maintains editable working memory, archival memory, and searchable message history. Our method, GrepMem combines summarization with a lossless, searchable history log, enabling on-demand retrieval through grep without a retrieval index. Token usage includes prompt and generated tokens across all model calls. Figure 5 reports per-task results.

Method

We introduce GrepMem, which separates the agent’s bounded working context from its persistent history (Figure 2). We build on the summarization loop of SUPO (Lu et al., 2026), a reinforcement learning (RL) algorithm that jointly optimizes tool use and context summarization. When the context reaches a length threshold, the agent summarizes its interaction history and resumes from the initial prompt and the summary. GrepMem extends this loop by automatically appending every message and tool output to a persistent plain-text log. After the first compaction, the agent can retrieve earlier observations through a single tool, grep_history, which searches the log using regular expressions and supports both retrieving and counting matching lines. In GrepMem, summaries provide context for ongoing reasoning, while the lossless log preserves all details omitted during compaction. No embeddings, retrieval index, memory extraction or additional models are required.

GrepMem overview

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.

Getting ready…

(a) Agent Loop

LLM Inference → Agent Response / Tool Calls, repeat

LLM InferenceAgent ResponseTool Calls: grep_history

(b) Context Compaction

Active Context usage · 0% · dashed line = Compaction Limit

    (c) Grep Retrieval

    grep disabled · available after the first compaction

    grep_history(pattern=…)

    (d) Full History Log

    Lossless & Index-free, append-only

      MethodHow it worksScales past the windowKeeps everythingRecallNo index
      ReAct (Yao et al., 2023)keeps all, stops when full✗✓✗✓
      Masking (Lindenbauer et al., 2025)hides old tool outputs✓✗✗✓
      SUPO (Lu et al., 2026)summarizes and restarts✓✗✗✓
      MemGPT (Packer et al., 2024)summarizes, manages memory✓✓✓✗
      GrepMem (ours)summarizes, greps raw log✓✓✓✓

      Table 1: Comparison of Context Management Strategies. We compare how five strategies manage context, preserve interaction history, and support retrieval. GrepMem augments SUPO with an automatically appended plain-text history log and a grep tool, preserving the full history for retrieval without an index.

      Main Results

      We evaluate the model before and after RL training. OpenResearcher, BCP-Multi, and AMA-Bench answers are graded by gpt-5.6-sol (OpenAI, 2026) against references; ContextBench uses its official programmatic graders. We report 95% confidence intervals for accuracy. These intervals reflect variation across test items, not across training runs. We report total token cost across all model calls, including summarization and recall (Appendix E).

      Questions known before the inputQuestions revealed after the input
      StrategyBCP-MultiAMA-Bench fileAMA-Bench streamContextBench KVContextBench LogAvg.
      After RL
      ReAct0.238.326.60.00.013.0
      SUPO38.145.545.213.96.629.8
      Masking34.844.544.920.76.630.3
      MemGPT39.946.445.499.950.056.3
      GrepMem46.946.346.197.495.366.4
      Before RL
      ReAct0.022.426.20.00.09.7
      SUPO45.245.541.710.96.630.0
      Masking18.642.342.113.35.524.4
      MemGPT34.445.446.195.848.954.1
      GrepMem40.946.945.893.975.060.5

      Table 3: Accuracy Before and After RL Training. We compare the accuracy (%) of five context management strategies using Qwen3.5-9B before and after training with the same RL recipe (§4). Within each block, bold marks the best value in each column, and underlining marks the second best. For ContextBench, we report exact-value accuracy on KV Store and exact-answer accuracy on Log Triage. Avg. denotes the mean accuracy across five tasks.

      Evaluation performance during RL training

      Figure 3: Evaluation Performance of Five Context Management Strategies During RL Training. Scores are averaged over the evaluation samples available at each checkpoint. Evaluation details are provided in Appendix E.

      ContextBench performance across stream lengths

      Figure 4: ContextBench Performance Across Stream Lengths. We evaluate how stream length affects ContextBench performance before (dashed) and after RL (solid) training.

      Conclusion

      We introduce GrepMem, a simple, lossless, and index-free memory mechanism that combines context summarization with a complete, searchable interaction history. Our results demonstrate that GrepMem supports accurate long-horizon recall, scales effectively to increasingly long input streams, and lies on the accuracy–token-cost Pareto frontier across all five evaluation tasks. Comparisons before and after RL training further show that RL can improve how agents retrieve and utilize historical information, with benefits varying across tasks and context management strategies.

      Our work reexamines the boundary between short-term context management and long-term memory in LLM agents. What should an agent forget when it can remember everything, and how much complexity does remembering truly require? By preserving complete histories alongside compact working contexts, GrepMem suggests that lossless preservation and lossy compaction can serve complementary purposes. Looking ahead, we envision GrepMem as a simple but competitive baseline for evaluating future methods, inspiring principled memory designs for reliable long-running agents.

      BibTeX

      @misc{yang2026forgetfreelygreplater,
            title={Forget Freely, Grep Later: Lossless, Index-Free Memory for Long-Horizon LLM Agents}, 
            author={Ziyan Yang and Taiwei Shi and Jieyu Zhao},
            year={2026},
            eprint={2611.25123},
            archivePrefix={arXiv},
            primaryClass={cs.CL},
            url={https://arxiv.org/abs/2611.25123}, 
      }