Prompt caching correctly — the variable input goes LAST — step 9 of 9
Checkpoint
One last thing before we move on. Same surface as a write step — but the lesson doesn't complete until this passes.
Final drill. Build a cache strategy decision helper. Write
recommend_strategy(stable_tokens, variable_tokens, calls, session_minutes, input_rate) that:
- Computes uncached cost, 5m-cached cost, and 1h-cached cost.
- 5m-cached writes 1.25× input rate; 1h-cached writes 2× input rate.
- Both read at 0.1× input rate.
- 5m cache: if
session_minutes > 5, the cache expires mid-session — the second half of calls re-write the cache. Approximation: half the calls write, half read. - 1h cache: write once, read for all subsequent calls.
- Returns a dict
{"uncached": <cost>, "cached_5m": <cost>, "cached_1h": <cost>, "winner": <strategy with lowest cost>}. - All costs rounded to 2 places.
Two scenarios run for you. Expected output:
short session (1 min): cached_5m=0.33 cached_1h=0.35 winner=cached_5m
long session (30 min): cached_5m=0.36 cached_1h=0.35 winner=cached_1h
⌘↵ runs the editor.read, then continue.
Checkpoint
One last thing before we move on. Same surface as a write step — but the lesson doesn't complete until this passes.
Final drill. Build a cache strategy decision helper. Write
recommend_strategy(stable_tokens, variable_tokens, calls, session_minutes, input_rate) that:
- Computes uncached cost, 5m-cached cost, and 1h-cached cost.
- 5m-cached writes 1.25× input rate; 1h-cached writes 2× input rate.
- Both read at 0.1× input rate.
- 5m cache: if
session_minutes > 5, the cache expires mid-session — the second half of calls re-write the cache. Approximation: half the calls write, half read. - 1h cache: write once, read for all subsequent calls.
- Returns a dict
{"uncached": <cost>, "cached_5m": <cost>, "cached_1h": <cost>, "winner": <strategy with lowest cost>}. - All costs rounded to 2 places.
Two scenarios run for you. Expected output:
short session (1 min): cached_5m=0.33 cached_1h=0.35 winner=cached_5m
long session (30 min): cached_5m=0.36 cached_1h=0.35 winner=cached_1h
this step needs the editor
on desktop today; in the app (coming soon). save your spot and we'll bring you back here when you're ready.