Claude and the Anthropic SDK (TypeScript) · 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
- TypeScript
- In the all labs plan
The first 2 chapters of Claude and the Anthropic SDK (TypeScript) 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: 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.ts: the timestamp that breaks the cache
Ask the two QUESTIONS (given) with system: buildSystem() (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 fixedSystem(): two blocks, INTRO + '\n\n' + policyWording() with cache_control: { type: 'ephemeral' }, then 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.ts: 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 await labClock(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.ts: what caching saves
Export costOf(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; null fields count as 0).
Under if (import.meta.main), run every claim of claims() through runClaim (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% (Math.round((X - Y) / X * 100)).
- Task 4 · interview question
Interview: fan-out.ts
"Four questions sent at once write the cached policy four times and read it never. Why, and what is the fix?" With system: SYSTEM (given), model 'claude-opus-5-5', max_tokens 1024: await forgetCache() (given), send all four QUESTIONS at once and print all at once: W writes, R reads (how many replies wrote, how many read).
Then await forgetCache(), send QUESTIONS[0] alone and await it, 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 (TypeScript) are free. This one comes with the all labs plan.