Sub topic 12
Parallel requests and warming the cache
This chapter assumes you know how exact prefix matching works, what a cache lifetime (TTL) is, and how to read the usage fields that report cache writes and reads. It also assumes you can read short Python snippets.
Why parallel requests miss
A provider can only reuse a prefix that it has already processed and stored. The stored entry usually becomes available only after the first request has started producing its response. If you send 20 requests with the same long prefix at the same moment, none of them finds an entry. Each one processes the full prefix, and each one may pay the cache write price.
The result is a burst of misses, even though the prompts are identical and the layout is correct.
Warming the cache
Cache warming means sending one request first so the prefix is stored, then sending the rest. The warm-up request can be a real piece of work, such as the first item in a batch. It can also be a cheap call with the same prefix and a very small max_tokens.
The example below warms the cache with one call and then fans out with a thread pool. It uses the same explicit breakpoint as the earlier chapter.
import anthropic
from concurrent.futures import ThreadPoolExecutor
client = anthropic.Anthropic()
POLICY = open("policy.md").read() # must meet the minimum cacheable length
def ask(question, max_tokens=512):
response = client.messages.create(
model="YOUR_MODEL_NAME",
max_tokens=max_tokens,
system=[
{
"type": "text",
"text": POLICY,
"cache_control": {"type": "ephemeral"},
}
],
messages=[{"role": "user", "content": question}],
)
u = response.usage
return question, u.cache_creation_input_tokens, u.cache_read_input_tokens
questions = [f"Summarize refund rule {i}." for i in range(1, 9)]
# 1. Warm: one request writes the prefix. Output is kept tiny.
print(ask("Reply with OK.", max_tokens=5))
# 2. Fan out: the remaining requests can now read the stored prefix.
with ThreadPoolExecutor(max_workers=8) as pool:
for question, written, read in pool.map(ask, questions):
print(question, "written:", written, "read:", read)If warming works, the first line shows tokens written and the fan-out lines show tokens read with little or nothing written.
Keeping the warm-up valid
The warm-up request must share the exact prefix of the real requests. That includes the tools, the system prompt, and any breakpoint markers, in the same order. If the warm-up differs in even one early token, it warms a different entry and the fan-out still misses.
Two practical points:
- The entry expires according to its TTL. Warm shortly before the burst, not hours earlier.
- Each cache read usually refreshes the TTL on that entry. A steady trickle of requests keeps the entry alive, but a long gap lets it expire. Check your provider's rules for the exact behavior.
Checking that it worked
Use the hit ratio from the measuring chapter. Compute it per burst, not only as a daily average. A healthy burst shows a low ratio on the warm-up request and a high ratio on everything after it. If every request in the burst shows writes, the requests are probably racing the warm-up or differ in their prefix.
Trade-offs
Warming adds one request of latency before the burst. For a fan-out of two or three short requests, that delay and the extra call can cost more than the misses it prevents. Warming pays off when the prefix is long, the fan-out is wide, or the same prefix will be reused many times after the warm-up.
Some providers cache automatically and do not let you control the write step. Warming still helps there. Send one request, wait for it to finish, then send the rest.
Exercise
You send 30 parallel requests with an identical 6,000-token prefix to a cold cache. The usage data shows cache writes on nearly every response and almost no reads. What two changes would you try, and how would you confirm the fix?
Show answer
First, send one warm-up request and wait for its response before starting the other 29, so the entry exists when they arrive. Second, confirm that the warm-up uses exactly the same prefix (tools, system prompt, breakpoints) as the real requests. Confirm the fix by logging cache read and write tokens per request. The warm-up should show a write, and the 29 remaining requests should show mostly reads, giving a hit ratio near 1.0 for them.