Skip to content
Back to log

How prompt caching works and when it helps

Chainabit's Chao · · 7 min read

In short

Prompt caching reuses the processed state of the prompt prefix shared across requests. This post focuses on breakpoint placement, content matching, and use cases.

Prompt caching reuses the processed state of the start of a prompt that is shared across multiple requests. This shared start is called the prefix. OpenAI explains that the state stored for a matching prefix can be used in a subsequent request. New input that comes after the prefix is processed normally.

Anthropic lists prompts with many examples, large context, repeated tasks with consistent instructions, and long multi-turn conversations among the cases where prompt caching can be useful. What these cases have in common is content that can be reused in later requests.

What is reused?

According to OpenAI, while processing input tokens the model computes intermediate states called key-value (KV) states. These states let the model refer to earlier tokens when processing later input and generating output tokens. The prompt cache stores these KV states rather than the original prompt text.

When a matching cache entry is found, the model can use the stored state instead of reprocessing the prefix covered by that entry. The portion of the new request that follows the prefix still has to be processed separately. A cache hit does not mean the entire request is served from a previously generated response.

OpenAI notes that the prefix presented to the model can include parts such as developer instructions, tool definitions, and conversation history. For this reason, matching is not only about the latest user message. Earlier content and the order in which it is presented to the model are also part of the shared start.

What does a cache breakpoint determine?

A cache breakpoint marks the point where the prefix that can be written to the cache ends. In Anthropic's Messages API approach, cache_control can be added at the top level of the request or placed on specific content blocks. The top-level field enables automatic caching. The block-level marker creates an explicit cache breakpoint.

Automatic caching applies the breakpoint to the last block that can be cached. As the conversation grows, the point moves forward in subsequent requests. Content up to and including the last marked block is part of the prefix. When a later request contains the same prefix, the cached content can be reused.

An explicit breakpoint gives finer control over which section is cached. Anthropic says this approach is useful when sections that change at different frequencies need to be cached. For example, you can use an explicit breakpoint for the system prompt while using automatic caching for the conversation.

Anthropic describes placing static tool definitions, system instructions, context, and examples at the start of the prompt. The end of the content to be reused is marked with cache_control. The prefix order is tools first, then system, then messages. Each level builds on the content before it.

A breakpoint does not cover only the marked block. All content up to that point, including the marked block, becomes part of the prefix. The position of the breakpoint therefore also determines which content must stay the same in the next request.

How is a match found?

According to Anthropic, the cache write happens at the breakpoint. For a block marked with cache_control, an entry is written that carries the hash of the prefix ending at that block. The same operation does not create a separate entry for each earlier block. If any block at or before the breakpoint changes, the hash computed in the next request changes as well.

During a cache read, the system first looks for a matching entry for the prefix at the current breakpoint. If there is no match, it moves backward through earlier blocks and looks for entries written by previous requests. This lookup does not write static content to the cache by itself; it only searches for an entry that was written earlier.

In Anthropic's example, static system content is followed by a block that changes with every request. If the breakpoint is at the end of the changing block, the written prefix includes that block too. When the block changes in the next request, the prefix hash changes. If no separate entry was written at the end of the earlier static blocks, the backward lookup finds no entry for those blocks.

In the same example, the breakpoint is moved to the end of the static section. Subsequent requests can then read the shared prefix. For prompts with a static prefix and a changing final section, Anthropic recommends placing the breakpoint on the last block that stays the same across requests. Request-specific context and the incoming user message are examples of the changing final section.

Turn this insight into action.

Use the mobile app to continue with guided workflows.

Anthropic requires that, for a cache hit, the portion of the prompt up to and including the marked block be identical. This matching includes text and images. If the content changes, a changed prefix will not match an existing cache entry even if one exists.

As a conversation grows

In Anthropic's automatic caching example, the first request writes the content up to the last cacheable block. In the next request, the shared earlier portion is read from the cache and a new entry is written for the added content. Because the breakpoint moves to the last suitable block on each request, you do not need to move cache_control markers by hand as the conversation grows.

OpenAI likewise describes appending new messages in multi-turn conversations rather than rewriting earlier context. Expanding the content of an earlier user message can move a cache point that was previously at the end of the message into the middle of it. In OpenAI's example, if there is no explicit breakpoint at that boundary, the next request does not reuse the earlier prefix.

Compaction replaces earlier conversation context with a shorter representation. According to OpenAI, this can change the prefix. In that case, the first request after compaction may reuse less of the earlier cache, even if the conversation is logically unchanged.

What content can be cached?

Anthropic lists tool definitions in the tools array and content blocks in the system array among the items that can be cached. User and assistant text blocks in messages.content can also be included. Images and documents in user turns, and tool use and tool results in user or assistant turns, are also on the list.

These items can be cached through automatic caching or by placing cache_control on them. Being cacheable and finding a matching entry in a later request are separate steps. The later request must match the prefix covered by the earlier entry.

Anthropic states that thinking blocks cannot be marked directly with cache_control. Thinking blocks from earlier assistant turns can be cached along with other content. When they are read from the cache this way, they count as input tokens. Empty text blocks cannot be cached.

Sub-content blocks such as citations also cannot be cached directly. For citations, Anthropic states that the top-level block carrying the relevant content can be cached. This way the parent block can be covered by the cache without placing a separate cache_control on the sub-block.

Seeing usage in the response

Anthropic reports cache usage through the usage fields in the API response. cache_creation_input_tokens shows the number of input tokens written to the cache when a new entry is created. cache_read_input_tokens shows the number of input tokens read from the cache during the request.

The input_tokens field refers to input tokens that were neither read from the cache nor used to create a cache entry. These fields help you distinguish which content in a request corresponds to a cache read, a cache write, or normal input processing.

According to Anthropic, the minimum prompt length that can be cached depends on the model and platform. A prompt below the threshold is processed without being cached, even if it is marked with cache_control, and no error is returned. If both cache_creation_input_tokens and cache_read_input_tokens are zero, the prompt was not cached. Not meeting the length threshold is one possible reason for this.

Limits and cases where it does not fit

If the start of the prompt that is expected to be shared changes with every request, a cache hit may not occur for the same prefix. If the breakpoint is at the end of a changing section and there is no separate entry at the end of the static section, then in Anthropic's example the backward lookup finds no entry for the static section either.

For parallel requests, Anthropic states that the first cache entry becomes available only after the first response begins. Other requests sent while the entry is not yet available cannot read it.

Using the cache does not mean a previously generated response is returned again. According to OpenAI, the model uses the cached prefix to generate a new response; identical requests are not guaranteed to produce the same output. OpenAI also states that cache entries are not shared between organizations and cannot be reused across regional processing boundaries.

Ready to start?

Download the app and continue from your phone.

End of Entry