Applied AI

Prompt caching: why a stable prefix matters more than a repeated question

Prompt caching: why a stable prefix matters more than a repeated question
Written byVien Hong
Published
Reading time4 min

A document assistant can receive the same instructions and reference material on every request while answering a different question each time. Prompt caching can reuse some of the computation behind that repeated input. Asking the same question again is not the requirement, and receiving the same answer is not the promise.

The useful engineering question is where the unchanged part of a request ends. Once that boundary is clear, you can check whether the chosen model will actually reuse it.

What prompt caching reuses

OpenAI's prompt-caching guide describes retaining key-value, or KV, state for an unchanged input prefix. These are intermediate tensors from processing the input, rather than a stored answer or a copy of the original tokens.

A matching later request can reuse that state. The model still processes new input and generates a response. OpenAI explicitly says caching does not change output-token generation or guarantee identical outputs for identical requests.

That distinction affects how you design an application. An answer cache would return an earlier response. Prompt caching lets a new response use computation already completed for matching input. Treat it as an input-processing optimization, not a mechanism for making answers deterministic.

Matching input cards reuse a shared computation block before branching into different response windows

Put reusable material before changing input

Consider a hypothetical document-analysis assistant. Each request needs the same task instructions and reference document, followed by a question about that document. A sensible starting layout is:

  • Stable instructions describing the task.
  • Stable reference material needed for the answer.
  • The current question and any request-specific details.

This arrangement gives the API an unchanged beginning to match. Anthropic's documentation recommends static material first and describes its prefix order as tools, then system, then messages. The request's structure matters, not just the order of paragraphs in one text field.

A timestamp placed ahead of the reference document changes that beginning on every request. Moving genuinely variable details after reusable material is therefore worth considering. This is a proposed layout, not a measured performance result, and it must still preserve the information and ordering the task needs.

The word "stable" should describe content that is actually unchanged. Do not freeze a document that needs updating or remove relevant context just to improve cache reuse. Start by separating reusable material from changing input, then check the provider's eligibility rules.

Two request rows compare an early changing clock icon with a stable document prefix followed by a changing question card

Matching text is only part of a cache hit

OpenAI documents that the entire rendered prefix must match. Tool names, descriptions, schemas, and ordering can affect that prefix. Its guide also identifies the model and certain generation or context settings as relevant. A visually unchanged reference document is therefore not enough evidence that two complete requests match.

There is a less obvious boundary issue, too. In OpenAI's newer documented behavior, extending an existing message can remove the previous message-end lookup boundary. The earlier text may still be present, yet the request may no longer offer the same endpoint for cache lookup.

The guide recommends preserving the earlier message and appending another message when appropriate, or using a matching explicit content-block breakpoint. This is a reason to inspect message construction when reuse disappears, rather than only comparing the visible text.

Anthropic documents a related trap: putting a breakpoint only after a changing timestamp or user-input block can produce fresh cache writes without cache reads. Backward lookup finds entries previously written; it does not discover arbitrary stable text. Its recommended fix is an explicit breakpoint at the end of the stable prefix. The Anthropic guide explains that distinction in detail.

Check the rules for the exact model

Cache configuration is not interchangeable across APIs. OpenAI's guide documents a minimum cacheable prompt length of 1,024 tokens for GPT-5.6 and later, while earlier models have setting-dependent differences. It supports implicit and explicit caching for GPT-5.6 and later. In explicit-only mode, omitting explicit breakpoints means no cache reads or writes.

Anthropic supports top-level automatic caching through cache_control as well as explicit block-level breakpoints. Its default time to live is five minutes, with a one-hour option. Reads refresh the default TTL without an additional refresh charge, and the TTL starts when the request starts, not when the response finishes.

Billing also needs a provider-specific check. Anthropic documents five-minute writes at 1.25 times base input and one-hour writes at twice base input; cache-read multipliers vary by model. Those are Anthropic's rules, not a general pricing formula for prompt caching.

Before estimating savings, write down the exact model, minimum eligible length, activation mode, lookup boundaries, retention, and read/write charges. A correct prefix layout cannot compensate for an ineligible request or an expired entry.

A reusable prefix passes through separate eligibility, boundary, and expiration checkpoints

Measure reads and writes, not repetition

A repeated prompt is a test input, not proof of a cache hit. Anthropic exposes cache_creation_input_tokens and cache_read_input_tokens for monitoring. Record both so that repeated writes do not look like successful reuse.

OpenAI's Prompt Caching 201 recommends monitoring cached tokens, cost, and latency. That page is archived and explicitly limited to models before GPT-5.6. Its measurement principle is useful, but its older pricing and retention statements should not be applied to newer models.

A proposed evaluation can start with a small set of representative questions against unchanged reference material. Compare the initial request with later requests, then deliberately change an early input block. Record actual cache reads and writes, total input cost, and time to first token. These are suggested checks; no API experiment or benchmark is reported here.

Judge the result by the workload's cost and response timing, rather than hit rate alone. Frequent writes with little subsequent reuse deserve a different decision from repeated reads of a useful prefix.

Start with one request layout

Choose one repeated workflow and mark the last point where its input stays unchanged. Inspect everything before that point, including tool definitions and message boundaries. Place the reusable material first where the task permits, then configure an eligible cache boundary for the exact model.

Before extending that layout across an application, confirm a cache read in the provider's usage data and compare total input cost and response timing. That gives you evidence for the next change instead of an assumption that repeated text must be saving work.

Sources

More from the blog

View all