Manuscript Offprint

arXiv:2610.38980

Context Compaction for World Models

Hayder Tirmazi

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

ABSTRACT

World models let observers explore and interact with generated worlds. Recent work shows that when observers revisit a region, current models often fail to regenerate it consistently. We view this as a context retrieval problem. LLM agents manage context retrieval at two layers of their stack. At the model layer, the KV cache design determines how much context fits in memory. At the agent harness layer, harnesses such as Claude Code pick the context sent to the LLM. Prior work on world models has mostly focused only on the model layer. In this paper, we make three contributions. First, we build a benchmark to measure world model context retrieval. It generates new worlds for every episode, preventing a model from recalling rooms using training data. It also separates context retrieval from guesses based on nearby frames. Second, we build an agent harness that performs context compaction for world models, which to our knowledge no prior work has studied. Third, we use our benchmark to study four world model designs. Context compaction lets our world models retrieve rooms up to 4 times beyond their context window without retraining. However, world models with sliding-window attention lose most of their context retrieval when dreaming. Therefore, a dreaming world agent benefits more from extending the reach of its world model using context compaction than switching to sliding window attention at the model layer.

1 INTRODUCTION

World models are systems that can predict a given environment from actions and observations. World models are useful in the realm of robotics and physical AI because they allow an RL agent to learn from a world model's imagined environment and then use that training to act in the environment (Ha & Schmidhuber, 2018; Hafner et al., 2020). In addition to agents, people can also explore and play inside the vast and imaginative worlds constructed by world models (Bruce et al., 2024) making way for a plethora of opportunities in the entertainment and video game industry (Team et al., 2026).

When an observer explores and interacts with a world model's generated world for an extended period of time, they often return to places they visited earlier on in their trajectory. A realistic world model is expected to generate the same output for a given place on a revisit as it did on the first visit. Unfortunately, two recent evaluations (Lian et al., 2025; Ye et al., 2026) demonstrate that world models do not currently maintain good consistency when observers revisit the same area in the generated world.

We view this as a context retrieval problem, similar to how LLMs are expected to maintain the context of a user's con-

versation during long sessions. For LLMs, this is a well-explored area of research. Context in LLMs is typically managed at two layers of the inference stack: the model layer and the agent harness layer. At the model layer, there is a subtle tradeoff between the size of the KV cache and an LLM's context retrieval. If an LLM designer reduces the number of bytes of KV cache required per LLM token, it enables a fixed amount of GPU memory to fit a longer context. On the other hand, reducing the size of the KV cache per token can also reduce how accurately an LLM retrieves previous tokens in its context.

There are many optimizations that reduce the size of an LLM's KV cache. Multi-query attention (Shazeer, 2019) and Grouped-query attention (Ainslie et al., 2023) share an LLM's keys and values across multiple attention heads. A Decoder-Decoder model (DD) (Sun et al., 2024) computes an LLM's keys and values once for a token and shares them across all of the LLM's upper layers. Sliding Window Attention (SWA) (Beltagy et al., 2020) lets each of an LLM's tokens only attend to a fixed window of tokens preceding it. In addition to the KV cache, some model architectures also focus on reducing the compute necessary for the prefill stage in inference. For example, recently, the Causal Encoder-Decoder (CED) introduced by DeepSeek-V4.1-Flash (DeepSeek-AI, 2026) lets an LLM project the keys and values for each upper layer from the hidden state of the middle layer. Note that the key and value weight matrices still remain separate for each upper layer in a CED, hence the KV cache in a CED is not smaller than the KV

¹Boston University, Boston, Massachusetts, USA ²AllSpice Inc., Boston, Massachusetts, USA. Correspondence to: Hayder Tirmazi hayder@bu.edu.

Figure

Figure 1. Left: Impact of model architecture on context retrieval for our video game room generation world model. Decoder-only needs 28.3 MB of KV cache memory for a context window of 128 steps. In the same memory, DD fits a context of 1,020 steps. Middle: Context retrieval gain of each world model when it dreams (with context compaction enabled). DD, which saves its memory with SWA, loses most of its context retrieval. Right: Agent harness layer can improve context retrieval on top of model layer changes. Decoder-only retrieves a room revisited after 262 steps. The visit from 262 steps ago is no longer in Decoder-only's 128-step context window. The model therefore cannot reproduce the room. However, context compaction allows the original visit to still be sent to the model.

cache in a standard LLM. However, since the keys and values in the upper layers depend only on the middle layer, CED can fill the global KV caches of its upper layers during the prefill stage without running the upper layers on the full input prompt.

