Caching
Caching is the single highest-leverage cost control available to an assistant builder, because personalized prompts are mostly repetition. The system prompt, the tool definitions, the formatting instructions, the retrieved knowledge-base article — these bytes are identical across thousands of requests. Caching lets you pay full price to process them once and a steep discount to reuse them thereafter.
Key Points
- Prompt (context) caching reuses the model's computation for a prompt segment: ~90% read discount and up to 80–85% lower latency.
- Anthropic is explicit (
cache_controlbreakpoints, write costs 1.25x–2.0x input) while OpenAI is automatic and free (prefix routing). Both need at least 1,024 tokens. - Cache hits require an exact prefix match, so static content must go first and variable content last — the static-first assembly from Chapter 4.
- Semantic (response) caching matches query meaning via embeddings, cutting inference cost 30–70% by skipping the model for similar questions.
- Cache reads are excluded from Anthropic's ITPM limit, so caching also buys throughput headroom.
Prompt / Context Caching Mechanics
Prompt caching (also called context caching) stores the model's internal computation for a portion of a prompt so that subsequent requests reusing that portion skip the recomputation. The model loads the already-processed representation and continues from there rather than re-reading cached tokens from scratch.
Anthropic (Claude) uses explicit caching: you mark cacheable content with cache_control breakpoints. Cache writes cost more than a normal input token (1.25x for a 5-minute TTL, or 2.0x for a 1-hour extended TTL), while cache reads cost only ~10% of the base input price. OpenAI uses automatic caching, routing each request to a server that recently processed the same prefix — no code changes, no extra fee — enabled automatically for any prompt of 1,024 tokens or more, reducing latency up to 80% and input cost up to 90%.
| Dimension | Anthropic (Claude) | OpenAI |
|---|---|---|
| Activation | Explicit (cache_control breakpoints) | Automatic (prefix routing) |
| Extra fee to enable | Cache write costs 1.25x–2.0x input | None |
| Cache read price | ~10% of base input | Discounted automatically (up to 90% off) |
| Minimum length | 1,024 tokens | 1,024 tokens |
| TTL | 5 min (standard) / 1 hr (extended), refreshed on access | Rolling; prefix stays warm while reused |
| Rate-limit interaction | Cached reads excluded from ITPM limits | Cached prefix reused via routing |
The performance numbers are dramatic: Anthropic reports latency reductions up to 85% for long prompts — in a 100,000-token "book" example, response time dropped from 11.5 s to 2.4 s once caching was enabled. There is also a subtle rate-limit interaction: on Anthropic's current models, cache_read_input_tokens are excluded from the input-tokens-per-minute (ITPM) limit, so caching buys throughput headroom as well as cost savings.
Analogy: Prompt caching is like a barista who keeps your regular order on a sticky note. The first time, they take your full order slowly (the cache write, which even costs a little extra to write down). Every visit after, they glance at the note and start pulling shots immediately (the cheap, fast cache read). But the note only helps if your order starts the same way every time.
Cache-Friendly Prompt Ordering
Cache hits require an exact prefix match. The provider can only reuse computation up to the first byte that differs between two prompts. OpenAI caches the longest previously-computed prefix, starting at 1,024 tokens and growing in 128-token increments.
Figure 13.1: Prompt-cache hit/miss decision flow — an exact prefix match reads cheaply, otherwise the prefix is written to cache.
The hard ordering rule: put static content first, variable content last. This is exactly the static-first prompt assembly pattern from Chapter 4 — instructions and tools before per-user context before the live turn — which turns out to also be the cache-optimal layout.
| Layout | Order of segments | Cacheable prefix |
|---|---|---|
| Anti-pattern | "You are helping {user_name}..." → tools → instructions → user turn | Nearly nothing — the name in byte ~20 breaks the prefix for every user |
| Cache-friendly | System instructions → tool definitions → few-shot examples → per-user memory → live user turn | Everything up to per-user memory is shared and cached |
A single misplaced variable — a timestamp, a session ID, a greeting with the user's first name at the top — can silently collapse your cache hit rate from 90% to near zero. Anthropic's multiple cache_control breakpoints do not increase cost; OpenAI's optional prompt_cache_key steers requests sharing a long prefix to the same warm server.
Semantic and Response Caching
Prompt caching reuses computation for identical prefixes, but two users often ask the same question in different words — "What's your refund policy?" vs. "How do I get my money back?" A semantic cache closes that gap by comparing meaning: each query is embedded, compared against cached query vectors (usually cosine similarity), and if close enough in meaning, the cached response is served directly — no model call at all. This is a response cache.
Savings run roughly 30–70%. The open-source GPTCache (sentence-transformer embeddings + a vector DB such as Milvus, Weaviate, or FAISS) reduced API calls up to 68.8% with hit rates of 61.6–68.8%. The catch is the similarity threshold: too conservative and you miss safe reuse; too aggressive and you serve a wrong answer to a merely similar-looking query — a correctness failure worse than a cost failure. The remedy is category-aware caching: vary threshold and TTL by query category.
For a personalized assistant this is critical: semantic caching is safest for shared, non-personalized knowledge and dangerous for anything depending on a user's state. "What is my current balance?" and "What is my account balance?" are near-identical semantically but must never share a cached response. Scope semantic caches by tenant and exclude any query whose answer depends on per-user data.
| Cache type | What it matches | What it reuses | Best for |
|---|---|---|---|
| Prompt / context cache | Exact prefix | Model computation for that prefix | Long static instructions, tools, retrieved docs |
| Semantic (response) cache | Similar meaning | The entire prior response | Shared FAQ / knowledge answers |