TL;DR
KV-streams is a plug-and-play strategy for training LLM agents that eliminates the massive "prefill tax" associated with context compaction. By streaming the Key-Value (KV) cache forward instead of flushing and re-processing it, the authors achieve up to a 5× training speedup while enabling the KV cache to act as a "pseudo-recurrent" state that retains information even after it has been evicted from the explicit context.
What Problem Does This Paper Address?
Scaling Large Language Model (LLM) agents to long-horizon tasks—like complex software engineering or multi-turn games—is fundamentally limited by GPU memory. As the conversation history grows, the KV cache (the model's "memory" of past tokens) eventually exceeds the GPU's capacity.
To fix this, researchers use context compaction: they summarize old turns or simply delete them (e.g., using a sliding window) to keep the memory footprint constant. However, standard compaction methods suffer from a hidden "prefill tax." Every time the context is compacted, the inference engine flushes the KV cache and re-processes the retained tokens to rebuild it. This redundant computation makes training incredibly slow and compute-expensive, especially when you want to keep a large portion of the recent context.
Methods
The authors propose KV-streams, a drop-in modification for LLM inference and training pipelines.
- Streaming vs. Flushing: Instead of rebuilding the context from scratch after compaction, KV-streams evicts specific entries directly from the KV cache in the inference engine. This maintains a single, continuous stream of KVs.
- Custom Attention Masking: On the training side, rather than creating separate training traces for each compaction step, KV-streams uses a custom attention mask. This mask blocks attention to deleted spans within a single forward pass, perfectly replicating the eviction pattern without re-processing a single token.
- Compatibility: The authors tested this with four common compaction strategies: Summary (replacing old turns with a summary), Markovian Thinker (deleting the oldest half), Markovian Pick (letting the model choose what to keep), and Sliding Window (keeping only the most recent tokens).
Datasets
The researchers evaluated KV-streams across a diverse set of long-horizon benchmarks:
- TextWorld: Procedurally generated text-based adventure games.
- ALFWorld: A household simulator where agents complete tasks via text commands.
- SWE-bench Verified: A challenging benchmark requiring agents to fix real-world GitHub issues in Python.
- Transfer Tasks: To test generalization, they evaluated models on ScienceWorld, Jericho, and DiscoveryWorld.
Key Insights and Ideas
- Eliminating the "Prefill Tax": Standard compaction costs grow exponentially as the retained context approaches the memory budget. KV-streams brings this extra token cost down to zero (see Figure 3).
- Emergent Pseudo-Recurrence: The most striking finding is that the streamed KV cache acts as a recurrent state. Even when a specific piece of information (like a fruit name in a synthetic task) is evicted from the explicit context, the model can still "remember" it because the information was encoded into the KV entries of subsequent tokens that were not evicted.
- RL is All You Need: Prior research suggested that this "pseudo-recurrence" required a Supervised Fine-Tuning (SFT) stage to emerge. This paper proves that Reinforcement Learning (RL) alone is sufficient for the model to learn how to leak and retrieve information through the KV cache, significantly simplifying the training pipeline.
Key Results
- Massive Speedups: KV-streams achieved a 5× wall-clock speedup on TextWorld and a 3× speedup on SWE-bench Verified compared to full-context training.
- Performance Parity: Despite the speedup, models trained with KV-streams matched or even exceeded the success rates of full-context and re-prefill baselines. On ALFWorld, KV-streams reached the same final success rate as re-prefill compaction at a fraction of the compute.
- Better Generalization: Models trained with KV-streams generalized better to unseen text-based games than those trained with full context, likely because compaction forces the model to focus on the most relevant information.
Limitations and Future Work
- Turn-Wise vs. Token-Wise: The authors found that evicting individual tokens (rather than whole turns) led to degenerate text in early experiments. They stuck to turn-wise compaction for stability, but suggest that finer-grained token-level eviction is a promising future direction.
- Inference Engine Overhead: While KV-streams is faster, current engines like vLLM use 16-token blocks, requiring some padding that adds slight overhead. Optimizing inference engines for single-token block sizes (as seen in their SGLang experiments) would further maximize efficiency.
- Scaling: Future work could explore how these findings scale to even larger models (e.g., 70B+ parameters) and even longer context horizons.
Summary updated