Prompt caching is a breakthrough inference-time optimization in modern Large Language Model (LLM) serving architectures. In applications requiring extensive system prompts, few-shot demonstration exemplars, large codebases, or multi-turn conversational histories, standard autoregressive inference repeatedly recomputes Key-Value (KV) attention activations for identical prefix tokens across every request.
By persisting precomputed attention tensors across identical prefix token hashes, prompt caching reduces time-to-first-token (TTFT) latency by up to 80% and slashes input token API costs by up to 90% (e.g., Anthropic, Google Gemini, and OpenAI prompt caching).
This guide details KV-cache memory dynamics, prefix hashing mechanisms, breakpoint boundaries, and production cache-management strategies.
+-----------------------------------------------------------------------------------------+
| PROMPT CACHING IMPACT METRICS |
+-----------------------------------------------------------------------------------------+
| Metric | Without Prompt Caching | With Prompt Caching (Hit) | Improvement |
+----------------------------+------------------------+---------------------------+-------------+
| Time-To-First-Token (TTFT) | 1,500 ms - 4,000 ms | 200 ms - 500 ms | 75% - 85% |
| Input Token Billing Cost | 100% Full Price | 10% - 25% Cached Price | 75% - 90% |
| GPU Compute FLOPs | O(N^2) Full Attention | O(M · N) Incremental | 80% FLOPs |
| Cache Minimum Token Floor | N/A | 1,024 - 2,048 Tokens | Provider req|
+-----------------------------------------------------------------------------------------+
Standard Transformer self-attention computes Key (K), Value (V), and Query (Q) matrices:
When generating tokens t_{N+1}, t_{N+2}, \dots, the model requires K_{1:N} and V_{1:N} for all preceding context tokens.
Prefix KV-Cache Hash Tree (Radix Tree in vLLM / SGLang):
[ Root: System Prompt (Hash: 0x8F21) ] (Cached KV-Cache in VRAM)
|
v
[ Tool Definitions & Schema (Hash: 0x3B9A) ] (Cached KV-Cache in VRAM)
|
+--------+--------+
| |
(Session A History) (Session B History)
- Reused 8,000 Tokens! - Reused 8,000 Tokens!
- 0 ms Pre-fill cost - 0 ms Pre-fill cost
Prompt caching relies on deterministic prefix matching: any single modified character early in the prompt invalidates the entire cache for all subsequent tokens.
Structure prompts strictly from most static (slowly changing) to most dynamic (per-turn mutations):
Optimal Prompt Caching Layout:
[ 1. Master System Instructions & Personas ] ---> 100% Static (Permanent Cache Hit)
[ 2. API Tool Schemas (JSON-RPC / MCP) ] ---> 100% Static (Permanent Cache Hit)
[ 3. Reference Documentation / Context ] ---> Static per Repo (Cache Hit)
[ 4. Few-Shot Demonstration Examples ] ---> Static (Cache Hit)
------------------------------------------------- CACHE BOUNDARY / BREAKPOINT
[ 5. Dynamic Conversational Session Turns ] ---> Semi-Dynamic (Appended)
[ 6. Current User Query & Timestamp ] ---> 100% Dynamic (Pre-fill Computed)
+---------------------------+-----------------------------------+------------------------+
| Provider / Engine | Cache Activation Model | Cache Retention TTL |
+---------------------------+-----------------------------------+------------------------+
| Anthropic Claude | Explicit `cache_control:ephemeral`| 5 Minutes (Refreshes |
| | breakpoints (max 4 per prompt) | on cache hit) |
| OpenAI (GPT-4o) | Automatic Prefix Matching | 5 - 10 Minutes (LRU |
| | (Min 1,024 token threshold) | memory eviction) |
| Google Gemini | Context Caching API (Explicit) | Configurable (Hours to |
| | (Min 32,768 token threshold) | Days; storage fee) |
| vLLM / SGLang (Self-Host) | Automatic RadixAttention | In-memory VRAM LRU |
| | (PagedAttention dynamic memory) | block eviction |
+---------------------------+-----------------------------------+------------------------+