Caching & Context Optimization
Make the window cheaper and denser
You pay by the word, every single time. Caching is refusing to pay twice for the words that never change.
Most of what you send a model is the same on every request — the instructions, the company style guide, the tool descriptions. You are billed for all of it, again, on every call. Caching lets the provider keep the unchanging opening section warm so the repeat visits are far cheaper and faster. The catch is that it only works if that section is byte-for-byte identical, so a timestamp in the wrong place quietly costs you the whole discount.
15 entries · 5 categories · 4 equations
How it actually works
no jargonPut everything that never changes at the very front: instructions, tool definitions, examples. Anything that varies — the user's actual question, today's date — goes last.
The arithmetic
Provider caching
Anthropic prompt caching
Up to four cache_control breakpoints with 5-minute or 1-hour TTL, refreshed on read
Writes cost 1.25x (5-min) or 2.0x (1-hr) input. Break-even at roughly 0.28 and 1.11 reads.
Gemini explicit caching
Declared cache object with 60-minute default TTL and a large token minimum
Adds storage cost per token-hour, unlike the other two.
OpenAI automatic caching
Zero-config prefix cache with a 1,024-token minimum and no write surcharge
Self-hosted KV cache
SGLang RadixAttention
Radix tree over token sequences for longest-prefix KV match, on by default
vLLM prefix caching
Block-level content-hashed KV reuse, on by default in V1
LMCache
Cross-instance KV cache sharing and offload to CPU or NVMe
The answer for multi-node; SGLang alone suffices single-node.
Compression & pruning
Sub-agent context isolation
Spawn agents with private windows so only summaries reach the parent
A pattern rather than a product, and often more effective than a compression library.
LLMLingua
Token-classification prompt compressor achieving 2-5x reduction
Last release April 2024, no commits since April 2026. Still recommended in roundups.
Token accounting
Anthropic count_tokens
Free endpoint returning billing-accurate counts including system prompt and tools
LiteLLM
Gateway normalizing token accounting and cache headers across providers
tiktoken
OpenAI BPE tokenizer giving exact counts for OpenAI models only
Long-context evidence
Context Rot (Chroma)
Eighteen models degrade non-uniformly as input grows; includes a replication toolkit
LongFuncEval
Long-context degradation measured specifically for tool and function calling
Lost in the Middle
U-shaped positional accuracy, with over 30% drop for mid-context evidence
NoLiMa
Needle-in-haystack without lexical overlap; scores collapse where standard NIAH saturates