Claude and the Anthropic SDK · Chapter 14 of 21
What breaks the cache, and what it saves
By the end of this chapter you can find the silent changes that make every request miss the cache (a timestamp, a key order, a tool list, a model, parallel first requests), choose between the 5-minute and 1-hour lifetimes, price every reply with the real cache rates, and tell Farhan in one number what caching saves on Sahay's claim suite. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Cost
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Claude and the Anthropic SDK are free. This one comes with the all labs plan.
Why you need this
Last week Sahay's cache worked. This week Farhan's bill is back up, and nothing is broken: every request succeeds, every answer is right. Arjun added one small, sensible thing to the system prompt, today's date, so Sahay can work out the 15-day deadline. From that request on, the cache has missed every time.
That is how caching fails in real life: silently. No error, no warning, just cache_read_input_tokens at 0 and a higher bill. Chapter 13 put the breakpoints in. This chapter is about keeping them working, and proving, in dollars, that they do.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
fix_the_timestamp.py: the timestamp that breaks the cache
Ask the two QUESTIONS (given) with system=build_system() (given, the date before the breakpoint), one after the other: model "claude-opus-5-5", max_tokens 1024. Print broken N: I in, W written, R read.
Write fixed_system(): two blocks, INTRO + "\n\n" + policy_wording() with "cache_control": {"type": "ephemeral"}, then f"Today is {now}." with no marker. Ask the two questions again with it and print fixed N: I in, W written, R read. The second fixed request must read the cache.
- Task 2
one_hour.py: five minutes or an hour?
Send QUESTION (given) four times (model "claude-opus-5-5", max_tokens 1024, system: INTRO then the policy wording with a cache breakpoint), calling lab_clock(6) (given) before every request but the first. Requests 1 and 2 use "cache_control": {"type": "ephemeral"}; 3 and 4 use {"type": "ephemeral", "ttl": "1h"}.
After each reply print request N (TTL): I in, W written, R read, $COST, with TTL 5m or 1h and COST from PRICES["claude-opus-5-5"] (5-minute writes at cache_write_5m, 1-hour writes at cache_write_1h, from usage.cache_creation), 6 decimals.
- Task 3
savings.py: what caching saves
Write cost_of(model, usage): the dollars of one reply with PRICES[model] (input, cache_creation's 5-minute and 1-hour writes at their prices, cache reads, output, over 1,000,000; None counts as 0).
Under if __name__ == "__main__":, run every claim of claims() through run_claim (given) on "claude-sonnet-5-5", all sixteen with cached=False, then all sixteen with cached=True, and print without caching: $X, with caching: $Y (6 decimals) and saved: P% (round((X - Y) / X * 100)).
- Task 4 · interview question
Interview: fan_out.py
"Four questions sent at once write the cached policy four times and read it never. Why, and what is the fix?" Inside main(), with ask() (given, the async client): forget_cache() (given), send all four QUESTIONS at once with asyncio.gather and print all at once: W writes, R reads (how many replies wrote, how many read).
Then forget_cache(), await ask(QUESTIONS[0]) alone, then the other three at once, and print first, then the rest: W writes, R reads over those four replies.
What you will learn
- The one rule: a cache hit needs the same bytes, in order, up to the breakpoint.
- The silent invalidators: timestamps, key order, tool lists, model switches, effort changes.
- Parallel first requests: why they all miss, and the fix.
- The two lifetimes: 5 minutes and 1 hour, on the lab's clock.
- The real prices of writes and reads, and what caching saves on the suite.
More in Cost
Build "What breaks the cache, and what it saves" yourself
Parchi, a Jaipur startup, helps families claim health insurance. You build Sahay, a claims assistant that reads bills and policies with Claude, and measure what caching, models and effort cost. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Claude and the Anthropic SDK are free. This one comes with the all labs plan.