Sub topic 13
Routing hints and cache keys for high-volume traffic
This chapter assumes you know exact prefix matching, how to read usage fields, and how to compute a hit ratio. It covers what changes when one prompt prefix receives many requests per minute.
Why routing matters
A cache entry is stored on the servers that processed the prompt. It is not a single global store. A new request can only hit if the load balancer sends it to a machine that holds the matching prefix.
At low traffic this rarely matters. At high traffic, two things can go wrong:
- Requests with the same prefix spread across many machines. Each machine must build its own copy, so you pay for many cache writes and get fewer reads.
- One prefix is so popular that a single machine can't serve all of it. Some requests overflow to other machines that have no entry yet.
Routing hints
Some providers let you pass a routing hint. The hint is a string you choose. Requests that share the same prefix and the same hint are sent to the same machines more often. OpenAI calls this prompt_cache_key. Check your provider's documentation for the exact name and behavior.
The hint does not change what is cached or what counts as a match. The prefix must still be identical. The hint only raises the chance that matching requests reach the machine that holds the entry.
from openai import OpenAI
client = OpenAI()
POLICY = open("policy.md").read()
def ask(question, tenant_id):
response = client.chat.completions.create(
model="YOUR_MODEL_NAME",
prompt_cache_key=f"support-policy-{tenant_id}",
messages=[
{"role": "system", "content": POLICY},
{"role": "user", "content": question},
],
)
details = response.usage.prompt_tokens_details
return response.choices[0].message.content, details.cached_tokensProviders without a routing hint, such as Anthropic's Messages API as described in this tutorial, rely on your explicit breakpoints. Routing is handled for you.
Choosing a good key
The key should group requests that share a long prefix. Too coarse and too fine are both a problem.
| Key choice | Effect |
|---|---|
| One key for all traffic | Every request targets the same machines, which can overflow when volume is high. |
| A key per user, when users share no prefix | Each key warms its own entry and the entries are never reused across users. |
| A key per prompt template or per tenant | Related requests stay together, and load spreads across many keys. |
Follow these rules:
- Use the same key for requests that have the same long prefix.
- Split into several keys only when one prefix has too much traffic for one machine. Providers may document a rate, such as roughly 15 requests per minute for one prefix and key. Treat any figure as approximate and check the current docs.
- Never put a timestamp or request ID in the key. A key that changes on every call defeats its purpose.
- Don't put secrets in the key. It is a label you send with each request.
Verify with your own data
Don't assume the key helped. Log the hit ratio from the previous chapter, grouped by key. Compare it before and after you add the key, using similar traffic.
from collections import defaultdict
stats = defaultdict(lambda: [0, 0]) # key -> [cached tokens, total input tokens]
def record(key, cached, total):
stats[key][0] += cached
stats[key][1] += total
def ratio(key):
cached, total = stats[key]
return cached / total if total else 0.0
record("support-policy-acme", 3800, 4000)
record("support-policy-acme", 3900, 4050)
print(round(ratio("support-policy-acme"), 2)) # 0.96If one key shows a low ratio while its prefix is stable, it may be overloaded. Try splitting it. If many keys show low ratios, look for prefix changes first, because routing can't fix a prefix that differs between calls.
Exercise
A service sends 200 requests per minute that all share one 6,000-token prefix, all with the same cache key. The hit ratio is 55% even though the prefix is byte-identical. What is a likely cause, and what would you try?
Show answer
One key and prefix probably receives more traffic than one machine can serve, so some requests overflow to machines without the entry. Split traffic across several stable keys, for example by tenant or template. Then compare the hit ratio per key before and after the change.