Home Knowledge Base KV Cache Optimization

KV Cache Optimization is the set of techniques for reducing memory footprint and bandwidth requirements of cached key-value pairs in autoregressive transformer inference — including quantization (INT8/INT4), eviction policies, compression, and architectural changes that collectively enable 2-10× memory reduction, allowing larger batch sizes, longer contexts, or deployment on smaller GPUs while maintaining generation quality.

KV Cache Fundamentals:

<svg viewBox="0 0 760 470" xmlns="http://www.w3.org/2000/svg" font-family="-apple-system,Segoe UI,Roboto,sans-serif">
  <rect x="0" y="0" width="760" height="470" fill="#0d1117"/>
  <text x="380" y="28" fill="#e6edf3" font-size="21" font-weight="700" text-anchor="middle">KV Cache — The Memory Bottleneck of LLM Inference</text>
  <text x="380" y="48" fill="#8b98a5" font-size="12" text-anchor="middle">store past key/value tensors to avoid recomputation — grows linearly with context length</text>

  <!-- Why KV cache exists -->
  <rect x="30" y="65" width="700" height="115" rx="6" fill="#080d14" stroke="#233043" stroke-width="1"/>
  <text x="380" y="84" fill="#e6edf3" font-size="11" font-weight="600" text-anchor="middle">Why KV Cache: Avoid Recomputing Attention</text>

  <!-- Without cache -->
  <text x="50" y="108" fill="#f87171" font-size="9" font-weight="600">Without cache (step t):</text>
  <text x="50" y="124" fill="#8b98a5" font-size="8">recompute K, V for ALL t tokens → O(t²) per step → O(t³) total</text>

  <!-- With cache -->
  <text x="50" y="148" fill="#34d399" font-size="9" font-weight="600">With KV cache (step t):</text>
  <text x="50" y="164" fill="#8b98a5" font-size="8">compute K_t, V_t for new token only → append to cache → O(t) per step → O(t²) total</text>

  <!-- Visual: growing cache -->
  <rect x="470" y="98" width="20" height="60" rx="2" fill="#1e3a5f" stroke="#60a5fa" stroke-width="0.4"/>
  <rect x="494" y="98" width="20" height="60" rx="2" fill="#1e3a5f" stroke="#60a5fa" stroke-width="0.4"/>
  <rect x="518" y="98" width="20" height="60" rx="2" fill="#1e3a5f" stroke="#60a5fa" stroke-width="0.4"/>
  <rect x="542" y="98" width="20" height="60" rx="2" fill="#1e3a5f" stroke="#60a5fa" stroke-width="0.4"/>
  <rect x="566" y="98" width="20" height="60" rx="2" fill="#1e3a5f" stroke="#60a5fa" stroke-width="0.4"/>
  <rect x="590" y="98" width="20" height="60" rx="2" fill="#14261f" stroke="#34d399" stroke-width="0.8"/>
  <text x="600" y="132" fill="#34d399" font-size="7" text-anchor="middle">new</text>
  <text x="530" y="170" fill="#6b7684" font-size="7" text-anchor="middle">cached K/V from previous tokens</text>

  <!-- Memory calculation -->
  <rect x="30" y="190" width="345" height="130" rx="6" fill="#080d14" stroke="#233043" stroke-width="1"/>
  <text x="202" y="210" fill="#e6edf3" font-size="10" font-weight="600" text-anchor="middle">KV Cache Size Formula</text>

  <text x="50" y="234" fill="#38bdf8" font-size="9.5" font-weight="600">size = 2 × L × n_kv × d_head × seq × bytes</text>
  <text x="50" y="254" fill="#8b98a5" font-size="8">L=80 layers, n_kv=8 (GQA), d_head=128, seq=128K, FP16</text>
  <text x="50" y="270" fill="#8b98a5" font-size="8">= 2×80×8×128×131072×2 = 32 GB per sequence!</text>
  <text x="50" y="290" fill="#f87171" font-size="8">batch of 16 → 512 GB KV cache alone</text>
  <text x="50" y="308" fill="#6b7684" font-size="7.5">this is why long context is expensive — KV cache dominates memory</text>

  <!-- Optimization techniques -->
  <rect x="390" y="190" width="340" height="130" rx="6" fill="#080d14" stroke="#233043" stroke-width="1"/>
  <text x="560" y="210" fill="#e6edf3" font-size="10" font-weight="600" text-anchor="middle">Optimization Techniques</text>

  <text x="410" y="234" fill="#34d399" font-size="8.5" font-weight="600">GQA (8 KV heads vs 32):</text><text x="580" y="234" fill="#8b98a5" font-size="8">4× smaller cache</text>
  <text x="410" y="252" fill="#60a5fa" font-size="8.5" font-weight="600">KV quantization (INT8):</text><text x="580" y="252" fill="#8b98a5" font-size="8">2× compression</text>
  <text x="410" y="270" fill="#a78bfa" font-size="8.5" font-weight="600">PagedAttention:</text><text x="545" y="270" fill="#8b98a5" font-size="8">no fragmentation waste</text>
  <text x="410" y="288" fill="#f59e0b" font-size="8.5" font-weight="600">Sliding window (W=4K):</text><text x="590" y="288" fill="#8b98a5" font-size="8">bounded cache</text>
  <text x="410" y="306" fill="#f87171" font-size="8.5" font-weight="600">Token eviction (H₂O):</text><text x="575" y="306" fill="#8b98a5" font-size="8">keep only important KVs</text>

  <!-- Bottom: prefix caching -->
  <rect x="30" y="332" width="700" height="98" rx="6" fill="#080d14" stroke="#233043" stroke-width="1"/>
  <text x="380" y="352" fill="#e6edf3" font-size="10" font-weight="600" text-anchor="middle">Advanced: Prefix Caching &amp; Multi-Query Serving</text>
  <text x="50" y="374" fill="#8b98a5" font-size="8.5">• System prompt KV cached once, shared across all requests (saves 80%+ recomputation)</text>
  <text x="50" y="392" fill="#8b98a5" font-size="8.5">• Radix tree indexing: reuse any common prefix (SGLang)</text>
  <text x="50" y="410" fill="#8b98a5" font-size="8.5">• Cross-request KV sharing: same document, different questions → compute KV once</text>
  <text x="50" y="424" fill="#6b7684" font-size="7.5">vLLM automatic prefix caching: hash-based dedup of KV blocks across requests</text>

  <text x="380" y="452" fill="#6b7684" font-size="11" text-anchor="middle">KV cache is the dominant memory cost in LLM serving — optimizing it directly translates to $/token savings.</text>
</svg>

Quantization Techniques:

Eviction and Compression:

Architectural Optimizations:

PagedAttention and Memory Management:

Production Deployment Impact:

KV Cache Optimization is the critical enabler of practical LLM deployment — by addressing the memory bottleneck that dominates inference costs, these techniques transform LLMs from research artifacts requiring massive GPU clusters into production systems that serve millions of users on reasonable hardware budgets.

kv cache optimizationkey value cache managementattention cache compressionkv cache quantizationinference memory reduction

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.