explainx.ai0k
TrendingAI News TodayPathwaysSkills
Pricing
explainx.ai

Upskill in AI — 16 free pathways, live workshops & bootcamps, and 50+ courses from practitioners. Plus the skills, tools, and MCP servers to practice on.

follow us

follow on google

Add explainx.ai as a preferred source

corporate training

support@explainx.ai

get started

Find your pathTake Free Evaluation

community

Join the community

learn

mind: share how you thinkpathways — start freeworkshopsbootcampscoursescompare Explainxcertificationsmock testsexplainx universitycorporate traininglearn skills & mcp

discover

skillsmcp serversexplainx mcpestacktoolsmdx readeragentsllmsdesignsdictionarypeopleagi trackerfelony benchranks

company

aboutvisionmissionteaminstructorsteach on explainxpartnershipscommunityhackathonscareers

content

daily AI newsstate of AI — live resultsblogreleasespromptsgeneratorsresource libraryfor LLMsexplainx.ai kids

solutions

all solutionsdeveloper upskillingmarketing upskillingproduct manager upskillingleadership upskilling

newsletter · weekly

Get AI news, tools, and insights in your inbox.

supportcontactprivacytermsdata rightshow we create contentsubmission guidelines

© 2026 AISOLO Technologies Pvt Ltd

explainx.ai

On this page

  • Quick answers
  • What the KV cache actually is
  • How memory scales (back-of-envelope)
  • Prefill, decode, and where time goes
  • Prefix caching and provider prompt cache
  • Tuning on local runtimes
  • Agent and RAG stacks: where KV shows up twice
  • What practitioners should do
  • FAQ recap
  • Related on explainx.ai
← Back to blog

explainx / blog

What Is the KV Cache in LLM Inference? Memory, Context and Tuning

Local AI, Inference, LLMs, AI Infrastructure, Guides

Part of Local AI

KV cache stores attention keys and values so decode skips recompute. See how memory scales with context, layers, and batch size, and how to tune vLLM and MLX.

Nov 8, 2026·8 min read·Yash Thakker
add explainx.ai
go deep
What Is the KV Cache in LLM Inference? Memory, Context and Tuning

The KV cache is the hidden memory bill on every autoregressive LLM call. Weights load once; the cache grows with every token you keep in context and with every parallel chat. If a 7B quantized model fits in 6 GB but a 32K thread crashes, the cache is usually why.

Quick answers

table · 2 cols
QuestionDirect answer
What is stored?Per-layer key and value activations for each prior token
When is it built?During prefill on the prompt, then extended each decode step
What drives size?Layers, hidden width, heads, dtype, sequence length, batch concurrency
Who documents tuning?vLLM CacheConfig, Hugging Face inference guide
Local leversmlx --max-kv-size, llama.cpp context, Ollama num_ctx
Server leversvLLM gpu_memory_utilization, prefix caching, KV offload
Weekly digest3.6k readers

Catch up on AI

Curated AI updates on agents, skills, and MCP — delivered to your inbox. Unsubscribe anytime.

What the KV cache actually is

KV cache explained as attention key-value pairs kept in a side pocket during decodeKV cache explained as attention key-value pairs kept in a side pocket during decode

Transformer decoders generate one token at a time. At step t, each attention layer must let the new token attend to all positions 1 … t−1. Without a cache, the stack would re-run the full prompt through every layer on every step — correct, but wasteful.

During prefill, the model processes the whole prompt in one or more forward passes and writes keys and values for each position into the cache. During decode, only the latest token gets fresh projections; earlier keys and values are read from GPU memory. Hugging Face documents this pattern as the motivation for a static KV cache when compiling models with torch.compile.

The cache is not part of the downloadable checkpoint. It is runtime state, like an expanding scratch pad. That is why context-window pricing and prompt caching economics treat long inputs differently from short ones: prefill work and cache residency both scale with prompt length.

How this differs from model weights

table · 3 cols
Model weightsKV cache
Loaded whenServer or app startEach session as tokens arrive
Shrinks with Q4/Q8YesNo (unless server quantizes KV)
Grows during chatFixedGrows with assistant output too
Shared across usersYesOnly with deliberate prefix reuse

