Curious Minds

Clear answers to science and history questions

Semantic vs Prompt Caching: Cost, Latency, Quality

· 7 min read · Science

Semantic caching and prompt caching both reduce cost and latency for LLM applications, but they differ fundamentally in how they match requests. Prompt caching reuses the exact same text prefix, while semantic caching matches by meaning, even when wording differs. This article compares the two on cost, latency, and quality, and explains when to use each.

in t | Total tokens per request | tokens | 4000 | 1 | 1000000
in c | Cache hit rate (%) | % | 50 | 0 | 100
in p | Price per 1M tokens (USD) | USD | 3 | 0 | 1000
out cost | Cost per request (USD) | USD | (t * (1 - c/100) * p / 1000000) + (t * c/100 * (p * 0.1) / 1000000) | 4
out saved | Savings vs no cache (USD) | USD | t * p / 1000000 - ((t * (1 - c/100) * p / 1000000) + (t * c/100 * (p * 0.1) / 1000000)) | 4

Formula

Cost per request = (tokens × (1 − hit rate) × price) + (tokens × hit rate × price × 0.1). The 0.1 represents a typical 90% discount on cached input tokens (common with major providers, but check your provider). Savings = cost without cache − cost with cache.

Variables: t = total tokens per request (input + output), c = cache hit rate (%), p = price per 1M tokens (USD). The output is in USD per request.

Worked Example

Using the defaults: t=4000 tokens, c=50%, p=$3. Cost without cache = 4000 × 3 / 1,000,000 = $0.012. With cache: cached portion = 4000 × 0.5 = 2000 tokens at 10% price = 2000 × 0.3 / 1,000,000 = $0.0006. Uncached portion = 2000 tokens at full price = $0.006. Total = $0.0066. Savings = $0.012 − $0.0066 = $0.0054. The calculator shows the same.

What Is Prompt Caching?

Prompt caching (also called prefix caching or context caching) stores the exact token sequence of a prompt prefix. When a new request arrives, the provider checks if the beginning of the prompt matches a stored prefix. If yes, the system skips recomputing the key-value (KV) cache for that prefix, which is the most compute-heavy part of inference.

This works best when you have a long, static system prompt, few-shot examples, or a large document that is reused verbatim. For example, a chatbot that always starts with a 2,000-token instruction set will get a high hit rate if the instruction never changes.

Providers typically charge a reduced price for cached input tokens—often 10% of the normal input price—and the latency benefit is proportional to the length of the cached prefix. The longer the prefix, the more time saved.

What Is Semantic Caching?

Semantic caching goes beyond exact matches. It uses embeddings to convert a prompt into a vector, then compares that vector to previously stored vectors using a similarity metric (like cosine similarity). If the new prompt is close enough to a cached one, the system returns the cached response without calling the LLM at all.

This is useful for applications where users ask the same question in many different phrasings. For instance, a customer support bot might get “How do I reset my password?” and “I forgot my password, what do I do?”—semantic caching would treat these as the same and serve one stored answer.

Semantic caching requires an embedding model and a vector database (or an in-memory store). The similarity threshold determines how aggressive the cache is: a high threshold (e.g., 0.95) means only near-identical prompts match, while a low threshold (e.g., 0.8) matches more broadly but risks returning irrelevant responses.

Cost Comparison

Prompt caching reduces cost by lowering the price of cached tokens. If 60% of your input tokens are cached, your input cost drops to 0.6×0.1 + 0.4×1 = 0.46 of the original, roughly a 54% saving. But you still pay for the uncached portion and for the output tokens, which are never cached.

Semantic caching can eliminate the LLM call entirely on a hit, saving 100% of the cost for that request. However, you pay for embedding generation (a small cost) and for storing and querying vectors. For high-volume, repetitive queries, semantic caching can be far cheaper. For low-volume or highly variable prompts, the overhead may not be worth it.

In practice, many systems combine both: prompt caching for the static prefix and semantic caching for the whole prompt. The cost model then becomes: embedding cost + (cached prefix cost) + (uncached LLM cost only on semantic miss).

Latency Comparison

Prompt caching reduces time-to-first-token (TTFT) because the model does not need to process the cached prefix again. The speedup is roughly proportional to the cached length. A 2,000-token prefix that takes 1 second to process might drop to 0.1 seconds if cached. The rest of the generation is unaffected.

