Chapter CPrompt cachingPage 2 of 8

Prompt caching

Understand the mechanism

Prompt caching becomes useful when you can predict its behavior, measure it, and name its limits.

~12 minMechanism

Before you start

Why this matters

Without looking anything up, sketch the path from input to output for Prompt caching. Circle the step where state, computation, or trust changes. The sketch can be wrong; its purpose is to make your current model testable.

1Learn the idea

Read

Follow the mechanism

Transformer inference computes attention keys and values for each prefix token. A provider can retain or recognize that prefix state and skip recomputing it on later requests whose bytes, ordering, model, and cache rules match. The changing user message belongs after the stable prefix. Cache lifetime, minimum prefix length, and discount rules are provider-specific.

Trace causality rather than memorizing vocabulary. First identify the state that exists before the operation. Next identify the computation and anything it persists. Finally identify what reaches the caller and what remains uncertain. That separation prevents a common category error: treating a convenient interface as proof that the underlying system learned, retrieved, secured, or validated something.

Here is the compact calculation to anchor the mechanism: without caching: 12.3M input tokens/day; with 90% prefix hits: 1.2M uncached prefix + 0.3M suffix = 1.5M full-price-equivalent tokens before cache-read pricing. The equation is useful only with its assumptions. Ask which quantities were measured, which were estimated, and whether an average hides a tail or subgroup. If the mechanism cannot explain a surprising metric, inspect the boundary conditions before tuning randomly.

Read

Apply it to a concrete case

A 12,000-token policy manual is shared by 1,000 daily requests; each request adds 300 unique tokens. Moving the manual before the user message makes the 12,000-token prefix reusable while the 300-token suffix stays dynamic.

The worked number is without caching: 12.3M input tokens/day; with 90% prefix hits: 1.2M uncached prefix + 0.3M suffix = 1.5M full-price-equivalent tokens before cache-read pricing. State the unit and denominator whenever you report it. A percentage without a denominator can conceal a tiny sample; a latency without a percentile can conceal slow users; a similarity score without a labeled task can conceal irrelevant neighbors. Compare the observed value with a threshold chosen before seeing the final test result.

Now test the tempting shortcut. Suppose the team optimizes only the most visible metric. The result may look better while the system becomes less trustworthy. The reason is concrete: A longer cached prefix can save more input work but may carry irrelevant context and increase uncached misses when any early token changes. Explicit caches improve control but require lifecycle management. Savings depend on repetition, provider pricing, and whether latency is dominated by input processing or output generation. This is why the decision record must include both the intended gain and the tolerated regression. If the tolerated regression is unknown, the change is not ready for a consequential workflow.

Read

Decision rules

  • Prefer a measured baseline over a persuasive demo.
  • Keep versions, inputs, and thresholds reproducible.
  • Separate syntactic success from semantic correctness and authorization.
  • Escalate or abstain when evidence falls outside the contract.
  • Re-evaluate when data, traffic, models, providers, or user goals change.

These rules turn the topic into an engineering decision rather than a slogan. They also make disagreement productive: another person can challenge the assumptions, rerun the evaluation, and reach a documented conclusion.

Read

Inspect state, not just output

At each mechanism step, annotate three things: the shape or type of data entering, the state read or written, and the possible error returned. Then ask whether rerunning that step is deterministic, probabilistic, or dependent on external state. This exposes bugs hidden by a successful final response. A useful trace includes versions and units—for example, tokens rather than characters, milliseconds at a named percentile, or vectors produced by a named embedding version. When an intermediate value cannot be observed directly, record the proxy and explain why it is informative.

Checking tutor…

Continue learning · glossary & guides