Prefix Caching vs Semantic Caching: Which Fits Your App?

Prefix caching and semantic caching both cut LLM costs and latency - but they work at completely different layers. Here's how to choose, and when to run both.

MK

Mohammed Kafeel

Machine Learning Researcher

June 18, 202613 min read
On this page

Your LLM is burning money on tokens it already processed yesterday. Up to 90% of that cost is recoverable - but only if you pick the right caching layer. Most teams pick one and leave the other on the table.

This post breaks down the prefix caching vs semantic caching decision with precision: how each works, where each fails, real numbers from production systems, and a clear framework for choosing - or combining - both.


TL;DR

  • Prefix caching (a.k.a. prompt caching) reuses the model's internal KV cache for identical prompt prefixes. It cuts token computation cost by up to 90% and latency by up to 85%.
  • Semantic caching intercepts the request before the LLM is called, returning a stored response for semantically similar queries. On cache hits, it saves 100% of inference cost.
  • They operate at different layers: prefix caching lives inside the inference engine; semantic caching lives at the application layer.
  • Neither replaces the other. The best production systems run both.
  • Your workload type - repeated context vs. repeated intent - determines which delivers more value first.

What Is Prefix Caching?

Prefix caching reuses the model's computed KV cache across requests that share an identical prompt prefix. Instead of reprocessing the same 10,000-token system prompt for every user, the inference engine stores the intermediate attention states once and reads them on every subsequent hit.

The mechanism runs entirely inside the inference layer:

  1. During the prefill phase, the model runs a forward pass over the full input and builds a key-value (KV) cache for each token.
  2. For a new request that shares the same prefix, the engine skips the forward pass for the cached portion and resumes from the last cached token.
  3. Only the new, unique tokens get computed.

The hard constraint: the prefix must be byte-for-byte identical. A single extra space, a reformatted date, or a shuffled JSON key breaks the cache entirely.

Where prefix caching runs today

All three major open-source inference frameworks support it natively:

  • vLLM - Automatic Prefix Caching (APC) via hash-based block matching, auto-enabled since v0.4.x
  • SGLang - RadixAttention, a radix-tree approach that finds the longest common prefix across all cached requests globally; eliminates 70–90% of prefill computation in RAG-heavy workloads
  • TensorRT-LLM - Proprietary cross-request KV reuse; benchmarks show ~34.7% throughput improvement over baseline

On the API side, Anthropic's Claude offers prompt caching with two TTL options: 5 minutes (default, 1.25× write cost, 90% read discount) and 60 minutes (opt-in via cache_control, 2× write cost, same 90% read discount). Google Gemini discounts cached tokens separately. OpenAI applies automatic prefix caching on eligible models.

Real numbers from Anthropic: up to 90% cost reduction and 85% latency reduction on long prompts. For a 100K-token session with a 90% cache hit rate, cost drops from ~$100 to ~$19 - an 81% saving.

What prefix caching is not

It is not semantic caching. It does not understand meaning. It does not skip the LLM call. It just skips recomputing tokens it has already seen - exactly as written.


What Is Semantic Caching?

Semantic caching intercepts the request before the LLM is called and returns a stored response if a semantically similar query already has a cached answer.

It operates at the application layer, not inside the model. The flow:

  1. The incoming prompt is converted into a vector embedding.
  2. That embedding is compared against a vector index of previously cached prompts.
  3. If the cosine similarity score exceeds a configured threshold (typically 0.85–0.90 in production), the cached response is returned immediately.
  4. If no match is found, the request goes to the LLM, and the new response is stored for future reuse.

The result on a cache hit: zero tokens consumed, zero inference latency, zero API cost.

The numbers

  • Latency: Redis reports ~15× speedup in some workloads. Response times drop from 1.2–2.5 seconds to 200–400ms on cache hits.
  • Cost: 40–80% reduction in LLM API calls in typical production deployments. In FAQ-heavy scenarios, this approaches 90%.
  • Cache hit rates: 30–70% for customer support, knowledge-base, and RAG-style workloads where users rephrase the same underlying question.

The similarity threshold trade-off

This is the lever that determines accuracy vs. efficiency:

  • Below 0.75: High hit rate, high risk of returning irrelevant answers.
  • 0.85–0.90: Recommended production range - catches paraphrases, maintains accuracy.
  • Above 0.95: Near-identical match only; safe but low hit rate.

Tools like GPTCache (open-source), Redis LangCache, and Portkey provide managed semantic caching with configurable thresholds, TTL policies, and vector store backends (Redis, Qdrant, Pinecone).

What semantic caching is not

It is not prefix caching. It does not touch the model's internal states. It does not help when users ask genuinely new questions. And it requires careful cache invalidation - stale cached answers are a real production risk when underlying data changes.


