Price a Call
Finance wants last week's bill for the support bot. Every logged call has a usage block, and the first attempt at pricing them came out ten times too high.
A usage block counts tokens by kind: {"input_tokens": 1200, "output_tokens": 300, "cache_read_tokens": 8000}. The kinds are input, output, cache_read and cache_write, and an absent kind is zero. PRICES (provided) maps a model name to a price for each kind in micro-dollars per million tokens (a micro-dollar is a millionth of a dollar), so tokens times price, divided by a million, is micro-dollars in whole numbers.
Implement two functions.
call_cost(usage, price): the cost of one call in micro-dollars, as an int. Add up all four kinds, then divide, rounding up: a call that cost a tenth of a micro-dollar cost something.
cost_report(calls, prices): calls is a list of {"model": ..., "usage": {...}}. Return a dict:
"by_model": model name to its total in micro-dollars."total": everything, in micro-dollars."unknown": models in the log but not in the price table, each once, sorted. Their calls are left out of the totals, never counted as free.
The catch: round once, at the end, for each figure. A thousand calls at a tenth of a micro-dollar each cost a hundred micro-dollars, not a thousand, which is how the first attempt went wrong.