LLM Context Caching: KV Cache Architecture
Multi-turn AI agents re-send thousands of tokens (system prompts, MCP tool definitions, repository context) on every turn. Master KV cache re-use across Gemini, Anthropic, and OpenAI to slash operational bills by up to 90% and accelerate responses by 80%.
The Turn 10 Explosion
A 10-turn coding agent loop without caching re-processes the 15,000-token system prompt and tool definitions 10 times, paying for 150,000 input tokens instead of 15,000.
How KV Caches Work
Transformer attention computes Key and Value matrices for each token. Providers keep the KV activations pinned in GPU HBM memory so subsequent requests bypass matrix multiplications.
80% TTFT Reduction
Time-to-first-token drops from 2,800ms down to 400ms because the model only computes attention over the brand new user message and the latest tool execution observation.
1. Provider Code Implementations
Production CodeAnthropic Claude — Ephemeral Breakpoints
Up to 4 cache breakpoints per request
Add cache_control: {"type": "ephemeral"} to large tools, system instructions, or documents. Claude caches blocks exceeding 1,024 tokens for 5 minutes (refreshed on each read).
Google Gemini — Explicit Cached Content API
Configurable TTL (hours / days)Gemini allows creating explicit, named cache objects with precise TTL durations. Ideal for indexing large PDFs or enterprise codebases (>32,768 tokens).
2. Golden Rules for Cache Hit Rates
Rule of Fixed Prefixes
KV caches are strict prefix trees. If even a single token at the beginning of the prompt changes, the entire downstream cache is invalidated.
Deadly Cache Anti-Patterns
- Dynamic Timestamps in System Prompt: Putting
Current Time: {datetime.now()}inside your system prompt invalidates the cache on every single request. - Changing Session UUIDs at Top: Keep conversation IDs in metadata headers, never in the prompt prefix.
- Shuffling Tool Order: Always sort your tool schemas alphabetically by name before serialization.
Cache Performance Across Models
Frequently Asked Questions
Does context caching alter model reasoning or output quality? →
No. Context caching is bit-for-bit mathematically identical to re-computing the full prompt from scratch. Attention activation tensors are identical, ensuring zero degradation in reasoning quality.
How can I monitor my production cache hit rates? →
Every response object from Claude, Gemini, and OpenAI includes token usage breakdowns. Log cache_read_input_tokens / total_input_tokens to Datadog or Prometheus. A well-architected agent fleet achieves an 85% to 92% cache hit ratio.