Prompt Caching
AI Engineer path · lesson 60 of 124Simulate API calls with and without prefix caching, compare provider pricing, and learn how to structure prompts for maximum cache hits — the cheapest performance optimisation in LLM serving.
Toggle prompt blocks on/off, enable caching, and fire API calls. Watch how costs change when the stable prefix is cached.
Key insight
The first call with caching is slightly more expensive (cache write at 1.25×), but every subsequent call reads cached tokens at 0.10× — a 90 % discount. After just 2 calls, you break even; by call 10, you've saved significantly. Toggle caching on, fire 3+ calls, and watch the per-call cost drop.
Lay out a cacheable prompt
Order the prompt so the cache actually hits.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
- 1Drop a step here
- 2Drop a step here
- 3Drop a step here
- 4Drop a step here
- 5Drop a step here
Test your knowledge
What does prompt caching actually store on the server?
The short version
Prompt caching keeps a popular book open on a bookstand. Instead of pulling it off the shelf and flipping to page 200 every time a customer asks about page 201, you read from where you left off — nearly free.
In more depth
Prompt caching
Providers cache the KV-cache (attention states) for a prompt's prefix. On a subsequent request whose prefix matches exactly, the model reuses those states instead of paying the prefill compute again.
The golden rule
Put stable content first and dynamic content last. Caches are keyed by exact byte sequence — a single whitespace change in the prefix invalidates every token after it.
Provider differences
cache_control breakpoints. Write ~1.25×, read ~0.10× (90% discount). ~5-min ephemeral TTL.cachedContent API, minimum ~32K tokens, configurable TTL, best fit for large stable corpora.What breaks the cache
Composes with everything else
Caching is cheapest first-line optimization. Compose with routing, semantic caching (paraphrase-level), and prompt compression on the dynamic tail.
Interview Questions
References & Further Reading
Love this visualization?
Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.
100% free • No credit card required