Prompt Caching is a pattern that stores and reuses the processed representation of common prompt prefixes to avoid redundant computation. When multiple requests share the same system prompt or context, the cached prefix eliminates re-processing, reducing both latency and cost.
At a Glance
- Problem: Many production requests repeat the same questions or share the same long system prompt and context. Each call is processed from scratch, so you pay in compute, latency, and API cost for identical tokens thousands of times a day.
- Forces: Hit rate versus correctness, since looser semantic matching serves wrong answers to similar prompts; longer TTLs that save more versus fresh answers; shared caches with higher hit rates versus per-user caches that avoid poisoning.
- Solution: Cache at two levels. Client-side, store full responses and serve them on exact or semantic matches. Server-side, put shared content at the start of the prompt so the provider reuses the computed KV states for that prefix.
- Consequences: Gains: response-cache hits skip the model call entirely, and prefix caching cuts cost and latency for shared context with little effort. Costs: stale answers without invalidation, false-positive semantic matches, embedding and vector store upkeep, and lost variety for creative workloads.
What problem does Prompt Caching solve?
Your application handles 100,000 requests per day. When you read the logs, you find that a large share of those requests are variations of the same questions. "What is your return policy?" and "How do I return an item?" and "Can I send this back?" all need the same answer. Yet each one triggers a full model inference, consuming compute, adding latency, and running up your API bill.
Requests that are not duplicates often still share structure. Every request to your coding assistant starts with the same 2,000-token system prompt. Every customer service query includes the same company knowledge base in the context. You are paying to process identical token sequences thousands of times per day, and the model does the same computational work each time as if it had never seen those tokens before.
At scale, this redundancy dominates your costs. The model does not remember previous requests, so each API call starts from scratch. Without caching, you give up large savings in both cost and latency.
How does Prompt Caching work?
Prompt caching eliminates redundant computation by reusing work that has already been done. There are two different approaches, and they operate at different levels of the stack.
Client-side caching works at the response level. You store the full response for a given prompt and serve it directly when you see the same (or sufficiently similar) prompt again. The simplest version is exact-match caching. Hash the prompt, check if you have a cached response, and return it if you do. This works well for FAQ-style workloads where the same questions recur frequently.
Semantic caching extends this idea to handle paraphrases. Instead of hashing the raw prompt text, you compute an embedding vector and search for cached responses whose prompts are semantically similar. "What is your return policy?" matches "How do returns work?" because the embeddings are close in vector space. You set a similarity threshold, and any prompt within that threshold gets the cached response. This raises your cache hit rate considerably but requires a vector store and an embedding model.
Server-side caching works at the computation level, inside the model's inference pipeline. When a model processes a sequence of tokens, it builds up internal representations called key-value (KV) states for each token. Prefix caching, offered by providers like Anthropic and Google, saves these KV states for token sequences that appear at the start of your prompts. When a new request shares the same prefix (your system prompt, for example), the provider skips recomputing those states and starts from the cached intermediate result. You see this as reduced latency and lower per-token costs, often 50-90% cheaper for the cached portion.
The two approaches are complementary. Server-side prefix caching reduces the cost of processing shared context. Client-side semantic caching eliminates the model call entirely for repeated queries. A production system can use both.
When should you use Prompt Caching?
Start with prompt caching when your API costs are a concern and your workload has any repetition. Look at your logs. If more than 10-15% of requests are semantically similar, caching will pay for itself quickly.
Prefix caching is nearly free to adopt if your provider supports it. Many providers automatically cache prefixes beyond a certain length. You only need to structure your prompts so that the shared content (system instructions, knowledge base, few-shot examples) appears at the beginning. This is a prompt engineering change, not an infrastructure project.
Semantic caching makes sense when your users ask the same types of questions in different ways. Customer support, FAQ bots, and documentation assistants are good candidates. The investment is moderate: you need an embedding model and a vector store, but these are standard components in most AI stacks.
Exact-match caching is the lowest-effort option and works well for programmatic use cases where the same prompts recur literally. Batch processing jobs, automated report generation, and CI/CD pipelines that run the same analysis repeatedly all benefit from simple hash-based caching.
When not to use it: Skip response caching when answers depend on data that changes often and you cannot invalidate entries reliably, or when requests are so diverse that the hit rate will not cover the embedding model and vector store upkeep; prefix caching may still help there. Avoid it for creative or brainstorming features that need varied output. If the cost comes from generating each unique answer rather than from repetition, look to Model Router or Small Language Models instead.
Worked Example: Cost and Latency
A coding assistant handles 100,000 requests a day; each starts with the same 2,000-token system prompt, adds 300 tokens of user input, and gets a 300-token answer from the large model.
| Setup | Calls per request | Tokens per request (in / out) | Cost per 1,000 requests | Latency per request |
|---|---|---|---|---|
| No caching | 1 | 2,300 / 300 | $11.40 | 5.5 s |
| Prefix caching | 1 | 2,300 (2,000 cached) / 300 | $6.00 | 5.5 s |
| Prefix caching plus semantic response cache, 20% hit rate | 1 embedding call, plus 1 LLM call on a miss | 50 embedded, plus 2,300 / 300 on a miss | $4.80 | 0.05 s on a hit, 5.6 s on a miss, 4.5 s average |
Uncached, the input costs 2,300 × $3.00 per million = $0.0069 and the output 300 × $15.00 per million = $0.0045, so $11.40 per 1,000. With the prefix cached at 10% of the input price, the 2,000 shared tokens cost $0.0006 and the 300 fresh tokens $0.0009, so $6.00 per 1,000: a 47% cut, or $540 a day. A 20% response-cache hit rate (assumed) skips the model on one request in five: 0.8 × $6.00 plus $0.001 for embeddings ≈ $4.80. The reference latency model holds first-token time fixed, so prefix caching shows up here as cost only; the 0.05 s cache lookup is also an assumption.
Prefix caching pays off whenever the shared prefix is long relative to the variable part; once output tokens dominate the bill (here $4.50 of the $6.00), only a response cache cuts further, and it earns its upkeep only at a meaningful hit rate.
What are the common pitfalls?
Stale caches are the primary risk. If the correct answer changes (your return policy updates, product information changes, prices shift) but the cache still holds the old response, users get outdated information. You need a cache invalidation strategy. Time-based expiration is the simplest approach. Event-driven invalidation (clearing relevant cache entries when underlying data changes) is more precise but harder to implement.
Semantic cache matching can produce false positives. Two prompts that are semantically similar but require different answers will return the wrong cached response. "What is the price of Product A?" and "What is the price of Product B?" might have similar embeddings but need different answers. Your similarity threshold needs careful tuning, and you may need to include structured metadata (product ID, user segment) in your cache key alongside the semantic embedding.
Cache poisoning is a concern in adversarial environments. If an attacker can craft a prompt that gets cached and then served to other users, they can influence the responses those users see. This is mainly a risk with shared caches across users. Per-user caches avoid this but reduce hit rates.
Over-caching non-deterministic responses can hurt quality. If your application benefits from response variety (creative writing, brainstorming), caching removes that variety, and those workloads gain little from caching.
What are the trade-offs?
Semantic caching requires infrastructure. You need an embedding model, a vector store, and the operational overhead of maintaining both. For small-scale applications, this overhead may exceed the cost savings. Simple exact-match caching with a key-value store is often a better starting point.
Cache hit rate determines the value of the entire system. If your workload is highly diverse with few repeated patterns, your hit rate will be low and the infrastructure cost will not be justified. Measure your actual repetition rate before investing heavily in caching.
Freshness and cost savings are in tension. Shorter cache TTLs keep responses fresh but reduce hit rates. Longer TTLs maximize savings but increase the risk of serving stale data. The right balance depends on how frequently your underlying information changes.
Prefix caching is largely free of trade-offs if your provider offers it. The main consideration is prompt structure. You get maximum benefit when the shared prefix is long and the variable suffix is short. Reorganizing your prompts to front-load shared content is usually straightforward.
Goes Well With
Small Language Models pair well with caching for a layered cost optimization strategy. Cache the most common requests to avoid model calls entirely. Route the remaining requests to a smaller, cheaper model when possible. Reserve the large model for the long tail of complex queries. Each layer reduces the load on the next.
Inference Optimization addresses the same cost and latency concerns from the infrastructure side. Caching removes redundant calls, and inference optimization makes the remaining calls faster and cheaper, so the savings compound.
Basic RAG systems benefit significantly from prefix caching. The retrieved context often shares a common structure, and the system prompt plus retrieval instructions can be cached as a prefix. This reduces the per-query overhead of the RAG pipeline.
References
- Kwon, W., et al. (2023). Efficient Memory Management for Large Language Model Serving with PagedAttention. SOSP 2023. arXiv