Prompt and Semantic Caching

Topics Covered

Why Cache LLM Work

Repetition in LLM traffic

Three layers of caching

What each layer saves

Latency benefits

When caching does not help

Measuring the opportunity

Where the work actually goes

The economics of repeated prefixes

Caching and quality

Caching across the request lifecycle

Choosing what to build first

Caching is not free of design work

LLM requests are expensive and slow compared with most web requests, and much of the work they do is repeated: the same system prompt on every call, the same document asked about by many users, the same question phrased slightly differently. Caching avoids repeating that work. This lesson covers the layers where caching applies, from KV cache reuse for shared prompt prefixes to exact and semantic response caches, how to estimate savings, and how to run caches safely without serving wrong or leaked answers.

Repetition in LLM traffic

Repetition shows up at several levels. At the prompt level, system prompts, tool definitions and few shot examples are identical across requests, and a chat resends its whole history every turn. At the document level, many users ask questions about the same handbook, codebase or contract. At the request level, popular questions repeat word for word, and many more repeat in meaning with different wording. Each kind of repetition can be cached, but with different mechanisms, different savings and different risks.

Requests share the system prompt. Many share a document. Some even share the question. Each layer can be cached.

Three layers of caching

Prompt or prefix caching reuses the model internal state, the KV cache, for an identical prefix, so the model skips recomputing it but still generates a fresh answer. Exact response caching stores the final answer for a request and returns it when the identical request arrives again, skipping the model entirely. Semantic caching returns a stored answer when a new request is similar in meaning to a previous one, using embeddings to judge similarity. Each step up saves more work but carries more risk of returning an answer that does not fit.

What each layer saves

Prefix caching saves prefill compute and time to first token for the cached part of the prompt, but the output is still generated, so output cost is unchanged. On APIs, cached input tokens are billed at a steep discount, often around a tenth of the normal price. Exact and semantic response caches save the whole request, input and output, and return in milliseconds instead of seconds. The hit rate of response caches is usually much lower than that of prefix caches, because whole requests repeat less often than prefixes.

Latency benefits

Caching helps latency as much as cost. Prefix cache hits cut time to first token in proportion to the cached share of the prompt; for a request with a long shared document, that can turn seconds into a fraction of a second. Response cache hits return almost instantly. For interactive products, these latency gains may matter more than the cost savings, since users notice slow first tokens immediately and a cached answer feels instant.

When caching does not help

Caching helps little when prompts are unique, such as when each request carries a different large document that is never asked about again, or when the variable part of the prompt comes first and breaks prefix matching. Response caching does not fit tasks where answers must be fresh, personalized or deliberately varied, such as creative writing with sampling. Understanding the traffic shape before investing in caching avoids building infrastructure that rarely hits.

Measuring the opportunity

Before building caches, measure repetition. For prefix caching, compute how many input tokens per request belong to stable prefixes such as system prompts and shared documents. For response caching, hash normalized requests over a week of logs and count duplicates. For semantic caching, embed a sample of requests and cluster them to see how many are near duplicates. These numbers give an upper bound on savings and show which layer is worth building first, usually prefix caching because it is safe and widely supported.

Where the work actually goes

To see why caching matters, look at where a request spends compute. Prefill processes every prompt token, and for a long prompt it dominates time to first token. Decode generates output tokens one at a time, and it dominates total latency for long answers. A typical retrieval augmented request might carry a two thousand token system prompt, three thousand tokens of retrieved documents and a fifty token question, then produce three hundred tokens of output. More than ninety percent of the input is not the question at all. Prefix caching attacks that ninety percent. Response caching attacks the whole request, including the decode work, but only when the whole request repeats.

The system prompt and documents fill most of the prompt. The question is a thin sliver at the end.

The economics of repeated prefixes

Consider an assistant that serves ten million requests a month, each with a four thousand token system prompt and tool block. That is forty billion input tokens per month spent re reading the same instructions. At three dollars per million input tokens, that alone costs one hundred twenty thousand dollars. If ninety percent of those tokens hit a prefix cache billed at a tenth of the price, the same instructions cost about twenty three thousand dollars, a saving of nearly one hundred thousand dollars a month from a change that does not alter a single answer. Numbers like these explain why prompt caching became a standard feature of every major API within a short time of its introduction.

Caching and quality

A subtle benefit of caching is that it can make longer, better prompts affordable. Teams often trim system prompts, drop few shot examples or retrieve fewer documents to control cost, accepting lower quality. When the stable parts of the prompt are cached, their marginal cost drops sharply, so richer instructions and more examples become cheap. In that sense caching is not only a cost optimization but a quality lever: it changes which prompt designs are economically viable. The same logic applies to agents, whose long tool definitions and accumulated context are resent on every step.

Caching across the request lifecycle

It helps to picture the lifecycle of a request and mark where each cache sits. The request first reaches a gateway, where an exact or semantic response cache can answer immediately. If it misses, retrieval runs, and retrieval results or embeddings may themselves come from cache. The assembled prompt then reaches the engine or API, where the prefix cache reuses internal state for the stable beginning. Finally the model decodes the answer, which may be written back to the response cache. Each layer catches a different kind of repetition, and a well designed system lets requests fall through the layers, cheapest and riskiest at the edge only where it is safe.

The response cache misses. The request moves on. The prefix cache hits, so only the tail is computed.

Choosing what to build first

The order of investment usually follows risk. Prefix caching changes nothing about the answer, so it is the first thing to turn on, and on APIs it may only require reordering the prompt. Caching deterministic intermediate results, such as embeddings and tool outputs, comes next because the model still composes the final answer. Exact response caching follows for features with real repetition and stable answers. Semantic caching comes last, for narrow, high volume features where the measured opportunity justifies the evaluation work. Teams that jump straight to semantic caching often spend weeks tuning thresholds for a few percent of traffic while leaving the much larger prefix saving untouched.

Caching is not free of design work

Even the safest cache needs design. Prompts must be restructured, keys defined, lifetimes chosen, invalidation wired to data changes, and dashboards built. Each layer also adds a component that can fail. The rest of this lesson treats each layer in turn, with the mechanics, the math and the operational practice needed to make it pay off.

Start with prefix caching

It never changes answers, it is supported by major APIs and engines, and it often cuts input cost and first token latency sharply. Build response caches only after measuring real repetition.