Semantic caching adds a small latency for embedding and similarity search (typically 5–50 ms), but on a hit it avoids the entire LLM call, which can take seconds. So for a hit, semantic caching is much faster. On a miss, you pay the embedding overhead plus the full LLM time, making it slightly slower than no caching.

The net latency depends on hit rate. If your hit rate is high (say 70%), the average latency is 0.7×fast + 0.3×(slow+overhead), which is usually still better than no cache. But if hit rate is low, the overhead can make things worse.

Quality and Accuracy

Prompt caching is lossless—the model receives the exact same tokens, so output quality is identical to a non-cached run. There is no risk of stale or wrong answers.

Semantic caching can degrade quality if the threshold is too loose. A cached response might not address the specific nuances of a differently-worded question. For example, “How do I reset my password?” and “How do I reset my password on my phone?” are similar but not identical—a cached answer about desktop might be wrong for mobile.

To mitigate, you can use a high threshold, or you can store multiple responses per semantic cluster. Some systems also use a “cache only for factual, stable answers” policy, avoiding caching for creative or time-sensitive queries.

When to Use Which

  • Prompt caching: use when you have a long static prefix (system prompts, few-shot examples, document context) and the user-specific part is short. It is easy to implement—just enable it in the API—and has no quality risk.
  • Semantic caching: use when you have many similar questions with different phrasing and a high expected hit rate (e.g., FAQ bots, support portals). It requires more engineering but can save nearly all costs for repetitive traffic.
  • Hybrid: use both—prompt caching for the fixed prefix, semantic caching for the whole prompt. This is common in production systems that want maximum savings without sacrificing accuracy.

Common Problems and Misconceptions

Misconception: semantic caching always beats prompt caching. Not true—if your prompts are unique or your hit rate is low, semantic caching adds overhead without benefit. Prompt caching is often the better first step because it’s free to implement and has no downside.

Problem: cache invalidation. Both caches need to be invalidated when the underlying data changes. For prompt caching, if you update your system prompt, the old prefix is no longer used. For semantic caching, if the correct answer changes (e.g., prices, policies), you must clear or update the cache, or you’ll serve stale answers.

Problem: measuring hit rate. Providers report prompt cache hit rates in their usage logs, but semantic cache hit rates require your own instrumentation. You need to log every request and whether it was a hit or miss.

Misconception: semantic caching is only for chatbots. It works for any LLM call—code generation, summarization, classification—as long as the same prompt (or similar) recurs.

Frequently Asked Questions

Can I use both prompt caching and semantic caching together?

Yes. Many systems enable prompt caching on the API level and add a semantic cache in front. The semantic cache catches exact or near-identical full prompts, while the prompt cache handles the static prefix on the remaining calls. This combination maximizes savings.

Does prompt caching affect output quality?

No. Prompt caching is mathematically identical to processing the full prompt; the model sees the same tokens, so the output is exactly the same as without caching. There is no approximation.

How do I set the similarity threshold for semantic caching?

Start with a high threshold (0.9–0.95) and measure precision (how often the cached answer is correct). Lower the threshold gradually until you see an acceptable error rate. Monitor user feedback or run A/B tests to validate.

What is the typical cost saving from prompt caching?

If your provider charges 10% for cached input tokens and 60% of your input tokens are cached, your input cost drops to 46% of the original, a 54% saving. Output tokens are unaffected. Actual savings depend on your token mix and hit rate.

Does semantic caching work for streaming responses?

Yes, but you need to store the full response and replay it on a hit. For streaming, you can still stream the cached response token-by-token, but you must ensure the response is complete and correct before serving it as a cache hit.

Is semantic caching safe for sensitive data?

It can be, but you must store embeddings and responses securely. If you cache user-specific data, you risk leaking it to other users if your similarity matching is too loose. Use a high threshold and consider encrypting cached responses or excluding personal data from caching.

In this article
  1. Formula
  2. Worked Example
  3. What Is Prompt Caching?
  4. What Is Semantic Caching?
  5. Cost Comparison
  6. Latency Comparison
  7. Quality and Accuracy
  8. When to Use Which
  9. Common Problems and Misconceptions
  10. Frequently Asked Questions