How Does LLM Prompt Caching Actually Work?

Prompt caching stores your model's internal attention state so repeated prefixes skip recomputation. Here's what that means mechanically, what each provider charges, and where it silently breaks.

Cover art for How Does LLM Prompt Caching Actually Work?

A 20,000-token system prompt sent 100 times a day costs 2 million input tokens at full price - most of them identical. Most LLM applications pay full input price on the same prefix tokens, request after request. That 20,000-token prompt sent 100 times is 2 million tokens billed at the standard input rate - the majority of them repeats. Prompt caching is the API-level feature that stops that. It is also the feature most teams configure incorrectly the first time, quietly getting 7% hit rates when they expected 80%.

This post is about what actually happens inside the model when a cache hit occurs, how the three main providers implement it differently, and the specific way agent loops break it - none of which shows up in the marketing copy.

What the model is actually storing

Prompt caching stores the computational state from an LLM's attention layers so the model can skip redundant prefill work on repeated prompt prefixes. That is a precise claim worth unpacking.

When a transformer processes a prompt, every token passes through attention layers and produces two sets of vectors - keys and values - that capture that token's relationship to everything before it. Modern systems cache the transformer's internal representations. After processing a token through the network, the model produces vectors that represent the token's state at various stages. The most important cached data is the key vectors (K) and value vectors (V), generated during self-attention.

It is worth separating this from semantic caching, which stores full prompt-response pairs and returns a saved answer for similar queries. Prompt caching does not reuse outputs. The model still runs inference on your new question. It just skips the prefill work for the prefix it has already processed.

The practical constraint that follows from this: prompt caching requires an exact byte-for-byte prefix match. Any modification to early tokens invalidates the cache for all subsequent tokens. Not approximately the same. Identical.

Prompt caching produces byte-identical outputs to uncached calls: the model processes exactly the same context, just starting from stored state rather than recomputing it.

How OpenAI, Anthropic, and Google each implement it

The three providers agree on the underlying mechanism but diverge sharply on activation, pricing, and TTL.

Provider Activation Cache read discount Min tokens TTL Write surcharge
OpenAI Automatic 50% off 1,024 ~30 min (up to 24 h on extended) None
Anthropic Explicit cache_control marker 90% off 1,024 5 min (1 h opt-in) 1.25× for 5-min; 2× for 1-hr
Google Gemini Implicit auto or explicit 75% off 32,768 Up to 24 h Storage fee per hour

OpenAI offers automatic prompt caching on GPT-4o and newer models, where caching activates automatically for prompts exceeding a minimum token threshold, with cache hits occurring only for exact prefix matches. No code change required.

Anthropic provides developer-controlled caching through explicit cache breakpoints, allowing users to specify which portions of their prompt should be cached, with configurable TTL options. In practice, you attach a "cache_control": {"type": "ephemeral"} field to a content block. You can define up to four cache breakpoints in your prompt.

Google offers both implicit caching, which activates automatically with no guaranteed cost savings, and explicit context caching, where developers create and reference caches with guaranteed discounts. The 32,768-token minimum makes it the wrong choice for a tidy 5K system prompt - that is an Anthropic or OpenAI-shaped workload.

The write surcharge on Anthropic is the detail most teams miss. With Anthropic's 5-minute cache, ten requests cost 1.7× the baseline - 83% less than ten uncached requests, and it pays off from just two requests. But a single request costs slightly more than without caching. If your traffic is bursty with long quiet gaps, the write overhead can outpace the read savings.

90%Anthropic cached read discounton tokens after the first write
50%OpenAI cached read discountfully automatic, no code change
41-80%cost reduction on agentic tasksmeasured across 500+ sessions (arXiv 2601.06007)

The part that breaks in agent loops

One team's initial cache hit rate was a dismal 7% - because the system prompt contained dynamic working memory that mutated as the agent worked, invalidating the entire cacheable prefix on nearly every step.

This is the failure mode that matters most in 2026, as agent workloads have become the dominant use case for long-context models.

Common patterns that inadvertently break the cache include timestamps, datetime strings, session identifiers, or user-specific information embedded in the system prompt. Any of these before the cache boundary means every request is effectively a cold start.

The fix is a single ordering rule. Cache invalidation is hierarchical - on Anthropic, any change to a content block invalidates that block and every block after it - so the entire optimization reduces to a single principle: put the content that never changes first, and the content that changes every request last.

On Claude, the cache is structured as tools, then system, then messages, and a change at any level invalidates everything after it. There is a less-documented wrinkle on top of that: a cache_control breakpoint searches backward through at most 20 content blocks to find an existing cache entry. In a multi-turn agent loop where each step appends tool call results, a trailing breakpoint can easily land out of range after a few turns, triggering a cache write at 1.25× instead of a read at 0.1×.

