KV cache reuse for agents, documents, restarts and fleets

KVStash pays off when a long prompt prefix comes back after the engine has lost its cache. Four patterns where that happens, each with the closest result we have measured.

Last updated

01Agents

KV cache for AI agents

An agent resends the same instructions, tools and history on every step of every run.

The situation
A coding or research agent runs dozens of steps per task. Each step starts with the same system prompt and tool definitions plus a growing history: thousands to tens of thousands of tokens the model has already processed.
Why the context is lost
Between steps the agent waits on tools for seconds or minutes while other sessions fill GPU and host memory, so the prefix is evicted. A step routed to another server starts cold.
What KVStash changes
Each step writes its KV-cache blocks to the store. The next step reads the shared prefix and earlier turns back and computes only the new tool output.
How to check fit
For each step, compare the tokens it shares with the previous step (from the first token) with the cached-token count your engine reports. The gap is context a store could return.

02Documents

Many questions against one long document

An assistant answers question after question about the same contracts, codebase or manual.

The situation
The document is tens of thousands of tokens; each question is a few dozen. The same text reaches the model again and again.
Why the context is lost
Document caches are large: one 524k-token prompt on GLM-4-9B-chat-1M with an fp8 KV cache is 21.47 GB, so a GPU holds only a few. Questions arrive minutes or hours apart, after the cache has been evicted.
What KVStash changes
The first question pays the full prefill. Later questions, on any server, read the document's cache back and compute only the question.
How to check fit
Count questions per document per hour, and check that the document reaches the model byte-identical each time: same whitespace, order and position. Retrieved passages placed before it end reuse at the first difference.

03Restarts and autoscaling

Start restarted and new servers warm

Deploys, crashes and spot reclaims empty the engine's cache at the worst moment.

The situation
Servers restart for deploys, crashes and spot reclaims, and autoscaling adds new ones under load.
Why the context is lost
The engine's cache lives in the process's GPU and host memory. When the process stops it is gone, and the first requests after a restart pay full prefill when traffic is highest.
What KVStash changes
KVStash is a separate daemon, so it keeps its blocks while the engine restarts. If the daemon itself restarts, its DRAM contents are gone and the engine recomputes; with an empty store in our test: requests served; cache refilled (64/64, 75% reuse).
How to check fit
Count restarts and scale-out events per day, then compare p99 time to first token in the ten minutes after each one with the rest of the day.

04Many servers, one store

Share KV cache across servers

Work computed on one server is read by the others instead of repeated.

The situation
Several engine servers sit behind a load balancer, and the same system prompts and documents reach different servers.
Why the context is lost
Each server's cache is private. Server B recomputes what server A worked out a minute ago, and every server keeps its own copy of the popular prefixes.
What KVStash changes
All servers write to and read from one store, so a prefix computed once is available to every server connected to it. Prefix-aware routing helps too; a store still covers restarts and overflow.
How to check fit
From your router logs, count how often the same prefix lands on a different server within an hour.

How to structure prompts for KV cache reuse

Reuse depends on the order of the prompt, not on how much of it repeats. Put the parts that stay the same first and the parts that change last.

A prompt laid out for KV cache reuse The prompt runs left to right: system prompt, tool definitions, shared document, earlier turns, then the new question. Everything before the first changed token, where the new question starts, is read from KVStash in whole 64-token pages. The last partial page and the new question are computed on the GPU. Read from KVStash Computed on the GPU System prompt same on every call Tool definitions same tools, same order Shared document byte-identical text Earlier turns appended, never edited New question differs per request 64-token pages (the page size in our SGLang tests) first changed token last partial page: recomputed A prompt laid out for KV cache reuse From top to bottom: system prompt, tool definitions, shared document and earlier turns are read from KVStash in whole 64-token pages. Below the first changed token, the new question is computed on the GPU. Read from KVStash, in 64-token pages System prompt same on every call Tool definitions same tools, same order Shared document byte-identical text Earlier turns appended, never edited first changed token Computed on the GPU New question plus the last partial page
FIGURE 1. Stable parts first. Everything up to the first changed token is reused, in whole 64-token pages; the last partial page and the new part are computed.

