Compute the Cost of Every Call
token pricing and cost calculation
Part of: Ship & Monitor an LLM App (Capstone)
Every log record already has token counts. This lesson turns those counts into money. Providers charge per token, and input and output tokens are priced differently. Output usually costs several times more than input, because generating text is more expensive than reading it. Get this wrong and your dashboard shows a number that's off by a multiple. The pricing shape Providers publish a price per million tokens, split by input and output: Output is priced roughly 5x input here. A summarizer that reads a huge document but writes three sentences is cheap. A code generator that writes a thousand lines from a short prompt is not. The ratio matters more than either number alone. Attaching cost to the log record Cost belongs on the same record as everything else, computed once, right after the call: Now every downstream aggregate reads a field instead of recomputing pricing math in five places: daily spend, per-model spend, the cost of one request. Why per-request cost, not just a monthly total Your provider's dashboard eventually shows you a monthly bill, but by then it's too late to catch a bug. Computing cost per request live lets you catch problems the moment they happen: a prompt th
Challenge: Bill the Session