An agent's KV cache during a tool call on one RTX 4090: restore it from host memory, or recompute it?
A coding agent alternates model turns with tool calls. While a tool runs, the session’s KV cache sits on the GPU doing nothing, and on a 24 GB card other sessions need that memory. If the cache is evicted, the next turn starts from scratch. vLLM ships two connectors that keep a copy in host memory so the cache can be restored instead of recomputed.
I measured what each choice costs on one RTX 4090 with Qwen2.5-7B-Instruct and an unmodified vLLM v0.30.0: the time to the first token of the turn after the tool call, when the session’s cache was kept on the GPU, restored from host memory, or recomputed.
The short answer: at 28,672 tokens, recomputing takes 3.8 s and restoring takes 0.34 s, 9% of that. But vLLM’s offloading connector by default stores only prompt blocks, so the turn the model just wrote is recomputed anyway, which adds 38 to 58 ms. The copy from host memory runs at 9.8 GB/s, and its time adds to, rather than overlaps with, what a kept session needs. The other connector, in its lazy mode, crashes the engine on this machine’s driver.
The setup
- One RTX 4090 (24 GB) in a two-socket Xeon Gold 5320 server, NVIDIA driver 550.144.03.
- vLLM v0.30.0 (CUDA 12.9 build), all defaults except the KV connector. Qwen2.5-7B-Instruct in bf16: 57,344 bytes of KV cache per token, and a GPU KV pool of 118,080 tokens (6.77 GB).
- A session has two turns. Turn 1 is a prompt of N - 768 tokens, and the model writes 512 tokens. Then, standing in for the tool call, other prompts are sent until together they need 1.5 times the KV pool, which evicts the session from the GPU. Two seconds later the resume request arrives: turn 1, the 512 generated tokens and a 256-token tool result, N tokens in all, sent as token ids. N is 2,048, 8,192 and 28,672.
- What is measured: the client’s time to the first streamed token of the resume request, and, from the server’s counters, how many of its tokens came from the GPU cache, from host memory or were recomputed. One request is in flight at a time.
- Five rounds, four sessions per configuration and length in each, the configurations in a fresh random order every round. A round counts only if no other process used the GPU and other processes used at most 2% of each CPU socket.
The configurations differ only in --kv-transfer-config:
| Configuration | What happens to the evicted session |
|---|---|
| kept (no eviction) | its cache is still on the GPU |
| recomputed | vLLM’s default: the cache is gone and the whole context is computed again |
| restored, prompt only | OffloadingConnector with a 16 GiB pinned host buffer, as shipped: it stores prompt blocks as they are computed (offload_prompt_only: true) |
| restored, prompt and turn | the same with "offload_prompt_only": false: generated blocks are stored too |
| lazy | SimpleCPUOffloadConnector with "lazy_offload": true: blocks are copied to host only when they are about to be evicted |
The counters confirm each configuration did what its name says, in every session: the kept session recomputes 272 tokens (the tool result plus the last partial block), the recomputed one all N, the prompt-only restore 768 and the full restore 272, with the rest coming from host memory.
Restoring beats recomputing at every length
| Tokens | kept | restored, prompt and turn | restored, prompt only | recomputed |
|---|---|---|---|---|
| 2,048 | 53 ms | 63 ms (0.31) | 100 ms (0.50) | 203 ms |
| 8,192 | 82 ms | 127 ms (0.15) | 170 ms (0.20) | 844 ms |
| 28,672 | 177 ms | 341 ms (0.09) | 399 ms (0.11) | 3,795 ms |
Medians over 20 sessions; in brackets, the time as a fraction of recomputing. Computed round by round, the fractions stay within 0.003 of each other for the full restore and within 0.02 for the prompt-only one.
Recomputing grows faster than linearly with the context, because attention over a longer prefix costs more per token, while restoring grows with the bytes copied. So the longer the session, the bigger the win: from 3x at 2K tokens to 11x at 28K.
Where the restore time goes
vLLM times its own host-to-GPU copies (vllm:kv_offload_load_time), and the counters were saved
before and after every resume request, so the copy can be separated from the rest:
- The copy runs at 9.8 GB/s at every length: 10.4, 46.3 and 167.3 ms for 0.10, 0.45 and 1.63 GB in the full restore (9.7 to 9.9 GB/s in each of the 120 restoring sessions). It is a rate, not a fixed cost, and it is 39% of the 25 GB/s a single large pinned copy from host memory to one GPU reaches on this machine (an earlier post).
- Why 9.8 and not 25 GB/s: the copy goes one 32 KB block at a time. The connector keeps one
host buffer per layer (the server logs
Allocating 28 CPU tensors), so the unit it copies is one 16-token block of one layer: 16 x 2,048 bytes = 32 KB. vLLM hands the whole list to one batched copy,cuMemcpyBatchAsync, which exists only from CUDA 12.8; on this driver (12.4) the lookup fails and vLLM falls back to onecudaMemcpyAsyncper block (cache_kernels.cu). Restoring 28,400 tokens is then 1,775 blocks x 28 layers = 49,700 copies, and each takes 3.37 µs (3.33 and 3.34 µs at the two shorter lengths). At 25 GB/s, 32 KB needs 1.3 µs; the other 2 µs of every copy is the cost of issuing and finishing it on its own. On a driver with CUDA 12.8 or later the same list goes out as one batch. - The copy is not hidden behind computation. The time of a kept session plus the copy time is 63, 128 and 345 ms; the measured restores take 63, 127 and 341 ms. A restored session pays for the copy and then for the same work a kept session does.
- The default costs the generated turn. With
offload_prompt_only: true, the 512 tokens the model wrote in turn 1 were never stored, so 496 more tokens are recomputed on resume (768 instead of 272). That costs 38, 43 and 58 ms at the three lengths, more than a third of the resume time at 2K tokens. For an agent, whose context grows by its own output every turn,"offload_prompt_only": falseis the setting to use.
In one round, three of the four prompt-only sessions at 28K tokens were 23 to 40 ms faster than the rest, and in another round one full-restore session was 42 ms faster. Their token counts, their copy times (165 to 167 ms) and the GPU clock around them (2,700 MHz, sampled once a second) are the same as everyone else’s, so the time they saved is in the rest of the request, not the copy, and none of the recorded quantities shows why. The medians above include them.
The lazy connector crashes on this driver
SimpleCPUOffloadConnector with lazy offload crashed the engine in every round, a few requests
into the first eviction, with Segfault encountered and no Python stack. Its copy path calls the
CUDA driver function cuMemcpyBatchAsync, which drivers older than CUDA 12.8 do not have; this
driver reports 12.4. The OffloadingConnector’s copy above checks the lookup and falls back; this
connector’s copy, written separately in Python, does not check it and calls a null pointer. With a check added, the connector instead fails at start-up with an error that names
the driver; that fix is vllm-project/vllm#59974.
On a driver with CUDA 12.8 or later the connector takes the batched path, which this machine’s
driver does not have.
How often eviction happens
Whether any of this matters depends on how often a session is evicted during a tool call. I read the public TraceLab coding-agent trace (release v0.0.1, 432,510 tool calls from real sessions) and took, for each tool call, the context of the model turn that issued it. Only 9.3% of them (40,318) fit Qwen2.5-7B’s 32,768-token window at all; the rest belong to longer sessions that this model could not serve. The token counts are the providers’ (the trace comes from Claude and Codex sessions), not Qwen’s.
A 32,768-token context needs less than half of this card’s 118,080-token pool, so one or two such sessions always fit. Shared by four sessions, 15% of these tool calls would not fit their quarter of the pool, and shared by eight, 80% would not fit their eighth (89% of the tool calls that run longer than a second, the ones worth evicting for). So on one 24 GB card with a 7B model, eviction during tool calls becomes common between four and eight concurrent agent sessions.
What to take from it
On one RTX 4090 with a 7B model and one session in flight at a time:
- If an agent’s session may be evicted during a tool call, give vLLM an
OffloadingConnectorwith a host buffer and"offload_prompt_only": false. At 28K tokens the next turn then starts in 0.34 s instead of 3.8 s. - Expect the restore to cost its copy, at about 10 GB/s, on top of what a kept session costs: at 1.6 GB that is 167 ms, against more than 3.6 s of recomputation.
- Do not use
SimpleCPUOffloadConnector’s lazy mode on drivers older than CUDA 12.8.
Reproduce
The harness is at
rockyshi-dev/agent-kv-retention. It builds the
sessions, starts each configuration, runs the five rounds, and recomputes every count, time and
ratio above from the raw records (python -m akvr report); python -m akvr trace does the TraceLab
count, and scripts/restore_copy.py the copy times from the saved counters. The numbers in this
post come from a run of that repository.