Manuscript Offprint

arXiv:2609.04852

KVMem: Virtualizing Million-Token Agent Workspaces on a Consumer GPU

Di Chai, Leye Wang, Zeshen Su, Zhiguo Xia, Zhihang Yu

Abstract

Modern LLM agents operate in persistent workspaces whose accumulated history can exceed both GPU KV capacity and the model's native context window. Existing systems typically compact older context into summaries or retrieve it later as text, either losing fine-grained execution evidence or repeatedly prefilling content that the model has already processed. We present KVMEM 1^{1}, a KV-context virtualization system that preserves overflowed workspace history as paged KV state across GPU memory, host memory, and NVMe. KVMEM uses lightweight, model-native attention-space indexes to select relevant historical blocks and materializes a query-dependent execution view bounded by the model's native context window. Extensive evaluations on long-context agent benchmarks spanning histories up to one million tokens, including LongMemEval, MemoryAgentBench, and AgentLongBench, show that KVMEM generally achieves higher task utility and greater inference efficiency than compaction-based approaches, the de facto standard for handling context overflow. In the DeepSWE long-context test with Qwen3.8-27B, KVMEM improves task success from 43.8% with compaction-only context management to 48.4%.

In our local-deployment evaluation, KVMEM runs Qwen3.6/3.8-27B NVFP4 with MTP on an off-the-shelf laptop equipped with a 24 GB RTX 5090 Laptop GPU, virtualizing agent workspaces of up to 1M tokens—four times the model's native 256K-token context window. In a single-session setting, KVMEM generates ~50 tokens/s, providing interactive responsiveness for local agent execution. More broadly, by decoupling addressable workspace size from the LLM's native context window, KVMEM provides a practical path toward long-running agents whose workspaces can grow beyond that window.

1 Introduction

Modern LLM agents increasingly work inside persistent task workspaces, such as project folders, software worktrees, or notebook directories. A workspace is more than a long conversation transcript. It is the task-local environment in which an agent repeatedly observes state, invokes tools, reads and edits artifacts, incorporates feedback, and accumulates intermediate results. Consequently, a long-running agent builds up a large amount of model-relevant working context.

This growing workspace context faces two distinct execution limits. First, even when the accumulated history remains within the model's native context window, retaining all of its KV state on GPU may exceed the memory available to a single agent session. Model weights, runtime buffers, speculative-decoding state, and KV cache compete for the same limited GPU capacity, making the practically usable context substantially smaller than the model's advertised maximum window on commodity hardware. Second, as execution continues, the accumulated workspace may itself grow beyond the model's native context window. For example, Qwen3.6-27B supports a native context window of 256K tokens 2^{2}, whereas long-context agent benchmarks already contain interaction trajectories ranging from hundreds of thousands to several million tokens [6]. Once either limit is reached, some previously processed context must leave the active model input, even though its exact contents may still be needed later.

Existing agent systems generally handle this overflow by reducing the amount of historical text presented to the model. The most common mechanism is compaction, which replaces older execution history with shorter summaries. Systems may additionally retain the original history in an external text store and retrieve selected snippets when the compact representation is insufficient [4, 10, 13, 19]. Although these mechanisms differ in how memory is summarized, indexed, and recalled, they share a text-centric abstraction: once workspace history leaves the active context, it is retained and later recovered primarily as text.

This abstraction creates a coupled fidelity–efficiency problem. Compaction must decide what information to preserve before future agent steps reveal which details will matter. It may therefore omit a sentence from an earlier file, a value from a tool output, or a user constraint that later becomes task-critical. Text retrieval can recover some omitted evidence, but every retrieved token must be processed through prefill again, even though the same content was already processed earlier in the workspace. Text-centric overflow handling not only approximates the historical workspace state, but also discards the reusable KV computation associated with that state.

The loss of reusable KV state, however, is not inherent to context overflow. By the time historical context leaves the active execution window, the model has already encoded it into KV state; the only uncertainty is which subset will be needed again. Drawing inspiration from virtual memory, our key insight is to decouple the size of an agent's addressable KV workspace from the amount of KV state physically resident on the GPU. Overflowed workspace history can then be preserved as paged KV state in a larger backing store and selectively materialized when relevant. Under this abstraction,