Prefix Caching vs Semantic Caching: Full Comparison Table

Dimension Prefix Caching Semantic Caching
What is cached KV cache (model's internal attention states) Full input/output response pair
Match condition Byte-for-byte identical prefix Semantic similarity (embedding cosine score)
Where it runs Inside the inference engine Application layer (pre-LLM intercept)
LLM call made? Yes - but prefix tokens are skipped No - LLM is bypassed entirely on a hit
Latency impact 13–85% TTFT reduction Up to 15× speedup; sub-400ms on hits
Cost impact Up to 90% reduction on cached token reads Up to 100% savings on cache hits
Handles paraphrasing? No - any text change breaks the cache Yes - designed for varied phrasing
Complexity Low - often automatic in vLLM/SGLang/TRT-LLM Medium - requires embeddings + vector store
Staleness risk None - purely a compute optimization Real - cached answers can go stale
Best use cases Long shared system prompts, document QA, multi-turn agents FAQs, customer support, knowledge-base lookups, RAG pipelines
Failure mode Any character change invalidates the cache Wrong answer returned if threshold is too low
Provider support Anthropic, OpenAI, Google, vLLM, SGLang, TRT-LLM GPTCache, Redis LangCache, Portkey, custom middleware

Which Should You Choose? A 3-Scenario Decision Framework

01 - Your app has a large, fixed context that many users share

Choose prefix caching first.

Think: a 50,000-token legal contract that 500 users query daily. Or an enterprise knowledge base loaded as context on every agent turn. Or a RAG pipeline where the retrieved documents are the same for a given topic.

In these workloads, the prefix is the cost center. Prefix caching eliminates that cost without any risk of wrong answers - it's a pure compute optimization. Structure your prompts so static content (system instructions, documents, examples) comes first, and dynamic content (user query) comes last. That maximizes cache hit rate.

When prefix caching alone is enough: your users ask genuinely different questions about the same context. Semantic caching won't help much here because there's low query repetition.

02 - Your app handles high-volume, repetitive user queries

Choose semantic caching first.

Think: a customer support chatbot where 60% of tickets are variations of "how do I reset my password," "I can't log in," and "what's the refund policy." Or an internal HR assistant where employees ask the same policy questions in different words every week.

Here, the query is the cost center. Semantic caching eliminates the LLM call entirely on hits. A well-tuned cache with a 0.87 similarity threshold can absorb 40–70% of traffic before a single token is sent to the model. (When intents span several distinct topics, semantic caching for heterogeneous workloads is worth a look.)

When semantic caching alone is enough: your context is short and changes frequently. Prefix caching won't find many hits if the system prompt is dynamic.

03 - You're running a multi-step AI agent with long context and repetitive sub-tasks

Deploy both. This is the enterprise AI agent scenario.

Agents accumulate context across turns - tool outputs, memory, retrieved documents. That context is often shared across parallel agent runs or repeated workflow steps. Prefix caching handles the shared context. Meanwhile, agents frequently re-ask similar sub-questions ("summarize this section," "extract entities from this paragraph") across different runs. Semantic caching handles those.

Combined savings in this scenario can exceed 80% compared to a naive implementation with no caching.


Can You Use Both? The Hybrid Double-Caching Architecture

Yes. And for production AI agents, you almost certainly should. (We dig into stacking prefix and semantic caching together as a full multi-tier setup.)

The architecture is a two-tier intercept:

Tier 1 - Semantic cache (application layer) Every incoming request hits the semantic cache first. If the intent matches a cached response above the similarity threshold, return it immediately. The LLM never sees the request. Cost: zero tokens.

Tier 2 - Prefix cache (inference layer) If the semantic cache misses, the request goes to the LLM. If that request shares a long prefix (system prompt, retrieved context) with recent traffic, the inference engine reuses the cached KV states. Cost: new tokens only, not the full prompt.

Concrete example - enterprise customer support agent:

  • The agent loads a 20,000-token knowledge base on every request. Prefix caching ensures that knowledge base is processed once, not thousands of times per day.
  • 55% of incoming queries are variations of 12 common issues. Semantic caching returns stored answers for those without touching the model.
  • Only genuinely new, complex queries reach the LLM with full computation.

The math: prefix caching might cover 70%+ of input tokens; semantic caching might deflect 40–55% of total requests. Combined, you're looking at 80–90% cost reduction vs. a no-cache baseline.

Implementation note: place static content before dynamic content in your prompts to maximize prefix cache hits. Avoid timestamps, request IDs, or per-request variables in the shared prefix - they break the cache on every call.


How to Implement Each in Practice

Prefix caching implementation checklist

  • Front-load static content. System prompt → retrieved documents → few-shot examples → user query. Always.
  • Use deterministic serialization. If you're injecting JSON context, fix key ordering. Non-deterministic output = cache miss every time.
  • Avoid dynamic elements in the prefix. No timestamps, no UUIDs, no per-request metadata in the shared portion.
  • Batch similar requests. Group requests sharing the same prefix to maximize KV cache reuse across concurrent calls.
  • Monitor cache hit rate per workload. vLLM and SGLang expose this metric. Treat it as a first-class KPI. (See how prefix caching in vLLM's block-based system tracks and reports those hits.)
  • Set the right TTL. For Anthropic's API: use the default 5-minute TTL for interactive sessions; opt into 60-minute TTL for long-running agentic tasks or batch workflows with gaps between steps.

Semantic caching implementation checklist

  • Choose your vector store. Redis (sub-millisecond search), Qdrant, Pinecone - all work. Redis LangCache is the fastest managed option.
  • Tune your similarity threshold per workload. Start at 0.87. Run A/B tests. Lower it for FAQ-style apps; raise it for high-stakes or factual queries.
  • Define TTL and invalidation policies. Cached answers go stale. Set TTLs based on how frequently your underlying data changes.
  • Cache at the gateway layer, not the application layer. A centralized semantic cache shared across services gets dramatically higher hit rates than per-application caches. At the gateway, semantic routing as a complement to prefix caching can steer requests to the right model or cache before they ever hit inference.
  • Log cache hits, misses, and savings. You can't optimize what you can't measure. Track token savings and latency delta per cached query.

Key Takeaways

The bottom line on prefix caching vs semantic caching:

  • Prefix caching = model-layer optimization. Saves compute on repeated context. Requires exact prefix match. Up to 90% cost reduction on cached tokens. Supported natively by vLLM, SGLang, TensorRT-LLM, Anthropic, OpenAI, and Google.
  • Semantic caching = application-layer optimization. Skips the LLM entirely for repeated intent. Handles paraphrasing. Up to 15× latency speedup. Requires a vector store and threshold tuning.
  • They are not competitors. They solve different problems at different layers.
  • For AI agents: run both. Prefix caching handles shared context; semantic caching handles repeated sub-tasks.
  • The single most impactful prompt engineering decision for prefix caching: put static content first, dynamic content last.
  • The single most impactful tuning decision for semantic caching: calibrate your similarity threshold to your domain. 0.85–0.90 is the production sweet spot for most apps.

FAQ

What is the difference between prefix caching and semantic caching?

Prefix caching reuses the model's internal KV cache (computed attention states) for requests that share an identical prompt prefix. It reduces the cost of processing repeated context but still calls the LLM. Semantic caching stores complete input/output pairs and returns cached responses for semantically similar queries - bypassing the LLM entirely on a hit. Prefix caching operates inside the inference engine; semantic caching operates at the application layer.

What is a KV cache in LLMs?

A KV cache (Key-Value cache) stores the intermediate attention states computed during the prefill phase of LLM inference. During decoding, the model reuses these cached states instead of recomputing them for every new token. Prefix caching extends this concept across multiple requests: if two requests share the same prefix, the second request reuses the KV cache built by the first.

Does prefix caching work with paraphrased prompts?

No. Prefix caching requires a byte-for-byte identical prefix. A single character difference - a space, a punctuation mark, a reordered JSON key - invalidates the cache. For handling paraphrased or semantically similar queries, semantic caching is the right tool.

What is prompt caching and how does it relate to prefix caching?

Prompt caching is the term Anthropic uses for its implementation of prefix caching in the Claude API. The terms are used interchangeably in practice. Both refer to caching the KV states of a prompt prefix so that subsequent requests sharing that prefix skip recomputation. Anthropic's prompt caching offers up to 90% cost reduction on cached token reads and 85% latency reduction for long prompts.

When should I use semantic caching vs prefix caching for AI agent latency?

Use prefix caching when your agent loads large, shared context (documents, knowledge bases, system instructions) on every turn. Use semantic caching when your agent handles repetitive user-facing queries with similar intent. For multi-step enterprise agents, deploy both: prefix caching reduces the cost of shared context; semantic caching deflects repeated sub-queries before they reach the model. Combined, the two layers can reduce AI agent latency and cost by 80–90% vs. a no-cache baseline.

What similarity threshold should I use for semantic caching?

The recommended production range is 0.85–0.90 cosine similarity. Below 0.75 risks returning irrelevant cached answers. Above 0.95 catches near-identical matches only, leaving most paraphrases uncached. The right threshold depends on your domain: stricter for legal or medical queries, more permissive for FAQ-style customer support. Always A/B test threshold changes against real traffic before deploying.


Useful Sources


What caching strategy are you running in production? Drop a comment below - we read every one. If you're evaluating LLM inference optimization for an enterprise AI agent platform, is a good next read.