Context Caching Explained
A practical guide to context caching in LLM applications, including prompt caching, cached prefixes, latency and cost reduction, cache invalidation, memory usage, and common implementation patterns.
Large language model applications often send the same information repeatedly. A system prompt, instructions, tool definitions, formatting rules, documentation, or other static context may appear in many requests even though only a small part of the request changes. Processing all of that repeated context from scratch can increase latency and API costs.
Context caching is a technique that allows reusable context to be processed once and reused across multiple requests. Depending on the model provider and inference system, caching may reduce the amount of repeated computation required and can lower the cost or latency associated with repeated input.
Context caching is closely related to prompt caching and prefix caching. The exact implementation differs between providers, but the general idea is the same: identify context that remains stable and avoid unnecessarily processing the same information again.
What Is Context Caching?
Context caching is the practice of reusing previously processed context when multiple LLM requests share the same or compatible input. Instead of treating every request as completely new, an inference system can reuse cached information for the repeated portion.
Request 1:
[System instructions] + [Documentation] + [User question A]
Request 2:
[System instructions] + [Documentation] + [User question B]
Request 3:
[System instructions] + [Documentation] + [User question C]
The stable prefix can potentially be reused.The cached portion is typically a stable prefix or other reusable context. The dynamic portion, such as the current user message, is still processed normally.
Why Context Caching Matters
Repeatedly processing a large context can become expensive when an application sends many requests. This is especially noticeable when the static portion of each request contains thousands of tokens.
- Large system prompts can be reused.
- Long documentation can be reused across requests.
- Tool definitions can remain cached.
- Repeated instructions do not need to be processed from scratch.
- Interactive applications can reduce repeated input processing.
- Applications may reduce input-related API costs when the provider offers discounted cached input.
How Context Caching Works
The exact mechanism depends on the inference platform, but a simplified process looks like this.
- The application sends a request containing a reusable context.
- The inference system processes the context.
- The reusable portion is stored or retained in a cache.
- A later request contains the same compatible context.
- The inference system detects the reusable portion.
- Cached computation or representations are reused.
- Only the new portion needs to be processed normally.
First request
↓
Process context
↓
Create cache
↓
Later request
↓
Reuse cached context
↓
Process new inputWhat Can Be Cached?
The most useful cached data is usually information that remains stable across many requests. The exact types supported depend on the provider and model.
| Context | Caching Potential |
|---|---|
| System instructions | High when they remain unchanged |
| Long documentation | High when reused across many requests |
| Tool definitions | High when the available tools remain stable |
| Output schemas | High when the same schema is repeatedly used |
| Conversation history | Possible, depending on the application and cache strategy |
| Current user message | Usually dynamic and not a useful shared cache target |
Prefix Caching
Many context caching systems are based on the idea of caching a prefix. A prefix is the portion of the prompt that appears at the beginning and remains unchanged across requests.
Stable prefix:
[System prompt]
[Rules]
[Tool definitions]
[Reference documentation]
Dynamic suffix:
[Current user request]Keeping stable content together at the beginning of the request can make prefix caching more effective when the inference system uses prefix matching.
Cache Hits and Cache Misses
A cache hit occurs when a request contains context that can be reused from an existing cache entry. A cache miss occurs when the required context is not available or does not match the conditions required for reuse.
| Result | Meaning |
|---|---|
| Cache hit | Reusable context was found and can be reused |
| Partial hit | Only part of the expected context could be reused |
| Cache miss | The required reusable context was not available |
A high cache hit rate is generally desirable when caching is intended to reduce repeated processing. However, the cache should not be designed simply to maximize hits. Cached data also consumes resources and may become stale.
How Prompt Structure Affects Caching
Applications should separate stable and dynamic information when possible. If frequently changing content is placed inside the reusable prefix, small changes may prevent the larger context from matching the cache.
Better for prefix caching:
[Stable instructions]
[Stable tools]
[Stable documentation]
[Dynamic user input]
Less cache-friendly:
[Dynamic data]
[Stable instructions]
[Stable documentation]Context Caching and Latency
Large prompts can increase the amount of work required during the input or prefill phase of inference. Reusing previously processed context can therefore reduce some of the work required before generation begins.
The actual latency improvement depends on the inference system, cache implementation, prompt size, cache hit rate, and network behavior. Caching should therefore be benchmarked rather than assumed to produce a specific speedup.
Context Caching and API Costs
Some model providers offer lower pricing for cached input compared with regular input tokens. When a large prompt prefix is reused frequently, this can reduce the cost of repeated requests.
Pricing models differ between providers and can change over time. Developers should check the current pricing and caching rules of the specific API they use instead of assuming that every caching system provides a discount.
Context Caching vs Response Caching
Context caching and response caching solve different problems. Context caching reuses processed input, while response caching avoids making an LLM request when the same result can safely be reused.
| Technique | What Is Reused? |
|---|---|
| Context caching | Previously processed input context |
| Response caching | The completed result of a previous request |
Response caching can eliminate model computation completely for a cache hit, but it is only appropriate when returning the previous answer is safe and correct. Context caching is more flexible because the dynamic part of the request can still change.
Context Caching vs KV Cache
Context caching and the KV cache are related but should not be treated as the same thing. The KV cache is an inference mechanism that stores attention keys and values for active sequences during autoregressive generation. Context caching is a broader application or serving strategy for reusing previously processed context.
| Feature | Context Caching | KV Cache |
|---|---|---|
| Primary purpose | Reuse repeated context | Avoid recomputing attention state during generation |
| Typical scope | Across compatible requests or sessions | Within active inference sequences |
| Main benefit | Reduce repeated input processing | Improve efficient autoregressive decoding |
| Memory implications | Depends on cache implementation | Can grow significantly with context and concurrency |
Context Caching and Long Conversations
Chat applications often send conversation history with every request. Caching can help when a large portion of that history remains unchanged between consecutive requests.
However, conversations are naturally dynamic. Every new message changes the sequence, so the exact cache behavior depends on how the inference system matches and reuses prefixes.
- Keep stable system instructions unchanged.
- Avoid unnecessarily rebuilding the entire prompt.
- Preserve reusable prefixes when supported.
- Summarize old history when the conversation becomes very large.
- Monitor cache hit rates for long-running sessions.
Context Caching for RAG
Retrieval-augmented generation applications can contain large amounts of reference information. If the same documentation or knowledge base content is repeatedly supplied to the model, caching may reduce repeated processing.
However, sending an entire knowledge base as a permanent context is usually inefficient. Retrieval should still select relevant information. Caching is most useful when a substantial context genuinely needs to be reused across many requests.
Context Caching for AI Agents
AI agents often use long system instructions, tool definitions, policies, and workflow descriptions. These elements may remain stable while the user's task and tool results change.
Caching the stable portion can reduce repeated processing, but rapidly changing tool results should generally remain outside the stable cacheable prefix.
Cache Expiration
Cached context cannot necessarily be retained forever. Depending on the system, cached entries may expire after a period of inactivity or may be removed when resources are needed for other requests.
Applications should not assume that a cache entry will always be available. The application must continue to work correctly when a cache miss occurs.
Cache Invalidation
Cache invalidation becomes important when cached information changes. If an application continues using outdated instructions, documentation, policies, or other data, the model may produce responses based on stale context.
- Invalidate cached context when its source changes.
- Use version identifiers for changing documents.
- Separate stable and frequently changing information.
- Define expiration policies where appropriate.
- Never assume cached data is permanently current.
Versioning Cached Context
A practical strategy is to associate reusable context with a version. When instructions or documentation change, the version changes as well, creating a new cacheable context instead of continuing to reuse outdated information.
Documentation v1 -> Cache A
Documentation v2 -> Cache B
Documentation v3 -> Cache CVersioning makes changes explicit and reduces the risk of accidentally using obsolete cached information.
Memory Trade-Offs
Caching can reduce repeated computation, but cached information consumes resources. The larger the cached context and the more entries retained simultaneously, the greater the memory requirements can become.
- Set reasonable cache limits.
- Evict rarely used entries.
- Monitor cache memory usage.
- Prefer frequently reused context.
- Avoid caching very large contexts with little reuse.
- Consider the cost of retaining cache entries versus recomputing them.
When Context Caching Is Most Useful
Context caching provides the most value when a large amount of input remains stable across many requests.
- Large system prompts.
- Repeated documentation.
- Stable tool definitions.
- Long-lived AI agents.
- Applications with repeated interactions against the same knowledge.
- High-volume API workloads with common prompt prefixes.
- Applications where repeated input processing contributes significantly to latency or cost.
When Context Caching May Not Help
Caching is less useful when requests are highly unique or when the reusable portion of the prompt is very small.
- Every request has a completely different context.
- The reusable prefix is too small to provide meaningful savings.
- Cached entries expire before they are reused.
- The application frequently changes the supposedly stable context.
- Cache management overhead exceeds the benefit.
- The provider or runtime does not support the required caching behavior.
How to Design a Cache-Friendly Prompt
A cache-friendly prompt separates stable information from dynamic information. The stable section should change as rarely as possible, while user-specific data and current results should be placed later when the caching system uses prefix matching.
[Stable]
System instructions
Output rules
Tool definitions
Reference material
[Dynamic]
Conversation updates
Current user request
Current tool resultsMeasuring Context Caching Performance
Caching should be evaluated using real workload measurements. A cache that sounds useful conceptually may have little effect if cache hits are rare or the reusable context is small.
| Metric | Why It Matters |
|---|---|
| Cache hit rate | Shows how often reusable context is actually reused |
| Input processing latency | Shows whether repeated processing has been reduced |
| Time to first token | Shows the effect on interactive responsiveness |
| Input token cost | Shows potential API cost reduction |
| Cache memory usage | Shows the resource cost of retaining cached data |
Common Context Caching Mistakes
- Assuming every repeated prompt automatically produces a cache hit.
- Changing the supposedly stable prefix unnecessarily.
- Putting dynamic content inside a cache-sensitive prefix.
- Caching information without an invalidation strategy.
- Ignoring cache expiration.
- Caching large contexts that are rarely reused.
- Ignoring memory consumption.
- Assuming cached input is always cheaper.
- Measuring only total latency and not input processing time.
- Treating context caching and response caching as the same technique.
- Relying on cached data that may become outdated.
Best Practices for Context Caching
- Identify context that is genuinely reused.
- Keep stable prefixes stable.
- Separate static and dynamic prompt content.
- Use versioning for changing cached data.
- Define appropriate expiration and invalidation behavior.
- Monitor cache hit rates.
- Monitor cache memory usage.
- Measure latency before and after caching.
- Measure actual API costs instead of assuming savings.
- Keep the application correct when a cache miss occurs.
- Avoid caching data that changes too frequently.
- Re-evaluate the cache strategy as the application's workload changes.
Frequently Asked Questions
What is context caching in LLMs?
Context caching is a technique for reusing previously processed input context across compatible LLM requests. It can reduce repeated computation and, depending on the provider, may reduce latency or input costs.
What is prompt caching?
Prompt caching is a form of context caching where reusable parts of a prompt, often a stable prefix, are processed once and reused across later requests.
Does context caching reduce LLM latency?
It can reduce the time required to process repeated input context. The actual improvement depends on the provider, cache implementation, prompt size, cache hit rate, and workload.
Does context caching reduce API costs?
It can when the provider offers lower pricing for cached input or when caching avoids repeated computation. Pricing and eligibility vary by provider, so current API pricing should be checked before estimating savings.
What is the difference between context caching and response caching?
Context caching reuses processed input while still allowing a new request to be generated. Response caching reuses a completed result and can avoid the model call entirely when the cached answer is valid.
Can cached LLM context become outdated?
Yes. Cached instructions, documentation, or other information can become stale when the source changes. Versioning, expiration, and invalidation strategies help prevent outdated context from being reused.
Conclusion
Context caching is an important optimization for LLM applications that repeatedly process the same large input context. By reusing stable prefixes or other previously processed information, applications can potentially reduce input processing latency and API costs.
The technique is especially useful for large system prompts, documentation, tool definitions, AI agents, long-running conversations, and other workloads with substantial repeated context. Its effectiveness depends on how often the context is reused and how the specific inference system implements caching.
A good caching strategy keeps stable content separate from dynamic data, monitors cache hits and memory usage, handles expiration and invalidation, and continues to work correctly when a cache miss occurs. When measured against a real workload, context caching can become a practical way to improve both the efficiency and responsiveness of LLM applications.