What the KV cache actually is
“KV” stands for key/value — the internal representations the model computes for every token it has processed so far. Instead of recomputing those representations from scratch for every new token, the model stores them in the KV cache and reuses them, appending a new entry each time a token is added to the sequence. This is what makes generation practical at all. Without a cache, generating token 500 of a reply would mean reprocessing all 499 tokens before it, every single time. With the cache, the model only has to do the new work for each new token, and reuse everything it already computed. The cache is built during prefill (when the model reads your prompt) and extended by one entry per token during decode (when it generates the reply) — see How inference works for how those two phases work.Why longer context costs more memory
The KV cache holds one entry per token, per layer, for as long as that token stays in the active context. That means its size scales directly with:- Context length — more tokens in play means more cache entries. A 128K-token conversation holds far more cached state than a 4K-token one.
- Batch size — if you’re serving multiple requests at once, each one needs its own KV cache, so total cache memory multiplies with concurrent requests.
The context-versus-speed tradeoff
Beyond memory, a larger KV cache has a speed cost too. Decode is memory-bandwidth-bound — on every single token generated, the model reads the entire KV cache built so far, in addition to its weights (see How inference works). A bigger cache means more data to read on every step, so:- Longer context slows decode down. Generation speed (tokens/sec) tends to drop as a conversation grows, even though the model itself hasn’t changed.
- Longer context costs more memory, independent of the speed cost — it’s a second, separate bill.
There’s no way around this tradeoff by picking a different quantization — quantizing weights and quantizing the KV cache are separate levers, and some backends support KV-cache quantization independently as a way to fit longer contexts. That’s a backend-specific setting; check your runtime’s documentation for support before relying on it.
Practical takeaway
When you’re deciding how much context to allocate for a deployment, treat it as a real resource budget, not a free setting. A model advertised with a huge maximum context (128K, 256K) doesn’t mean you should always run it at that limit — size your context to what the task actually needs, and check the real memory cost with the GPU model-fit guide before committing.Go deeper
How inference works
The full prompt lifecycle — prefill, decode, and what goes where in memory.
Memory and offloading
The complete sizing formula, including weights and overhead alongside the KV cache.
Once context and memory fit
Sizing your KV cache is part of making a model run well locally. Once it’s running and answering requests onlocalhost, the next step is reaching it from anywhere else.
Give it a stable endpoint
Tokios doesn’t pick, host, or fine-tune the model — once it runs locally, Tokios turns it into a stable, authenticated endpoint you can call from anywhere.