Our quantization guide notes that Q4 may free enough VRAM for weights while leaving little headroom for a fat cache — a common laptop failure mode.

How memory scales (back-of-envelope)

Lab · KV cache size

LLM KV cache memory growth sketched as a long context ribbon passing through a ringLLM KV cache memory growth sketched as a long context ribbon passing through a ring

Exact bytes depend on architecture (GQA, MLA, sliding window), but planners still use a proportional model:

KV bytes ≈ 2 × layers × sequence_length × kv_heads × head_dim × bytes_per_element × concurrent_sequences

The leading factor 2 is keys plus values. Sequence length includes system prompt, tools, retrieved chunks, and every assistant token already emitted — not just the user message you typed last.

Grouped-query attention (GQA) cuts kv_heads versus full multi-head attention, which is why many 2026 open models advertise long context without the KV footprint of older 70B stacks. Sliding-window layers cap effective history but add complexity: some positions never enter cache, yet runtimes must still track masks correctly.

When you compare vLLM versus SGLang, both engines fight the same physics; they differ in schedulers, prefix-hash policies, and optional disaggregated prefill/decode when one GPU cannot hold model plus cache for your SLA.

Prefill, decode, and where time goes

table · 3 cols
PhaseComputesKV cache
PrefillAll prompt tokens in parallel (often compute-bound)Filled for every prompt position
DecodeOne new token per step (often memory-bandwidth-bound)Append one slot per layer per step

Long prompts dominate prefill latency; long answers dominate decode time because each step touches the full cache. Speculative decoding speeds decode by drafting multiple tokens, but verified steps still update KV states — a topic we cover in our companion speculative decoding guide.

For benchmarking long-context faithfulness, synthetic suites like RULER stress retrieval across hundreds of thousands of tokens — the same regime where KV sizing decides whether the benchmark runs at all.

Prefix caching and provider prompt cache

Automatic prefix caching on servers reuses KV blocks when a new request shares an exact token prefix with an earlier one. vLLM's design doc hashes blocks from parent hash, block tokens, and optional salts for multi-tenant isolation. Enable it with enable_prefix_caching=True on the engine. APC helps multi-turn chats and repeated system prompts, not one-off unique prompts.

Cloud APIs expose a similar idea as prompt caching: Anthropic and OpenAI bill cached input segments at a discount because the provider avoids recomputing identical prefill. That is a billing and latency feature for clients; locally you implement the same idea with vLLM, SGLang, or llama.cpp server flags. Read prompt caching tradeoffs before assuming cache hits are free on the security side — shared prefix stores can be a side channel in hostile multi-tenant layouts, which is why vLLM documents optional cache_salt.

Tuning on local runtimes

mlx-lm on Apple Silicon

Our MLX guide walks install and server mode. When wired memory warnings appear, mlx-lm suggests lowering quant width, raising iogpu.wired_limit_mb carefully, or passing --max-kv-size to cap cache growth. That trades maximum context for stability on 16–24 GB unified machines.

llama.cpp and Ollama

llama.cpp exposes context via -c / n_ctx. Ollama sets context per model in the Modelfile or environment. If generation slows sharply after a long thread, check whether you are paging or hitting GPU offload limits — often KV plus partial weight offload exceed VRAM.

vLLM and production servers

High-bandwidth memory stack feeding a chip, standing in for KV cache on GPU HBMHigh-bandwidth memory stack feeding a chip, standing in for KV cache on GPU HBM

vLLM CacheConfig lists the levers operators actually use:

  • gpu_memory_utilization — fraction of device memory reserved for weights plus KV pool
  • block_size — tokens per paged KV block
  • enable_prefix_caching — reuse hashed prefix blocks
  • kv_offloading_size — spill blocks to host RAM via the offloading connector (usage guide)

When requests preempt, vLLM logs that KV space ran out and may recompute sequences later (optimization docs). Fix preemption by more KV memory, fewer concurrent seqs, tensor parallel sharding (smaller per-GPU weights, more room for cache), or pipeline parallel layouts — same trade space discussed in our vLLM vs SGLang comparison.

