Join usage to a price table and fill missing prices with a default
Cost reports join a usage log to a price sheet. New or self-hosted models are often missing from the sheet, and dropping them silently understates the bill.
Write cost_report(calls, prices, default_price) where calls has columns model (str) and tokens (int), many rows per model, and prices has columns model and usd_per_1k (float), at most one row per model.
Return a DataFrame with columns exactly [model, tokens, cost]: one row per model that appears in calls, with tokens summed and cost = tokens / 1000 * price, using default_price when the model has no row in prices. Models only in prices are not included. Sort by cost descending, then model ascending, with a plain 0..n-1 index.
def cost_report(calls, prices, default_price)Example
calls a:1000, b:2000; prices a:0.5; default 1.0 gives [('b', 2000, 2.0), ('a', 1000, 0.5)].
Understand the idea
A cost report joins usage to a price sheet. New or self-hosted models are often missing from the sheet, and an inner join would silently drop them, making the bill look smaller than it is. So sum tokens per model first, keep every used model with a left join, and fill any missing price with a default. Worked example: calls a: 500 and 500 tokens, c: 3000 tokens. Prices: a 0.5, b 2.0 per 1k. Default 0.2. Totals: a 1000, c 3000. a costs 1000 / 1000 * 0.5 = 0.5. c has no price, so 3000 / 1000 * 0.2 = 0.6. Model b appears only in the price sheet, so it is not in the output. Sorted by cost: [('c', 3000, 0.6), ('a', 1000, 0.5)]. Rule: group calls by model and sum tokens; left-merge prices on model; replace missing usd_per_1k with default_price; cost = tokens / 1000 * price; keep columns exactly [model, tokens, cost]; sort by cost descending then model ascending; reset to a plain 0 to n-1 index. This pattern underlies any LLM spend report that mixes hosted and self-hosted models.