This article was created with the help of AI. The numbers come from my own usage log.
In October Anthropic emailed me that my prompt cache hit rate was low and that caching could save up to 36% of my API spend. Over the first three and a half weeks with caching, the bill came out 16% smaller. With the cache marker alone I would have paid more than before.
The reason is one sentence in Anthropic's docs: "a cache entry only becomes available after the first response starts." The request that writes the start of a prompt to the cache pays a quarter more for that part. Requests after it read the same part for a fraction of the price, and the entry lives five minutes. But requests that leave together all find the cache empty. Each one pays for the write, and none of them gets to read.
How deep the read discount goes depends on the model. On Opus 5.5, which I use, and on Sonnet 5.5 a read costs a twentieth of normal input. On Fable 5.1 and 5.2 it is a fortieth, on the other models a tenth. My news site listed the ratios per model in its write-up of the Opus 5.5 price cut.
My requests leave together. The project is a news pipeline. It polls its sources every two hours, and Claude Opus rewrites every picked story into a post, in Russian and in English, with the same long prompt in front each time. Rewriting was more than two thirds of the bill. In the log for the two days before the email, three calls out of four had started within 20 seconds of the one before. The rest came later, mostly after more than an hour. Not one call started in the window where a finished entry would have been waiting, between 20 seconds and five minutes after the one before.
So the marker went in together with a gate. The system prompt is split in two. The part that every post of a news section shares gets cache_control, and the part that changes with the story follows it. Trimmed, the request looks like this:
const system = [ { type: "text", text: cached, cache_control: { type: "ephemeral" } }, { type: "text", text: rest }, ]; The gate lets the first call with a given prefix go alone. The others wait until it starts answering, 30 seconds at most, and then find the entry ready. This is the function, unchanged:
import { createHash } from "node:crypto"; import { setTimeout as sleep } from "node:timers/promises"; // A cache entry is readable only once the call that writes it has started // answering, so calls sharing a prefix that start together would each pay the // write premium. The first one goes alone; the rest wait for its first content // block. Keyed by model and prefix hash; per process. const warmCaches = new Map<string, { ready: Promise<void>; usedAt: number }>(); // The API keeps an entry 5 minutes after its last use. const CACHE_TTL_MS = 4 * 60_000; const FOLLOWER_WAIT_MS = 30_000; /** Waits for a call already writing this prefix; returns a release callback when this call is the writer. */ async function takeCacheTurn(model: string, prefix: string): Promise<(() => void) | undefined> { const key = createHash("sha256").update(`${model}\0${prefix}`).digest("hex"); const entry = warmCaches.get(key); if (entry && Date.now() - entry.usedAt < CACHE_TTL_MS) { entry.usedAt = Date.now(); await Promise.race([entry.ready, sleep(FOLLOWER_WAIT_MS, undefined, { ref: false })]); return undefined; } let release!: () => void; const ready = new Promise<void>((resolve) => (release = resolve)); warmCaches.set(key, { ready, usedAt: Date.now() }); return release; } The writer releases the others when its stream opens the first content block, and once more in finally, so a call that fails cannot hold the queue. The map counts an entry as gone after four minutes, a minute before the API drops it. The call site, trimmed to the gate:
const releaseCache = cached ? await takeCacheTurn(args.model, cached) : undefined; const stream = client.messages.stream(params); stream.on("streamEvent", (event) => { if (event.type === "content_block_start") releaseCache?.(); }); try { message = await stream.finalMessage(); } finally { releaseCache?.(); } In a test burst of twelve calls, two paid for a write and ten read.
The wait is the price of the gate. My posts go out on a schedule, so half a minute changes nothing for them. In front of a user who is waiting for an answer it would be a visible pause. The map also lives in one process, so two workers would each send a writer of their own.
Over those three and a half weeks three rewrite calls out of four read the prefix from the cache, and by the week of November 9 it was four out of five. A call that read paid 0.48 of the list price for its input. A call that wrote paid 1.14. Going by the two days of log above, with the marker alone every call would have paid that 1.14. Nothing got twenty times cheaper, because the shared prefix is a little over half of the prompt and the rest is the story's own text.
Across all rewrite calls the input got 37% cheaper. That is almost the number from the email, but the email was about the whole bill. Output tokens get no discount, so a rewrite call as a whole got 28% cheaper. And rewriting shares the API with triage, post checks and long articles. On the bill it came to 16%.
Triage got nothing from caching. Its first step is a single call every two hours, and by the next run the entry is gone. The one-hour cache would not last the two hours either, and its write costs double the normal input. Since early November triage goes through Message Batches at half price, with the answer arriving later.
Before adding cache_control to a pipeline, sort a day of your requests by start time and look at the gaps. Calls that start before the previous one has begun to answer will all write. Gaps under five minutes work the way the docs promise. Between five minutes and an hour the one-hour cache is worth a look, and past an hour nothing helps. Once caching is on, cache_creation_input_tokens and cache_read_input_tokens in every response's usage show which of the two that call paid for.
The read price for each model is in that write-up of the Opus 5.5 price cut on Promtime, the news site this pipeline writes for.
