Sub topic 4
Explicit Breakpoints and Automatic Caching
This chapter assumes you can call an LLM API, know what tokens and context windows are, and can read short Python snippets. It also assumes you know the previous idea: a cache hit needs an identical prompt prefix.
Providers differ in how you ask for caching. Some make you mark where the reusable part ends. Others detect repeated prefixes on their own. The prefix rule is the same in both cases. What changes is how much control you have and how much you must do.
Two models of caching
With explicit breakpoints, you place a marker in the request. The marker says "everything up to here is a prefix worth saving." Anthropic's Messages API works this way through the cache_control field.
With automatic caching, you send ordinary requests. The provider notices that the start of your prompt matches a recent one and reuses the stored work. You add no markers. OpenAI's API works this way for prompts that are long enough.
| Aspect | Explicit breakpoints | Automatic caching |
|---|---|---|
| What you do | Add a marker to the request | Nothing special |
| Who picks the cached span | You | The provider |
| Control over what is cached | High | Low |
| Main risk | Marker in the wrong place | Unstable prefix you never notice |
| Where to check | Usage fields | Usage fields |
Check your provider's current documentation before relying on details such as minimum lengths, lifetimes, and prices. They change.
Explicit breakpoints in practice
A breakpoint is a marker on a content block. The provider treats everything from the start of the prompt through that block as one cacheable prefix. In the Anthropic API the processing order is tools, then system, then messages, so a breakpoint on the last system block covers the tools and the system prompt.
import anthropic
client = anthropic.Anthropic()
POLICY = open("policy.md").read() # must meet the model's minimum cacheable length
response = client.messages.create(
model="YOUR_MODEL_NAME",
max_tokens=512,
system=[
{
"type": "text",
"text": POLICY,
"cache_control": {"type": "ephemeral"},
}
],
messages=[{"role": "user", "content": "Summarize the refund rules."}],
)
u = response.usage
print("written:", u.cache_creation_input_tokens)
print("read:", u.cache_read_input_tokens)
print("uncached input:", u.input_tokens)The first call writes the prefix to the cache, so cache_creation_input_tokens is large. A later call with the same prefix reads it, so cache_read_input_tokens is large instead. input_tokens counts only the tokens after the last breakpoint, which here is the user question.
Put the breakpoint on the last block that stays the same between requests. If you put it on a block that changes every call, you pay to write a prefix that nobody reads.
Automatic caching in practice
With automatic caching you have no marker to place. Your job is to make the start of every prompt identical. The provider checks incoming prompts against recent ones and reuses the longest matching prefix it has stored, usually in fixed-size steps.
Because nothing in the request shows what you intended to cache, the usage data is your only evidence. Look for a field that reports cached input tokens. If it stays at zero on repeated, long prompts, the prefix is changing somewhere.
Choosing between them
You rarely choose the model, because the provider you use decides it. What you choose is how to work with it.
- With explicit breakpoints, set them deliberately. Mark the end of the stable content, and check that the write and read counters behave as expected.
- With automatic caching, treat prompt layout as your only lever. Keep stable text first and per-request values last.
- In both cases, log the cache fields on every request, so a silent miss shows up in your data.
Exercise
You use an API with explicit breakpoints. You put the breakpoint on a block that contains the current timestamp, and every request shows a large cache write and a read of zero. What is wrong, and how do you fix it?
Show answer
The marked block changes on every call, so the prefix never matches a stored entry and each request writes a new one. Move the timestamp after the breakpoint, put the breakpoint on the last unchanging block, and confirm that the second call shows a large cache read.