- characters in the shared system prompt
- 14,783
- tokens written to cache on turn 1
- 5,454
- tokens read from cache on turn 2
- 5,454
- uncached input tokens on turn 2
- 110
What we changed
On October 20 we moved Ask Infinary, the chat on this site, from Gemini to Claude Haiku 5.5. It runs in our Cloudflare Worker through Anthropic’s SDK.
Every request carries the same long system prompt: what Infinary is, how the platform works, and what the chat must never claim. That prompt is 14,783 characters. A chat that re-sends it at full price on every turn pays for the same text over and over, so the move came with prompt caching from the start.
How we laid out the request
Caching only pays when the start of the request is byte-for-byte the same as last time. We split each request into three parts, and used two of the four cache breakpoints Claude allows:
-
The system prompt, as one block that is identical on every page and every request. Nothing in it changes: no page path, no date, no ID. It carries the first breakpoint.
system: [{ type: 'text', text: KNOWLEDGE_PROMPT, cache_control: { type: 'ephemeral' } }], -
The conversation, with a rolling breakpoint on the latest user turn. Every turn is sent as a text-block array, so a turn renders the same bytes whether or not it carries the marker.
-
The page the visitor is on, as a trailing system message after the last breakpoint. It isn’t kept in the history, so moving from page to page never invalidates the cached conversation.
The detail that would have quietly defeated it
The old Gemini path sent the last 16 messages of the conversation: messages.slice(-16). Had we kept that, every request past the 16th message would have started one message later than the one before. The prefix would have changed on every turn, and nothing past the system prompt could ever have been read from cache.
The new window keeps at most 20 messages and starts on a multiple of 10:
if (turns.length > HISTORY_WINDOW) { start = Math.ceil((turns.length - HISTORY_WINDOW) / HISTORY_STEP) * HISTORY_STEP;}While the start stays put, each request is the previous one plus the new turns, which is exactly what a rolling breakpoint needs. When a long conversation outgrows the window, the start jumps by 10 and the cache is written once more. Older messages are cut at 8,000 characters, always the same way, for the same reason.
What we measured
After the deploy, the Worker’s log line for each reply records the cache counts. In the first chat we checked:
- Turn 1 wrote 5,454 tokens to the cache.
- Turn 2 of the same chat read those 5,454 tokens back, with 110 uncached input tokens.
So the second message paid full price for 110 tokens and the cache rate for everything else.
Two honest caveats. Before the deploy we had estimated the system prompt at roughly 3,700 tokens, from its length at about four characters a token; we never counted it. The 5,454 also includes the first user turn, whose length we didn’t record, so the two numbers can’t be compared directly. And we didn’t measure latency, so we aren’t claiming the cache made replies faster.
Also in the same change
The chat is a public box that calls a paid model, so the move came with limits too, on the size of what a visitor sends and on the length of a reply, and anything the browser reports is checked before it goes near the prompt. The per-address rate limit that shipped that evening didn’t work, which is its own note.
What we’d tell someone else
Then look at how you trim history. A window that slides one turn at a time re-keys the cache on every request, and it does so silently: replies still work, they just cost more than they should. The only way to see it is to log the cache counts, which is why that log line shipped with the change.