Evaluate a cache policy without claiming provider internals
Run a mocked cache-eligibility rule on a 1,500-token message and separate the policy decision from the placeholder saving.
Provider prompt caching is an API-level optimisation with a contract: a minimum cacheable prefix, a write cost, a read cost, and an expiry. The contract is documented by the provider, but the internal cache implementation is not. A provider-neutral experiment can model the contract and its break-even; it cannot claim what the provider’s servers did.
This example runs a mock eligibility rule. A message with 1,500 tokens and a
cache_control block is eligible because the total is at least 1,024 tokens. The
result reports 1,500 cache-creation tokens, zero cache-read tokens and an
estimated_savings of 0.9. That 0.9 is a placeholder in the source, not a
measured discount. The page uses the example to teach the policy boundary, and
explicitly refuses to turn the placeholder into a provider claim.
Run it
From the canonical repository at commit
f958967d70992d7b09d9c75000bd8c543f1644d4:
python examples/ch32/cache_mock.py --jsonPython 3.11 or later, standard library only. No network, API key, hosted provider, model or GPU. It finishes immediately.
Implementation
evaluate_cache_control sums the tokens fields, compares the total with the
minimum, and returns creation/read counts. The savings value is hard-coded and
labelled as a placeholder in the source comment.
def evaluate_cache_control(messages: List[dict], min_tokens: int = 1024) -> CacheResult:
# Mock based on documented fields: cache_control on content blocks.
total_tokens = sum(m.get("tokens", 0) for m in messages)
eligible = total_tokens >= min_tokens
if eligible:
cache_creation = total_tokens
cache_read = 0
savings = 0.9 # documented discount placeholder, not a measured claim
else:
cache_creation = 0
cache_read = 0
savings = 0.0
return CacheResult(
eligible=eligible,
cache_creation_tokens=cache_creation,
cache_read_tokens=cache_read,
estimated_savings=savings,
metadata={"min_tokens": min_tokens, "total_tokens": total_tokens},
)Expected outcome
Observed with the command above (exit 0):
{
"eligible": true,
"cache_creation_tokens": 1500,
"cache_read_tokens": 0,
"estimated_savings": 0.9,
"metadata": {
"min_tokens": 1024,
"total_tokens": 1500
}
}The policy admitted the message and recorded a cache write, not a read. The
savings field is the source’s placeholder, not a bill. A real break-even needs
the provider’s dated write and read rates, the expected reuse count, the cache
lifetime and the workload’s prefix-sharing pattern; the chapter’s cache_lab.py
computes that from pinned inputs, and the provider’s own documentation remains
the authority for the contract.
Mechanism and limitations
Documented: the mock sums message token counts, admits at or above the minimum, and returns creation/read counts with a fixed savings placeholder. The tests check eligibility, ineligibility, the demo, JSON serialisation and a custom minimum.
Observed: the command exited 0 with eligible: true, 1,500 creation tokens,
zero read tokens and the placeholder saving above. The targeted
tests.test_ch32_cache is part of the canonical suite; no independent test count
is claimed here.
Not shown: Anthropic’s cache behaviour, cache hit rate, billed cost, latency, expiry, prefix matching, or any hosted API measurement. The mock does not send a request, does not know a real tokeniser and does not model the provider’s internal block structure. The 0.9 value must never be quoted as an observed saving.
Challenge this result
Change the message to 1,000 tokens and rerun: eligibility should become false and both token counts zero. Then add a second message and decide whether the mock’s simple sum is the right prefix model; explain why only a shared prefix, not a shared total, can be cached. Finally, replace the placeholder with a break-even formula using explicit write and read rates, and state every assumption the formula needs.
Source and verification
Canonical source:
examples/ch32/cache_mock.py
at commit f958967d70992d7b09d9c75000bd8c543f1644d4. Tests:
tests/test_ch32_cache.py
.
Chapter: Claude, Caching and API-Level Optimization
.
The run above is a fresh local execution on Windows 11 with Python 3.13.15; no
provider API was called and no provider measurement is claimed.
Understand this example
Copy this prompt into your AI tool. No code runs here.
Apply this example
Copy this prompt into your AI tool. No code runs here.