Agent and RAG stacks: where KV shows up twice

Coding agents are KV-heavy products even when the model is small. A single Claude Code or Codex session may carry system rules, tool schemas, file excerpts, linter output, and prior turns. Each category is still in the active context window, so the cache tracks the whole bundle. Trimming tool definitions helps as much as trimming chat — see MCP tool description reliability for why fat tool lists hurt both selection and memory.

RAG adds another twist. Retrieved chunks land in the prompt, so prefill cost and KV footprint jump on every query. A reranker that returns eight 800-token passages can add more than six thousand tokens before the model answers. Our reranker latency guide is about milliseconds, but the same design choice changes how many slots your inference server must keep warm. Agents that “read the whole repo” without retrieval often hit context limits first; agents that retrieve aggressively hit KV limits when many users run at once.

Multi-tenant SaaS has a third pattern: one model, many isolated caches. GPU memory is shared, but KV blocks are per request unless prefix caching explicitly deduplicates identical system prompts. That is why inference vendors quote both model memory and max concurrent users — the second number is really a KV scheduling constraint.

What practitioners should do

Size hardware with KV, not weights alone. A card that loads Q4 13B is not automatically a 128K-context card.

Cap context in products. Agent harnesses that stuff entire repos into prompts (context engineering) blow cache faster than chat UIs. Prefer retrieval (reranker guide) over infinite history.

Separate prefill and decode when scaling. Disaggregated serving moves prefill-heavy work to a different pool than decode-heavy chat — relevant once a single node's KV pool becomes the bottleneck.

Watch concurrent agents. Four coding agents on one local server mean four caches unless you serialize. If you run Hermes Agent or OpenClaw on a single GPU box, set per-agent context caps in the harness before you chase a larger quant.

Measure before buying hardware. Log max_num_seqs preemptions in vLLM, watch mlx wired-memory warnings, or track OOMs during long codegen sessions. Those signals are almost always KV pressure, not a bad checkpoint. Top laptop open models lists realistic context per RAM tier with this constraint in mind.

FAQ recap

The KV cache is the reason identical weights feel “heavier” after a long session. Quantization slimmed the checkpoint; it did not remove per-token state. Prefix caching and provider prompt cache discounts attack repeated prefill, not unbounded assistant length. For decode acceleration without changing cache math, see speculative decoding.

KV cache behavior, vLLM flags, and Hugging Face static-cache notes are accurate as of November 8, 2026. Always check your runtime version — block sizes and offload specs change between releases.

Related on explainx.ai

  • vLLM vs SGLang inference servers
  • Prompt caching cost and security
  • Context window pricing decoded
  • AI model quantization guide
  • MLX on Apple Silicon
  • RULER long-context benchmark
Spotted something out of date? Let us know.
Yash Thakker

Written by

Yash Thakker

Yash is an AI expert with over 300K learners. Join his workshops →

View Yash Thakker in People in AI →

Related posts

Nov 8, 2026

What Is Speculative Decoding? Faster LLM Token Generation Explained

Autoregressive LLMs load billions of weights for every single token. Speculative decoding breaks that pattern: a small drafter proposes several candidates, and the large target model accepts or rejects them in batched forward passes. When acceptance rates are high, users see two to three times faster output without changing the final distribution — if the stack is configured correctly.

Nov 7, 2026

vLLM vs SGLang: Which GPU Inference Server Should You Run in 2026?

If you self-host models on NVIDIA GPUs in 2026, vLLM and SGLang are the two frameworks teams argue about most. Both expose OpenAI-compatible HTTP APIs, both support MoE models, and both ship fast attention kernels — but vLLM leads on ecosystem breadth while SGLang leads on structured generation and RL-heavy serving. Here is how to choose.

Jun 16, 2026

What Is AI Model Quantization? Running Frontier AI Locally

A 7B parameter model at full float32 precision needs 28GB of VRAM — more than an RTX 4090 has. Quantization is the technique that makes local AI practical: reduce numerical precision, shrink the model, run it on hardware you own. Here is how it works, why quality holds up better than expected, and what you should actually run on your GPU in 2026.