Section 01 · Definition
What Prompt Caching Actually Is in an LLM System
Transformer models process every input token in sequence. The key value attention matrices from that computation can be stored and reused when the same prefix appears again.
Quick answer
How does LLM prompt caching work? LLM prompt caching stores and reuses the intermediate KV attention matrices from repeated prompt prefixes, so the model skips reprocessing tokens it has already seen. Provider APIs like Anthropic and OpenAI expose this mechanism for application use. An application level semantic cache adds a second layer, serving prior responses to semantically similar queries without any model call at all.
Transformer models process every input token in sequence, computing attention across the full context window each time. The intermediate computation — specifically the key value attention matrices for each layer — can be stored and reused when the same prefix appears again. That stored computation is the KV cache.
Provider level KV caching has existed since the first commercial LLM APIs launched. It is fully automatic: the provider caches frequently seen prefixes in memory for a short window, and your application benefits without any configuration changes. The catch is that you have no control over what gets cached, how long it stays cached, or what your cache hit rate actually is.
Provider prompt cache APIs give applications explicit control. You mark sections of the prompt as cacheable, the provider computes and stores the KV state for those sections, and subsequent calls that share the same marked prefix skip recomputing those tokens entirely. Anthropic and OpenAI both expose variants of this API with different mechanics and pricing.
Semantic caching sits one layer higher, in your application tier. Instead of reusing KV computation, it stores prior model responses and serves them when a new query is semantically similar enough to a cached query. No model call happens at all on a cache hit. The savings are real, but so is the correctness exposure.
Section 02 · Architecture
Three Production Caching Layers and How They Differ
The three layers target different parts of the inference cost curve and require different implementation effort.
Provider KV cache (automatic)
Every major inference provider maintains an in memory KV cache for hot prefixes. When you send a prompt, the provider checks whether it has recently computed the KV states for a matching prefix. If it has, those tokens are effectively free to process. You do not configure this, and you do not see the hit rate in standard observability tooling. The limitation is a short, unpublished cache TTL. This layer benefits workloads that send the same system prompt to the same model in rapid succession. It provides essentially zero benefit for infrequent or diverse prompts.
Provider prompt cache API (opt in)
Anthropic's prompt caching and OpenAI's automatic prompt caching make the cache explicit. You mark cache control boundaries in your prompt, and the provider computes, stores, and reuses the KV state for those sections on subsequent calls. Cached input tokens cost a fraction of standard input tokens, and TTFT drops because the model skips the attention computation for that prefix. To realize these savings, your prompt structure must be deterministic for the cacheable sections. Anything that changes between calls must appear after the cached prefix. The cache is keyed on an exact prefix match; a single character difference invalidates it.
Semantic cache (application tier)
A semantic cache sits in front of your LLM calls entirely. When a query arrives, you compute its embedding, query the cache for semantically similar prior queries, and if a hit exists above your similarity threshold, return the stored response. No model call happens. The win is significant for workloads with high query repetition: customer support bots where users ask the same questions in different words, RAG pipelines queried repeatedly for the same documents, scheduled agents running the same analysis. The implementation requires an embedding model, a vector store, a similarity threshold, and a response store.
Section 03 · Economics
What the Cost and Latency Numbers Actually Look Like
The headline numbers for provider prompt cache APIs are straightforward to establish from public documentation.
For provider prompt cache APIs, cached input tokens are priced at 10 percent of standard input token pricing (a 90 percent reduction) on Anthropic's API and at 50 percent of standard input pricing on OpenAI's. The right choice depends on your prefix size and cache hit frequency — Anthropic's economics reward long system prompts cached across many calls; OpenAI's automatic model is simpler to implement.
TTFT improvement follows the same logic. If your system prompt is 2,000 tokens and your user query is 200 tokens, and you cache the system prompt, roughly 91 percent of the input computation is skipped on a cache hit. In practice, TTFT reductions sit between 60 and 90 percent depending on the prefix size relative to the query and network conditions.
Semantic cache hit rates depend entirely on your workload. A customer support bot with a narrow FAQ surface can achieve 70 to 80 percent hit rates with a threshold of 0.92. A general purpose assistant with diverse query patterns may see 10 to 20 percent. The cost savings scale linearly with hit rate — a 70 percent hit rate on an application tier semantic cache eliminates 70 percent of model calls entirely.
The cost model for a production system with all three layers is layered: the provider KV cache reduces baseline cost for repetitive system prompts without effort; the prompt cache API compounds those savings for large fixed contexts; the semantic cache eliminates calls outright for repetitive queries. Each layer targets a different component of total inference spend.
Section 04 · Risk
Four Correctness Traps That Break Cached Systems
Caching failures are silent. The system returns a response, the user receives it, and no error is logged. The response is just wrong.
Stale context served to live queries
A semantic cache stores a response keyed to a query. If the underlying data changes — a product is discontinued, a policy is updated, a price changes — the cached response is factually incorrect. The cache does not know the data changed. The only safeguard is TTL: set a maximum age for cached responses that is shorter than your expected data change frequency. For RAG pipelines, key the cache on the query plus the document hash or retrieval timestamp.
Cross tenant PII contamination
If your semantic cache is shared across users or tenants, a cache hit for one user could serve a response containing another user's data from a prior call. A question like 'what is the status of my recent order?' should never produce a cache hit with a different user's order details — but at sufficient semantic similarity, it will if cache scoping is not enforced. The fix is mandatory: scope the cache by tenant or user identifier. This is a compliance requirement in regulated industries, not just a best practice.
Model version invalidation
Provider prompt cache APIs are keyed to a specific model version. When you upgrade your model — from an older to a newer version, or when a provider rolls out an update — all cached KV states are invalid. The provider recomputes them on the next call, which means your first batch of calls after a model update has no cache benefit and pays full latency and cost. Monitor your cache hit rates across model version transitions. A sudden drop in hit rate after an update is a model version invalidation event, not a prompt structure problem.
Similarity threshold miscalibration
Semantic caches return hits when a query exceeds a similarity threshold. Most production implementations start at 0.90 and adjust from there. The problem is that 0.90 is almost always too low for factual queries. Two questions that ask about the same topic but require different answers can easily score above 0.90 on a standard embedding model. Start at 0.95 for factual and task execution queries. Monitor cache hit rate alongside user feedback or downstream task success metrics. Lower the threshold only when quality is verified acceptable at that threshold.
Section 05 · Operations
Cache Invalidation, TTL, and PII Handling in Production
A production cache implementation needs an explicit invalidation strategy before you ship. There are three approaches, and most systems need all three.
TTL based invalidation
Set a maximum response age. Every cached response expires after a fixed duration regardless of how many times it has been returned. This is the minimum viable safeguard against staleness. For RAG pipelines, set the TTL to match your document reindex frequency. For real time data (prices, inventory, live status), the effective TTL is close to zero — semantic caching is not appropriate for these queries.
Event driven invalidation
Purge cache entries when the underlying data changes. A product catalog update triggers a cache purge for all queries about that product. This requires your cache to carry enough metadata to identify which entries are affected by a given data change. It is more precise than TTL and more complex to implement. If your data has clear change events (webhooks, database triggers, event bus messages), event driven invalidation is worth the engineering investment.
Per tenant scoping for PII safety
Every cache key is namespaced by tenant or user identifier. Cache hits are only returned within the same namespace. This is not optional for applications that handle personal or financial data. For the prompt cache API layer, the invalidation question is simpler: the cache is invalidated automatically when the prefix changes, when the model version changes, or when the provider's TTL expires. The main operational task is ensuring your cacheable prefix remains stable — factoring out the dynamic parts of your prompt into the uncached section.
Section 06 · Agentic Systems
Prompt Caching in Multi Agent Systems
Multi agent architectures create specific prompt caching challenges that simpler single call systems do not face.
Each agent in a chain typically maintains its own context window: the original task, its assigned instructions, tool call history, and observations from prior steps. When agents call LLMs independently, the system prompt for each agent is a candidate for prompt cache API caching. A five agent pipeline where each agent has 4,000 tokens in its system prompt can cache all five system prompts and save meaningful cost on every orchestration run.
The complication is context sharing. When an orchestrator agent passes its full conversation history to a subagent, that history changes with every step, which means the prompt prefix changes and the cache key changes. You need to separate the stable parts of a multi agent prompt (the agent's persona, its tool definitions, its operating constraints) from the dynamic parts (the conversation history, the current task state) and place the cache boundary between them.
For agentic cost optimization more broadly, prompt cache APIs are one of the highest-leverage levers — they require no changes to agent logic, only to prompt structure. See the Agentic AI Cost Optimization: Cut Token Spend at Scale post for the full prioritized lever stack in an agent context.
Section 07 · Exclusions
When Caching Hurts More Than It Helps
There are workload categories where adding a cache layer adds operational complexity without meaningful benefit, or actively harms quality.
Real time queries
Any query where the correct answer depends on the current time, live data, or the specific user's current state is a cache invalidation problem. Customer support bots for SaaS products should cache generic product questions but never cache queries about account state, billing, or live system status.
Highly diverse query distributions
A general purpose assistant where every user asks different questions will see semantic cache hit rates below 15 percent at a threshold of 0.92. The embedding, vector retrieval, and cache lookup overhead may exceed the LLM call savings at that hit rate. Profile your query distribution before building a semantic cache — if P95 queries are unique, the layer is not worth it.
Short contexts
Provider prompt cache APIs have minimum prefix size requirements — Anthropic requires at least 1,024 tokens in the cacheable block. If your system prompt is 300 tokens, you cannot use the prompt cache API for it. Provider level automatic KV caching will still help for repeated calls, but the explicit API savings require minimum prefix sizes that small system prompts do not meet.
High churn prompts
Prompts that change frequently because they include dynamic content like timestamps, user IDs, or real time data defeat prefix caching entirely. If your system prompt includes today's date, the cache is invalidated each day. Factor dynamic content out of the cacheable prefix or stop using the prompt cache API for those prompts.
Section 08 · Decision
Matching the Right Cache Layer to Your Workload
The right layer for each workload depends on what drives your inference cost and how stable your prompt structure is.
Provider automatic KV caching requires no effort and delivers baseline savings for any workload with repeated system prompts — enable it by default by structuring your prompts with a stable system prefix. Prompt cache APIs are worth the implementation cost for any workload with a large, stable context (long system prompts, large tool definitions, retrieved documents that remain constant across a session). The ROI is proportional to prefix size and call volume.
Semantic caching is worth building for workloads with demonstrably repetitive query patterns. Measure your actual query distribution first. If more than 30 percent of queries are semantically similar to a prior query in the last 24 hours, the semantic cache will pay for itself. Below that threshold, the embedding and retrieval overhead may not justify the complexity.
The combination of all three layers — provider KV cache, prompt cache API for large stable prefixes, and semantic cache for repetitive queries — can reduce total inference cost by 70 to 90 percent for the right workloads. Measure each layer independently so you know which lever is driving the savings. For a deeper look at the full cost optimization surface including context compression, model routing, and memory pruning, the LLM Inference Cost Optimization: A Production Playbook is the companion reference. For the application level semantic cache layer specifically, LLM Semantic Caching: Cut Latency and Cost at Scale goes deeper on embedding choice, threshold calibration, and hit rate monitoring.
If you are architecting LLM cost and latency infrastructure across a production system and want a second opinion on your caching strategy, that is a standard part of an AI Systems Architecture engagement.