Overview

KV cache optimization addresses the memory bottleneck in autoregressive LLM inference. The cache stores key-value projections from attention layers for reuse across generation steps, avoiding redundant computation. However, cache size grows linearly with sequence length, creating memory constraints for long contexts.

Optimization techniques

  • PagedAttention: Non-contiguous memory allocation
  • KV compression: Reduce stored tokens
  • KV retrieval: Selective cache reuse
  • Quantization: Lower precision storage
  • Speculative decoding: Reduce generation steps

Memory management

  • CPU-GPU offload: Free GPU memory
  • Chunked caching: Process in segments
  • Sliding window: Limit context horizon
  • Selective retention: Keep only key tokens

Key considerations

  • Balance memory savings against quality loss
  • Profile cache access patterns for optimization
  • Consider hardware-specific implementations
  • Handle cache invalidation for shared contexts

Build This Pattern

Copy this prompt and paste it into Claude Code, OpenCode, Codex, or Cursor to implement this pattern.

Explain KV-cache optimization for transformer inference. Architecture: describe how autoregressive generation recomputes KV projections for all previous tokens at each step, and how caching eliminates this redundancy. Walk through the implementation: allocate K and V tensors per attention layer, pre-fill from prompt processing, then append new token projections during generation. Derive complexity reduction from O(n^2) per step to O(n) for the new token. Cover memory overhead - KV-cache grows linearly with sequence length and batch size, potentially dominating VRAM for long contexts. Error handling: discuss cache misses when generation is interrupted or reset, numerical drift in cached values over very long generations, and memory fragmentation with variable-length sequences. Edge cases: handling very long generations where cache exceeds VRAM, interaction with beam search requiring separate caches per hypothesis, and compatibility with speculative decoding where multiple tokens are generated per step. Best practices: include practical guidance on implementing PagedAttention (non-contiguous memory), choosing between MHA, MQA, and GQA based on latency versus quality trade-offs, and applying KV-cache quantization (INT8, FP8) with calibration data. Discuss sliding window caches for infinite-length generation. Testing: suggest measuring peak memory usage against sequence length, validating cache correctness against non-cached baseline, and profiling cache-related operations for optimization.