Sub topic 7
Minimum Prompt Length for Caching
This chapter assumes you can call an LLM API, know what a token and a context window are, and can read short Python snippets. It also assumes you have read the earlier chapters on prefix matching and on explicit breakpoints versus automatic caching.
What the minimum length rule says
Providers do not cache every prompt. Each one sets a minimum cacheable prompt length, counted in tokens. If the part of your prompt that you want cached is shorter than that minimum, the provider skips caching for it. The request still succeeds. You just pay the normal input price, and the usage data shows no cache activity.
This rule is easy to miss because nothing fails. A small prompt with a cache_control marker returns a normal answer. The only sign is that the cache counters stay at zero.
Why a minimum exists
Caching has overhead. The provider must store the attention key/value state for the prefix, find it again on later requests, and keep it for the cache lifetime. For a short prefix, the saved prefill work is small, so the storage and lookup cost more than the reuse is worth. A minimum length keeps caching limited to prefixes where the saving is large enough to matter.
The consequence for you: the benefit of caching grows with prefix length, and below a threshold it is zero.
What the minimum applies to
Two details trip people up.
- The minimum applies to the cached prefix, not the whole prompt. A prompt with a 200-token stable prefix and a 5,000-token variable tail does not qualify on the strength of the tail. Only the stable leading part can be reused, so only its length counts.
- The count is in tokens, not characters or words. The same text can produce different token counts under different models, because tokenizers differ. A document that clears the threshold on one model can fall short on another.
With explicit breakpoints, the length that matters is the length of everything from the start of the prompt up to the breakpoint. That includes tools, the system prompt, and any messages before the marker.
How the rule varies by provider and model
Providers publish their thresholds, and the numbers differ by provider and often by model within one provider. Values in the low thousands of tokens are common, and some model tiers use a lower or higher figure than others. Providers also change these numbers over time as they release models.
Do not hard-code a threshold from memory or from a tutorial. Look up the current figure in the provider's documentation for the exact model you call. Then treat that figure as a configuration value in your code.
Some providers also cache in blocks. They match the prefix in fixed-size increments beyond the minimum, so a prompt just over the threshold may get only part of its prefix cached. Check the documentation for whether your provider does this, because it changes how much of a borderline prompt you can reuse.
Seeing the rule in the usage data
The cleanest way to confirm you are below the minimum is to read the usage counters from the earlier chapter. This sketch sends the same short prompt twice with a breakpoint set. It prints the counters for each call.
import anthropic
client = anthropic.Anthropic()
SHORT_POLICY = "You are a support assistant. Be polite and concise."
def ask(question):
response = client.messages.create(
model="YOUR_MODEL_NAME",
max_tokens=256,
system=[
{
"type": "text",
"text": SHORT_POLICY,
"cache_control": {"type": "ephemeral"},
}
],
messages=[{"role": "user", "content": question}],
)
u = response.usage
return u.cache_creation_input_tokens, u.cache_read_input_tokens, u.input_tokens
for q in ["What is your refund window?", "How do I reset my password?"]:
written, read, uncached = ask(q)
print(f"written={written} read={read} uncached={uncached}")Because SHORT_POLICY is only a dozen or so tokens, both calls should report written=0 and read=0, with everything under uncached. If you replace the policy with a document that exceeds the model's minimum, the first call should show a non-zero written value and the second a non-zero read value. Replace YOUR_MODEL_NAME with a real model name before running.
Checking length before you send
You can avoid guessing by checking the prefix length in your own code. Most providers offer a token-counting endpoint or a local tokenizer. The function below wraps whichever counter you use and warns when a prefix falls short.
def check_cacheable(prefix_text, count_tokens, minimum_tokens):
"""Return (ok, tokens). count_tokens is any function str -> int."""
tokens = count_tokens(prefix_text)
return tokens >= minimum_tokens, tokens
def fake_count(text):
# Rough stand-in for demonstration: about 4 characters per token.
return len(text) // 4
MINIMUM = 1024 # replace with the published value for your model
short_prefix = "You are a support assistant. Follow the policy."
long_prefix = "Refund policy clause. " * 300
for name, text in [("short", short_prefix), ("long", long_prefix)]:
ok, n = check_cacheable(text, fake_count, MINIMUM)
print(name, n, "cacheable" if ok else "below minimum")The 4-characters-per-token ratio is only an approximation for English text. For a real decision, use the provider's own counter, since your threshold is defined by their tokenizer.
The decision flow
Common situations and fixes
| Situation | Symptom | Fix |
|---|---|---|
| Short system prompt, breakpoint set | `written` and `read` always 0 | Move more stable content before the breakpoint, or accept no caching |
| Tools plus system prompt together just under the minimum | Intermittent or absent caching | Add stable reference material, or check whether the total clears the threshold |
| Switched to another model | Caching stops working with no code change | Look up the new model's minimum |
| Long prompt, but breakpoint placed too early | Cache counters show only a small cached part, or none | Place the breakpoint after the last stable block |
| Variable text placed first | No reuse even with a long prompt | Reorder so stable content leads, as in the layout chapter |
Padding to reach the minimum
A tempting trick is to add filler text so a short prefix passes the minimum. This is usually a bad idea. You would pay to write the padded prefix to the cache, and you would pay the read price on the padding for every later call. The extra tokens also add no information for the model.
Padding only makes sense if the content you add is useful, such as worked examples, a style guide, or reference tables that improve answers. In that case you are not padding. You are choosing to include more stable context, and caching makes that context cheaper to carry. Judge it by whether the added content improves output quality, not by whether it crosses the threshold.
Interaction with the other rules
The minimum length is one of several conditions that must all hold for a hit.
- The prefix must match exactly, as covered earlier. A long prefix with an early edit still misses.
- The entry must still be alive. The lifetime rules decide whether a previous write is still available.
- The next chapters on pricing and measurement explain whether the savings from reuse justify the write cost.
A useful debugging order when the counters stay at zero is: first check length against the minimum, then check that the prefix is identical between calls, then check timing against the lifetime. Length is the cheapest to check and the easiest to fix, so start there.
Edge cases
- Several breakpoints. Where a provider allows more than one, each breakpoint needs its own prefix to meet the minimum. A small block early in the prompt may be too short to cache by itself, even though the total prompt is long.
- Growing conversations. A multi-turn chat may start below the minimum and begin caching only once the history grows past it. Early turns show zero counters. Later turns show reads. This is expected.
- Tool definitions. Tool schemas count toward the prefix, since they sit at the start of the prompt. A large tool list can by itself bring a short system prompt over the threshold.
- Automatic caching. With providers that cache without markers, the same minimum applies. You have no marker to place, but short prompts still produce no cache activity.
- Documentation drift. Thresholds change as models are released. A prompt that cached last quarter may fall below a newly raised minimum on a new model version.
Exercise
A request has a 600-token system prompt with a breakpoint on it, followed by a 4,000-token user document that differs on every call. The model's minimum is 1,024 tokens. Will the system prompt be cached, and what would you do to get caching?
Show answer
No. The minimum applies to the prefix up to the breakpoint, which is only 600 tokens, so the usage counters will show no write or read. The 4,000-token document differs every call, so it cannot be part of a reusable prefix. To get caching, move additional stable content (such as policy text, examples, or tool definitions) before the breakpoint until the prefix reaches at least 1,024 tokens, but only if that content helps the model. Otherwise accept that this request does not benefit from caching.
Exercise
Your app cached well on one model. After you switched to a different model with no other changes, cache_creation_input_tokens and cache_read_input_tokens both read 0. Give the first thing you check and how you check it.
Show answer
Check whether the stable prefix is now below the new model's minimum. Look up the published minimum for the new model, count the prefix tokens with the provider's token counter, and compare. Tokenizers and thresholds differ between models, so the same text can fall under the limit.