Back to blog
ai-engineering

Prompt caching: one changing line cut cache hits to 7%

A status line that changed every turn kept a coding assistant from reusing its prompt cache. Moving it raised reuse from 7.5% to 98.3%. How to check yours.

HA

November 12, 2026

4 min read

Prompt caching: one changing line cut cache hits to 7%

On a model server we operate, a coding assistant's requests took about a minute each although the server had prompt caching switched on. A few lines near the top of every prompt changed on each turn, so almost nothing could be reused. Moving those lines to the end raised cache reuse from 7.5% to 98.3% of prompt tokens and cut the mean time to first token from 11.2 seconds to 1.4. Here is how to check for the same thing on your own server.

How a prefix cache decides what to reuse

Coding assistants send the whole conversation again on every turn: system prompt, tool descriptions, and all earlier messages. A model server with prefix caching, such as vLLM, keeps the work it did for the previous request and reuses it for as long as the new prompt is identical from the first token onward. The first token that differs ends the reuse. Everything after it is computed again, however similar it looks.

An interactive illustration. A prefix cache reuses the part of a prompt before the first changed token. If the change is 3% into a 60,000-token prompt, 97% is computed again.

Arithmetic: how much of a prompt a prefix cache can reuse

So the position of a change matters far more than its size. One changed word at the end of a 60,000-token prompt costs almost nothing. One changed word near the start costs the whole prompt.

What we found in the prompts

The client put a short status block into its first message: a running token count and a continuation counter. Both numbers changed on every turn. They sat near the start of a long prompt, so each request missed the cache almost completely and the server read the full conversation again.

Nothing was broken on either side. The client was reporting useful information, and the cache was doing what a prefix cache does. The two simply did not fit together.

The change

We changed the gateway in front of the model, not the client and not the model:

  • In the first message, the changing status lines are replaced by a fixed sentence, so the start of the prompt is identical from turn to turn.
  • The real status lines are appended to the last user message, so the model still sees the same information, at the end of the prompt where a change is cheap.
  • Requests that do not contain this block pass through untouched.
Before (4 minutes)After (5 minutes)
Prompt tokens served from cache7.5% (51k of 687k)98.3% (6.70M of 6.81M)
Mean time to first token11.2 s1.4 s
Median request time, coding client61 s3 s
Before and after moving the changing lines

In the first six and a half minutes after the change the rewrite applied to 43 requests from the coding client and one of our own test requests. The 34 requests from other clients in that time passed through unchanged, and a replay of the previous 24 hours of other clients' requests changed none of them.

How to check your own server

  1. Look at the cache counters. vLLM exports prefix-cache queries and hits in its metrics. A long-conversation workload with a hit rate in single digits is the signal.
  2. Compare two consecutive requests of one conversation and find the first position where they differ. If it is near the start, look at what sits there: timestamps, counters, usage figures, random identifiers.
  3. Move it, do not delete it. Keep the start of the prompt fixed and put anything that changes per turn at the end.

Setup and limits

A 27B model served with vLLM behind our own gateway, 2 October 2026. The before and after windows are 4 and 5 minutes of live traffic on the same evening, dominated by one coding client, so they are small samples and not a controlled test. The hit rate is counted in prompt tokens from vLLM's own metrics; request times come from the gateway's access log. The rewrite has a switch that turns it off without a restart.

Sources

Tags

inference-servingllm-opsperformance
HA

About the author

Hamza Abdagić

View profile