Prefix reuse: stable text first or question first
A fake provider that remembers the last prompt. Prices, the minimum size and the expiry time are example numbers: real providers differ and change them.
Many providers remember the work they did on the start of your prompt and reuse it when a later request begins with the same text, which makes repeated long prompts cheaper and faster to start. You benefit by putting the stable text first and the changing text last.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write common_prefix_length(a, b) for two lists and show it for [1, 2, 3, 4] with [1, 2, 9, 4], and for [5, 6] with [1, 6].
Write cost(total_tokens, cached_tokens, price_fresh, price_cached) returning the cost in micro-dollars (prices per million tokens, so tokens times price). Print it for 305 total, 300 cached, prices 2 and 1.
Explain it without notes
What does a prompt cache save, and how does it differ from an exact-match cache?
Why does the order of parts in a prompt matter for prompt caching?
How would you check that prompt caching is actually working in your product?
Expected output
stable text first:
t=0: 305 tokens, cached 0, cost 610 micro-dollars
t=10000: 305 tokens, cached 300, cost 310 micro-dollars
t=20000: 305 tokens, cached 300, cost 310 micro-dollars
t=400000: 305 tokens, cached 0, cost 610 micro-dollars
total 1840 micro-dollars
question first:
t=0: 305 tokens, cached 0, cost 610 micro-dollars
t=10000: 305 tokens, cached 0, cost 610 micro-dollars
t=20000: 305 tokens, cached 0, cost 610 micro-dollars
t=400000: 305 tokens, cached 0, cost 610 micro-dollars
total 2440 micro-dollars