Context as an Environment: Programmatic Context Management for Long-Horizon Agents

Yin Lin, Elaine Ang, Erkang Zhu, Bolin Ding, Jingren ZhouarXiv 2026

TL;DR

Scroll is a novel context manager that treats an LLM agent's session history as an executable environment rather than a static text prompt. By offloading history to an append-only log and a persistent Python kernel, Scroll allows agents to programmatically search, filter, and manipulate their own past interactions using code. This approach achieves state-of-the-art performance on long-horizon benchmarks, including a 37.4 percentage point boost on complex reasoning tasks.

What Problem Does This Paper Address?

As LLM agents are deployed for complex, long-running tasks like repository-level software engineering or deep web research, their interaction histories quickly explode. Standard LLM context windows are bounded, and even when a model can accept a massive prompt, its retrieval and reasoning capabilities degrade as the input length grows.

Existing solutions typically rely on context compression (truncating or summarizing old turns) or external memory (extracting facts into vector databases). The authors argue that both approaches are fundamentally lossy. They force the system to decide what information is worth keeping at the moment it is generated, long before the agent knows what specific details future steps will actually require.

Methods

Instead of serializing the entire history into the prompt at every turn, Scroll introduces a Session Environment that lives completely outside the model's context window.

Figure 1: Overview of Scroll. Scroll keeps the full session in a persistent, executable Session Environment; model-written code retrieves and computes over it via exec, and print selects the working view exposed to the next model call.

The architecture consists of three core components:

  1. Append-only Event Log: A durable, ground-truth database (implemented via SQLite) that stores every user message, model response, and tool output with stable sequence addresses (seq).
  2. Persistent Python Kernel: A sandboxed environment that survives across model calls. Instead of dumping massive tool outputs into the prompt, variables (like large DataFrames) are bound directly within the kernel's namespace.
  3. Programmatic Context Construction: Context management is transformed into a coding task. The agent executes code (exec) to interact with its history using a specialized memory surface object (ms). It can locate records via BM25 search, materialize exact spans, and compute over variables. Crucially, only data explicitly emitted via print() enters the model's "working view" for the next turn.
Figure 2: Programmatic context construction. Three exec turns compute over resident state in the kernel; only print output crosses into the next model context/working view.

When the working view approaches its token budget, Scroll evicts older spans from the prompt. However, it maintains a tiered eviction index—a compact map of position-based landmarks—ensuring the agent remains aware of what it can no longer see and can navigate directly back to those exact addresses if needed.

Datasets

The authors evaluated Scroll across three distinct long-context benchmarks:

  • LongMemEval (S and M splits): Evaluates long-term memory retrieval and reasoning over extensive user-assistant conversation histories.
  • BEAM (up to 10M tokens): Tests an agent's ability to track changes over time, resolve contradictions, and aggregate facts across massive, coherent histories.
  • LOCA (128K and 256K regimes): Measures an agent's capacity to reason and act continuously as its environment description grows over a long tool-use trajectory.

Key Insights and Ideas

  • Context as Code: The core "aha moment" is leveraging the rapidly improving coding proficiency of modern LLMs to manage their own memory. Writing a short Python script to filter a database is a much more robust way to handle scale than relying on an LLM to find a needle in a 10-million-token text haystack.
  • Deferred Selection: By keeping a lossless, verbatim Event Log outside the prompt, Scroll defers the choice of what is relevant to query time.
  • In-Kernel Computation: Keeping intermediate states and raw tool outputs as resident variables in a persistent Python kernel drastically reduces token overhead. Agents can perform complex joins, filters, and aggregations without ever forcing the raw data to cross the observation boundary into the prompt.

Key Results

Using Qwen3.8-Max as the backbone, Scroll achieved remarkable gains over existing memory systems and long-horizon agents:

  • BEAM10M: Scroll achieved 73.1%, outperforming the best published memory system (Exabase M-1) by 5.1 points.
  • LOCA256K: Scroll reached 86.7%, smashing the previous state-of-the-art long-horizon agent configuration by 37.4 points.
  • Ablation Impact: Discarding the original records in favor of lossy summarization caused performance on BEAM10M to plummet to a meager 19.9%. Removing the persistent REPL kernel (Scroll w/o REPL) cost 7.3 points, underscoring the necessity of computing over variables rather than serializing text.

Limitations and Future Work

Despite its success, Scroll's performance is tightly coupled with the coding capability of the underlying foundation model. When testing weaker backbones (like a 35B open-weight model) on the complex LOCA benchmark, performance dropped significantly due to execution errors and poor multi-step query planning.

Additionally, Scroll slightly underperforms ingestion-heavy pipelines on tasks like global summarization. Because Scroll stores raw events, it must reconstruct condensed views from scratch per query, making it susceptible to query formulation errors.

The authors suggest that future work should focus on policy distillation—using successful context-retrieval and injection traces from frontier models to fine-tune smaller, more efficient models to use the Scroll environment effectively.

Summary updated

BibTeX
@misc{lin2026context,
  author = {Yin Lin and Elaine Ang and Erkang Zhu and Bolin Ding and Jingren Zhou},
  title = {{Context as an Environment: Programmatic Context Management for Long-Horizon Agents}},
  year = {2026},
  eprint = {2608.21690},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2608.21690},
}
Figure