Order, from the first token

  1. System prompt. Keep dates, user names and request IDs out of it, or move them to the end.
  2. Tool definitions. Same tools, same order, same serialisation on every call (sort JSON keys).
  3. Shared documents. The text exactly as before, in a fixed order.
  4. Earlier turns. Append new turns; editing or summarising early turns breaks reuse from that point.
  5. The new part. The question, retrieved passages and anything else that differs per request.

Rules the cache follows

  • Exact prefix from the first token. One changed token near the start means everything after it is recomputed.
  • Whole 64-token pages. A prompt sharing 3,507 tokens reused exactly 3,456 (54 full pages) in our SGLang test, with identical output.
  • No fuzzy or semantic matching. Similar documents or reworded prompts do not match.
  • Same model and settings. A different model identity gave 0 tokens from KVStash and 0 store hits; the engine recomputed.

Fit checklist

If most of the left column describes your traffic, KVStash is worth measuring. If the right column does, keep what you have.

Good fit

  • Prompts are long, roughly 8k tokens or more, and their beginning repeats.
  • The same prefix comes back after eviction, after a restart, or on a different server.
  • You run vLLM or SGLang yourself (GPU-tested on SGLang 0.5.15.post1, 0.5.23 and vLLM 0.25.1, 0.27.1, 0.33.0).
  • You can run a daemon with DRAM, and optionally NVMe, on a private network close to the GPUs.

Poor fit

  • Prompts are short or unique, so little comes back.
  • The engine's own GPU and host cache already holds everything you reuse.
  • Only the end of the prompt repeats, or a per-request value sits near the start.
  • You only call a hosted model API.

Engines reuse prefixes on their own first

vLLM automatic prefix caching and SGLang's RadixAttention with HiCache keep prefixes in GPU and host memory. KVStash is the storage tier under them for the context they cannot keep.

Hosted model APIs

KVStash cannot reach the cache inside a hosted provider. Use its own prompt caching, such as OpenAI or Anthropic prompt caching. The layout rule is the same: stable parts first.

Use case questions

Short answers. For the money side, see what avoided prefill is worth; for the method, how we measure cache value.

Does KVStash reuse prompts that are similar but not identical?

No. Reuse needs an exact prefix, counted from the first token, in pages of 64 tokens. There is no fuzzy or semantic matching. One changed token near the start means everything after it is recomputed.

Does KVStash return a cached answer?

No. It returns the KV cache of the prompt prefix, which is the engine's working state, not a response. The model still generates a new answer for every request. In our SGLang test, outputs with KVStash matched SGLang's own cache at the same reuse split in 12 of 12 cases.

Will it help a RAG pipeline?

Only for the part of the prompt that repeats exactly from the start. If each question retrieves different passages, reuse stops at the first passage that differs. Put the system prompt and any fixed documents first and the retrieved passages after them.

Can tenants or teams share cached blocks?

Blocks are scoped to a namespace. Each connection authenticates to one namespace with a bearer token, and the server stamps that namespace on every operation, so one namespace does not read another namespace's blocks.

What if I change the model, tokenizer or engine settings?

The cache key includes the model identity, so blocks from a different model are not reused. In our SGLang test a different model identity gave 0 tokens from KVStash and 0 store hits, and the engine recomputed the prompt.

Does it work with OpenAI, Anthropic or other hosted model APIs?

No. KVStash runs under inference engines you operate (vLLM and SGLang). It cannot reach a hosted provider's internal cache. Use the provider's own prompt caching for hosted APIs.

How long must a prompt be before reuse is worth it?

We measured from 8k tokens up. At 8k tokens on an L40S, reading from KVStash took 0.12 s against 0.64 s to recompute. Below 8k we have not measured, and the saving per reuse is small, so many reuses are needed to matter.

Tell us where your context repeats.

Send your engine, model, GPUs, typical prompt length and where context is lost today. We reply with which of these patterns fits, what we would measure, and when KVStash is not worth it.

Or write to rudray@kvstash.com