The other invalidation surface that surprises teams: prompt caches do not travel between models. A KV cache is a model-specific artifact, and some vendors' agent harnesses document explicitly that switching models mid-session invalidates the prompt cache. Teams that route cheap subtasks to a smaller model and expensive ones to a larger one routinely find their cheap path has never cached once.

Beagle in action#ai-ops channel, 10:40am
The ask
'our Claude costs jumped 3× this week but usage is flat - where did the cache go?'
Beagle drafts
pulls the last 48 hours of token metadata, spots that cache_creation_input_tokens is 94% of total input - nearly all writes, almost no reads
You approve
draft reply: 'system prompt has a session timestamp at line 1 - it invalidates the cache on every call. Move it to the last user message.'
Do this in your workspace →

Ordering your prompt to maximize hits

The practical checklist, in priority order:

  • Freeze the system prompt. Instructions, persona, tool schemas - none of this should change per-user or per-session. A single edited word anywhere before the boundary is a full miss.
  • Static content before dynamic. Tool definitions before conversation history. Documents before the user's question.
  • Timestamps go last. If you need a current timestamp, inject it into the final user message, not the system prompt.
  • Place breakpoints before volatile content, not after it. A breakpoint on the final message block of a long conversation catches nothing if 20+ blocks of tool results have accumulated since the last hit.
  • Match your TTL to your traffic pattern. The 1-hour TTL wins only when you have steady reuse beyond 5 minutes - a customer-support agent with one request every 8-12 minutes for hours at a time. Bursty traffic with 5-minute gaps between bursts may never see the longer TTL pay back its 2× write cost.

A concrete example: a support bot with a 10,000-token system prompt and tool definitions, fielding 500 questions a day. Without caching: 5 million input tokens at full rate. With a correctly placed cache_control breakpoint and steady traffic, the system prompt tokens read at 10% on Anthropic - roughly 4.5 million of those 5 million tokens drop to one-tenth the price. Cost savings range from 41% to 80% across models, while time-to-first-token improvements range from 6% to 31%. An AI teammate like Beagle, which carries a large fixed system prompt across every Slack interaction, is exactly this shape of workload.

Same support bot, same traffic, different prompt structure
Without Beagle
session ID and current timestamp in line 1 of the system prompt; every request is a cold start; cache hit rate near 0%; paying full input price on 10K tokens per call
With Beagle
stable instructions and tool schemas cached with an explicit breakpoint; dynamic fields in the final user message; 80%+ hit rate; 10K prefix tokens billing at 0.1× per call

LLM prompt caching: common questions

What is prompt caching in LLMs?

Prompt caching stores the computational state from an LLM's attention layers so the model can skip redundant prefill work on repeated prompt prefixes. It reduces cost on cached input tokens - up to 90% on Anthropic, 50% on OpenAI - and lowers time-to-first-token because the model resumes from stored state rather than recomputing it.

Does prompt caching change the model's output?

No. Prompt caching produces byte-identical outputs to uncached calls: the model processes exactly the same context, just starting from stored state rather than recomputing it. Only the prefill computation is skipped, not the generation.

How much does prompt caching actually save?

Prompt caching reduces API costs by 41-80% and improves time-to-first-token by 13-31% in real agentic workloads, per a 2026 study across 500+ agent sessions. Exact savings depend on the fraction of each request that is cacheable and how consistently traffic arrives within the TTL window.

Why is my cache hit rate so low?

The most common cause: something in the prompt prefix changes between requests. A cache hit requires the exact same sequence of tokens from the start of the prompt up to the cache boundary. If anything before that boundary changes - a timestamp, a session ID, a user name injected into the system prompt - the cache is invalidated and you pay full price. Check your API token metadata (cache_read_input_tokens) to confirm whether hits are actually occurring.

Which provider is best for prompt caching?

It depends on the workload shape. Anthropic's 90% discount requires explicit breakpoint marking and has a 1.25× first-call surcharge; it pays back hardest on agentic workloads with many reuses. Google's 75% with 24-hour TTL wins for long-document and multimodal workloads. OpenAI's 50% automatic caching suits teams that want zero configuration overhead. For most Slack and Teams bot workloads with a large stable system prompt and steady traffic, Anthropic's deeper discount wins on the math.

Or just watch me work

Point me at your website.

I will read up on your business and come back with what I would run for you. No account, no card, about a minute.

I only read what is public. Nothing is saved to your name until you say so.

Keep reading

Beagle does this work for you, in your Slack.1,000 free credits. No card.Hire Beagle