The shop's assistant sends its whole support handbook as the system prompt on every call. The questions are a few words long, but each call has about 1,175 input tokens, and the model reads the handbook from the first word every time. Prompt caching lets the API keep the start of a prompt it has already read, so the calls that follow reuse it.
How it works: you mark the block where the fixed part ends with a cache_control key. The first call writes that part to the cache; calls in the next few minutes read it from there. reply.usage then counts tokens in three places: input_tokens (not cached), cache_creation_input_tokens (written to the cache) and cache_read_input_tokens (read from the cache).
Cached tokens are not free. They have their own prices. With this lesson's example prices per million tokens, a prompt part of 2,000 tokens costs:
normal input 2000 * 1.00 / 1000000 = 0.002 cache write 2000 * 1.25 / 1000000 = 0.0025 cache read 2000 * 0.10 / 1000000 = 0.0002
So the first call pays a little more, and every call after it pays a tenth for the same text.
When your program is done, it prints this:
Do you ship to Canada? -> Yes, we ship to Canada. Shipping costs a flat $15 and takes 7 to 10 business days. Express delivery isn't available. input tokens: 6 cache write: 1169 cache read: 0 cost: 0.001612 Can I return a mug I have used? -> No, sorry. An item can be returned within 14 days of delivery only if it is unused and in its original packaging. input tokens: 8 cache write: 0 cache read: 1169 cost: 0.000265 How long is the warranty on a lamp? -> Lamps come with a 2-year warranty on the electrical parts: the switch, the cable and the LED. input tokens: 9 cache write: 0 cache read: 1169 cost: 0.000241 Total cost: 0.002118
Your task: the program already sends the handbook with every question and prints the input tokens and the cost of each call. Turn caching on for the handbook block, print the tokens written to the cache and the tokens read from it, and add both to the cost with CACHE_WRITE_PRICE and CACHE_READ_PRICE. The total for the three questions must come out as 0.002118, about half of the 0.00393 the starting program prints.
Assignment
Follow the Coding Tutorial to cache the handbook, then count and price the cached tokens.
Hint
The marker goes inside the handbook's block as a third key: "cache_control" with the value {"type": "ephemeral"}. The two new cost lines look like the one for the input tokens; the division by a million comes once, for the whole sum.
Free plan: 30 tutor messages. PRO is unlimited.
