From one request to a month, and what a chat history costs
Prices are example numbers. The chat part counts tokens turn by turn: the system prompt is re-sent every time.
A model call is billed by tokens: input tokens times the input price, plus output tokens times the output price. A few lines of arithmetic tell you the cost per request, per user and per month, and show why long chat histories get expensive fast.
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.
A summariser reads 3,000 input tokens and writes 200 output tokens. With example prices of 0.40 dollars per MTok input and 1.20 per MTok output, print the cost of one call and of 50,000 calls, each with 6 decimals.
Write chat_input_tokens(turns, system, per_exchange, keep_last) returning the total input tokens of a chat. A new user message adds 50 tokens to its turn. Print it for 8 turns with a 300-token system prompt, 200 tokens per exchange, once with keep_last=None and once with keep_last=2.
Explain it without notes
Write the cost formula for one model call and say which part people forget.
A chat bot costs twice what the team predicted. Name three likely causes.
Why does the total input of an untrimmed chat grow faster than the number of turns?
Why are prices kept as named variables in code?
Expected output
one request: 0.001050 dollars
one user, 20 requests a day for 30 days: 0.63 dollars
10,000 such users: 6300.00 dollars a month
10-turn chat, send all history: input 14050 tokens, output 1500 tokens, cost 0.009275 dollars
10-turn chat, keep last 3 turns: input 9640 tokens, output 1500 tokens, cost 0.007070 dollars