Knaph

Your First Call3 of 37

The Price of a Reply

In [Reading the Reply](reading-the-reply) you saw that every reply carries a usage record: how many tokens went in, how many were read from or written to the cache, and how many came out. Here you turn it into dollars, the way mini will keep an eye on its own spending. Write reply_cost(model, usage). It gets the model's name and the reply's usage as a plain dict, like this one (the lesson's greeting): ``json {"input_tokens": 12, "output_tokens": 80, "cache_read_input_tokens": 0, "cache_creation_input_tokens": 0} ` and returns one row for each kind of token, in the order of FIELDS: the label, the number of tokens, and what they cost in dollars. - **Each kind has its own price**, per million tokens, in the PRICES table: so 2,000 input tokens on claude-opus-5-5 cost 2,000 × 4.00 / 1,000,000 = 0.008 dollars. - **A field that is missing, or None, counts as 0.** Real replies sometimes leave a field out or set it to None when there was nothing to count, and a harness shouldn't crash on that. - **An unknown model** returns None, and the printing code says so. The input is the model's name on the first line and the usage, as JSON, on the second. The code that reads it and prints the rows is already written. For the greeting it prints: `text model: claude-opus-5-5 input: 12 tokens, 0.000048 dollars cache read: 0 tokens, 0.000000 dollars cache write: 0 tokens, 0.000000 dollars output: 80 tokens, 0.001600 dollars total: 0.001648 dollars ` Notice where the money goes: the 80 output tokens (the thinking and the greeting) cost more than thirty times as much as the 12 input tokens. <details> <summary>A hint</summary> usage.get(field) or 0 gives the number of tokens, whether the field is missing, None or a number. Then multiply by PRICES[model][price_key]` and divide by 1,000,000. </details> The prices are rounded, for late 2026. Real price lists have more to them (Haiku's prices rise for prompts over 100,000 tokens, for example), which this exercise leaves out.

Sign in to try this exercise.