Book a 30-min callBook a call

Prompt caching and the KV cache: how reusing a prompt prefix cuts latency and cost

How the KV cache and prompt caching work: prefill and decode, prefix reuse in vLLM and SGLang, OpenAI, Anthropic and Gemini caching, and how to get hits.

Two request bars that open with the same long block and end with different short blocks, a store box, and a loop arrow carrying the shared block back.
Written by
BYD Tech
Published
Topic
Deep dives
Reading time
13 min read

Questions people ask

Does a changed system prompt reset the prefix?

From the first changed token onward, yes. Everything before the change can still match; everything after it is processed again and, where marked, written as a new cache entry. With Anthropic, a change to the system prompt leaves cached tool definitions valid but invalidates the system and messages segments. Keep anything that varies out of the system prompt.

Does prefix reuse change the model's answers?

It is not meant to. The stored keys and values are the ones the model computes for those exact tokens, so generation continues as if the whole prompt had been processed. What changes is the time to the first token and how the reused input tokens are billed.

Which models support prefix reuse?

On the hosted APIs, current models from OpenAI, Anthropic and Google all support prompt caching, with minimum prompt lengths that differ by model; check the provider's documentation for the model you use. When you serve open-weight models yourself, inference servers such as vLLM and SGLang provide prefix caching.

How do I tell whether a request hit?

Read the usage block of the response. OpenAI reports cached_tokens under input_tokens_details; Anthropic reports cache_read_input_tokens for tokens read and cache_creation_input_tokens for tokens written; Gemini reports a cached token count. A value above zero on the read side means part of the prompt was reused.

Is prefix reuse shared between customers?

Not across accounts on the hosted APIs: OpenAI and Anthropic both say caches are not shared between organizations, and Anthropic also isolates them by workspace on its own API. Within your own account, a prefix written for one of your users can serve another if the tokens match exactly, which is what you want for shared instructions. When self-hosting, vLLM accepts a per-request cache salt to keep tenants apart.

Building something like this?