What Is Prompt Caching? Prompt Caching Explained

Published Updated 44 viewsPrompt CachingCachingCost Optimization

Prompt Caching is an APIlevel optimization technique that lets an identical prompt prefix be processed only once across multiple requests, with subsequent calls

Prompt Caching is an API-level optimization technique that lets an identical prompt prefix be processed only once across multiple requests, with subsequent calls directly reusing the cached result, thereby substantially reducing cost and latency. Both Anthropic and OpenAI already offer this feature in their APIs.

Ingredients prepared in advance and reused, saving time on each new order

Ingredients prepared in advance and reused, saving time on each new order

Grab It in One Sentence First

Prompt Caching turns "re-reading a long document every time" into "read it once, cache it, and then jump straight to the new content."

A cache hit reuses computation for the repeated prefix, but cache reads are usually still billed. Total savings depend on hit rate, prefix size, write and read prices, and output-token charges. A prefix discount is not a discount on the entire bill.

How It Works

When a large language model processes input tokens, it internally generates a series of intermediate computation results (called the KV Cache in the Transformer architecture). The principle of Prompt Caching is: if this request's prefix is completely identical to the previous one, the model provider's infrastructure can skip this repeated computation and continue processing the newly added content directly from the cached intermediate state.

Keep the reusable prefix identical. A change affects matching from that point onward, but an earlier stable prefix may still hit; it does not necessarily invalidate caching for the whole request. Put fixed material first and changing fields last.

Minimum cache length varies by model. Claude Messages supports automatic caching configured at the request level as well as explicit block breakpoints; supported OpenAI models attempt caching automatically. Check the current documentation for the selected model and endpoint.

Which Scenarios Are Most Worth It

Applications with a long system prompt: If your system prompt contains thousands of words of role setup, rules, and format requirements, caching can substantially lower the cost of every conversation.

Document Q&A: each request still sends the document and question. A matching prefix is billed at the cache-read rate, and output is billed separately.

Fixed context in multi-turn conversations: In a conversation system, the history is usually incremental, but you can mark the fixed part (such as the system prompt and early conversation) as the cached prefix and have the model process only the newly added content.

Code review and codebase Q&A: unchanged file prefixes can reuse cached computation. New questions, uncached input, and generated answers retain their respective charges.

Changes in Cost and Latency

For Claude, cache writes carry a premium and hits still incur read charges. Read discounts vary by model. Calculate writes, reads, uncached input, and output separately, then use observed reuse to determine whether caching pays off.

On latency, a cache-hit response is usually also faster, because a large amount of computation is skipped. For requests containing long documents, the latency improvement is especially noticeable.

Considerations When Using It

The cache has a lifespan. Claude defaults to a 5-minute TTL and offers a differently priced 1-hour TTL. Hits refresh the lifetime. Rebuilding after expiry incurs write charges again, so compare TTL costs for infrequent requests.

Dynamic content isn't suitable for the cached prefix. If the system prompt contains the current time, a user ID, or variables that change each time, the cache will invalidate frequently. Move the dynamic parts to the end of the prompt and cache only the stable prefix.

Order matters: fixed content must come first, dynamic content last. Reversing them makes the cache completely ineffective.

Not all models support it. When a new model is released, you need to separately confirm whether caching is available, and the pricing and parameters may also differ from older versions.

Sources