1^{1}Source code: https://github.com/kvmem/kvmem-qw3

2^{2}https://huggingface.co/Qwen/Qwen3.6-27B the complete KV workspace need not fit in GPU memory at once. Instead, each agent step operates on a bounded, query-dependent execution view drawn from a much larger virtual workspace.

Based on this insight, we present KVMEM, a KV-context virtualization system for long-running LLM agents. KVMEM preserves previously processed workspace history as an addressable repository of KV blocks spanning GPU memory, host memory, and NVMe storage. At each agent step, it uses compact attention-space indexes derived from the serving model itself to select relevant historical KV blocks. It then materializes the selected blocks as a chronologically ordered and position-consistent execution view within the model's native context window and the available GPU KV budget. KVMEM therefore enables an agent to access a workspace much larger than its active context without reconstructing every recalled block as text and prefilling it again.

Making workspace-context virtualization practical requires addressing three challenges. First, the system must determine when historical context should be reconsidered. Retrieval that occurs too frequently adds ranking and working-set reconstruction overhead, whereas retrieval that occurs too infrequently may leave the model with stale context. Second, the system must determine what to recall from a workspace containing millions of historical tokens in an efficient way without scanning every stored KV tensor. Third, it must determine how to restore a sparse set of non-contiguous KV blocks efficiently and safely.

KVMEM addresses these challenges with three corresponding mechanisms. First, step-level memory scheduling updates the historical working set once per agent step, around agent-step boundaries where the model's historical attention is most likely to shift. Concretely, the update occurs after prefill and before decoding, and the selected working set remains fixed throughout decoding. Second, query-conditioned KV retrieval summarizes each historical block with a compact position-independent representation and ranks blocks using the serving model's own attention-space signals. Third, tiered KV management separates the persistent repository view from the GPU execution view. It proactively stages out completed blocks, reuses overlapping GPU pages across consecutive steps, keeps frequently recalled blocks in host memory, and rematerializes the remaining blocks through packed transfers and position-consistent re-RoPE. These mechanisms allow KVMEM to maintain a large virtual workspace while keeping each model invocation compact and executable on limited GPU memory.

KVMEM also makes million-token agent workspaces practical on commodity local hardware. On an off-the-shelf laptop equipped with a 24 GB RTX 5090 Laptop GPU, KVMEM runs Unsloth's NVFP4-MTP variant of Qwen3.6/3.8-27B and virtualizes a workspace containing up to 1M tokens—four times the model's native 256K-token context window. In a single-session setting, it sustains ~50 generated tokens per

second, providing interactive responsiveness for local agent execution. These results demonstrate that KVMEM not only reduces the cost of recalling historical context, but also enables substantially larger agent workspaces under limited GPU memory.

In summary, this paper makes three contributions:

  1. We identify accumulated agent context as a virtualizable systems resource and distinguish the addressable workspace from both the model-visible execution window and the GPU-resident KV set. This abstraction allows a long-running workspace to exceed the model's native context window without requiring the model to attend to the entire history in a single invocation.

  2. Based on the above abstraction, we present KVMEM, which preserves overflowed agent context as recoverable KV blocks across GPU memory, host memory, and NVMe. KVMEM combines step-level memory scheduling, model-native attention-space retrieval, hierarchical KV placement and reuse, and position-consistent rematerialization to construct a bounded execution view at each agent step.

  3. We evaluate KVMEM on three controlled long-context agent benchmarks and show that it generally delivers higher utility and substantially lower recovery overhead than compaction, the de facto standard for handling context overflow in current LLM agent systems. We further evaluate KVMEM during complete long-horizon software-engineering trajectories, i.e., DeepSWE, with Qwen3.8-27B, and demonstrate a 1M-token virtual workspace on a laptop with only 24 GB of GPU memory while sustaining approximately 50 tokens/s in single-session generation.

References (17)