Sub topic 7
Minimum prompt length for caching
This chapter assumes you know what tokens are, have called an LLM API, and can read short Python. It also assumes you know that a cache hit needs an identical prefix, which an earlier chapter covered.
Why a minimum exists
Providers do not cache every prompt. A cache entry costs storage and bookkeeping, and reusing a very short prefix saves almost no prefill work. So each provider sets a minimum number of prompt tokens below which caching does not apply.
Below the minimum, nothing breaks. The request succeeds and is billed at the normal input rate. The cache fields in the usage data just report zero for tokens written and tokens read. This is the main gotcha: a prompt that is too short fails silently.
What the minimum applies to
The minimum is measured in tokens, not characters or words. It counts the prefix that would be cached, not the whole request.
- With explicit breakpoints, the content up to and including the breakpoint must reach the minimum. A breakpoint placed after 600 tokens does nothing if the minimum is 1,024.
- With automatic caching, the whole prompt generally has to reach the minimum before any of it is cached.
- Some providers cache in fixed-size steps once the minimum is passed. In that case, a few extra tokens beyond a step boundary may be billed as uncached.
The exact numbers differ by provider and often by model within one provider. They also change over time. Check the provider's current caching documentation for the model you use, and do not hardcode a number from memory.
Checking whether you are above the minimum
You can compare your static prefix against the minimum before you ship. Most providers offer a token-counting endpoint or library. If you have neither, a rough estimate of about four characters per token for English text is enough to tell whether you are near the limit.
def estimate_tokens(text):
# Rough rule of thumb for English prose; use a real tokenizer when close.
return len(text) // 4
def check_cacheable(prefix_text, minimum_tokens):
est = estimate_tokens(prefix_text)
if est >= minimum_tokens:
return f"~{est} tokens: likely cacheable"
return f"~{est} tokens: below the {minimum_tokens}-token minimum, caching will not apply"
policy = "Refunds are allowed within 30 days. " * 40
print(check_cacheable(policy, 1024))
print(check_cacheable(policy * 4, 1024))The minimum_tokens value here is a placeholder. Replace it with the figure from your provider's documentation.
Spotting the problem in usage data
The reliable signal is the usage data from a real request. If you sent a prompt that should be cacheable and you see zero for both cache-write and cache-read tokens on repeated calls, suspect the minimum first.
If the first call shows tokens written and the second shows zero read, the minimum is not the cause. The prefix is long enough, so look for a change in the prefix instead. A later chapter covers that diagnosis.
What to do when your prefix is too short
You have three honest options.
- Accept it. If the prefix is short, the prefill cost is already small, so there is little to save.
- Move more stable content into the prefix. Examples, tool definitions, and reference text that you already send every time can all count toward the minimum, as long as they sit before the variable content.
- Do not pad the prompt with filler to reach the minimum. Padding adds tokens you pay for at the write price, and it can lower answer quality. Only add content the model needs.
Exercise
Your service sends a 700-token system prompt followed by a short question. The model's minimum cacheable length is 1,024 tokens. You place a breakpoint at the end of the system prompt and send the same request twice. What do the cache fields most likely show, and what is one legitimate way to fix it?
Show answer
Both calls will most likely report zero tokens written and zero tokens read, with no error, because the cached prefix is below the minimum. A legitimate fix is to move other stable content that you already send, such as tool definitions or fixed examples, ahead of the variable content so the prefix reaches 1,024 tokens, then put the breakpoint after it. Padding with meaningless text is not a good fix.