At the agent harness layer, a harness tries to manage the context of an LLM through a process called context compaction (Tirmazi et al., 2026). Most commonly, such as in Claude Code and OpenCode, context compaction involves creating an LLM-generated summary of the agent's session (Tirmazi et al., 2026). More recently, Codex with GPT-6 Astra can experimentally keep notes across context windows in place of a single summary. Earlier context windows stay searchable, allowing the model to retrieve details from previous messages and tool outputs when it needs them (OpenAI, 2026).

Unlike LLMs, the problem of context retrieval in world models has not been adequately studied. The vast majority of prior work is limited in scope and focuses primarily on the model layer. Prior work explores how a world model's architecture or its KV cache can be modified for better context retrieval. WorldMem (Xiao et al., 2025) is a video world model that stores previously generated frames into a memory bank and retrieves them back into its diffusion transformer blocks during inference. Similarly, WorldPack (Oshima et al., 2026) attempts to pack previously generated frames into fewer tokens allowing more frames to be fit into the context window during inference. WorldKV (Yi et al., 2026) keeps and retrieve KV-cache chunks that are evicted by a sliding window. Finally, CaR (Peng et al., 2026) learns a compression network to improve context retrieval.

All of the above papers evaluate their design by checking

whether a frame that an observer revisits looks similar to the frame that the world model generated on the observer's original visit. However, this method of evaluating the model design does not completely answer the isolated question of whether a world model is good at context retrieval. When a world model generates a frame on an observer's revisit that is similar to the original frame, it can be due to three distinct reasons.

  1. The world model retrieved the original frame from its context.

  2. The world model reconstructed a frame similar to the original frame from its original training data that the model memorized.

  3. The world model inferred a frame similar to the original frame using context clues from other recent frames that are in its context.

In this paper, we call the latter two cases context reconstruction and context inference respectively, to distinguish them from context retrieval. Since most evaluations of prior work score only the output of the world model, they cannot separate the three cases. The closest prior work to our approach is a recent study that replaces the memory that a world model reads with memory from elsewhere (such as another scene) and measures how much of the benefit of memory depends on its content (Tiwari et al., 2026). However, they evaluate existing world models on existing datasets and do not control whether those scenes appear in the training data. Therefore, the study still cannot rule out context reconstruction.

When it comes to the agent harness layer, we are not aware of any prior work that uses an agent harness to manage the context of a world model similar to how existing LLM harnesses such as Claude Code and Codex manage the context of an LLM. The closest prior works we were able to find are WorldPlay (Sun et al., 2025) and Echo-Memory (King et al., 2026). While WorldPlay and the context-based variants of Echo-Memory use a retriever outside of the model to choose frames for a world model, they require the world models to be trained using the frames chosen by the external retriever. In other words, they are not agent harness layer optimizations in the same sense as context compaction in LLM agents. Claude Code, for example, manages the context of an existing Anthropic LLM such as Claude Opus without having to change the LLM's weights or re-train it. World in World (Song et al., 2026) retrieves earlier generated states for a frozen world model without training, but it does so inside the model, by restoring their KV caches and by rescaling attention during generation. Our agent harness leaves the computation of the world model unchanged and only decides which context the world model reads.

In this paper, we conduct a thorough study of the problem of context retrieval in world models. We train research world models of about 14M parameters that generate video game rooms and then use them to investigate how an array of systems decisions across the model layer and agent harness layer impact context retrieval. Concretely, we make the following three contributions.

  1. Create a benchmark for world models that specifically evaluates a world model's context retrieval. Unlike prior work, our benchmark separates context retrieval from context reconstruction and context inference.

  2. Introduce an agent harness for world models that can perform context compaction. Our agent harness manages the context of an underlying world model without having to retrain or modify its weights. Our approach is similar to how agent harnesses for LLMs such as Claude Code, Codex, and OpenCode work.

  3. Conduct an evaluation of the effect of systems decisions at the model layer and agent harness layer (as well as their combinations) on context retrieval in world models, both when the world model reads true frames and when it dreams.

We vary the model architecture and the agent harness independently and measure context retrieval on each configuration. Note that the two layers interact with each other and both play a part in a world model's ability to do accurate context retrieval. For example, a good context compaction algorithm at the agent harness layer can extend the reach of a world model's context retrieval far beyond the model's context window. Similarly, the design of the KV cache at the model layer significantly impacts the size of the model's context window assuming the available memory is fixed. Figure 1 shows both effects. In practice, a world agent dreams, i.e., its world model predicts new frames from frames that it generated earlier. Once our world agents dream, world models whose lower blocks use SWA lose almost all of their context retrieval, with or without context compaction, as we show in the middle plot of Figure 1.

References (44)