LLM APIs charge per million tokens, with different prices for input and output, and a discount for input tokens served from the prompt cache.
Implement estimate_cost(calls, prices):
prices maps a model name to {"input": $, "output": $, "cached_input": $}, each per 1M tokens."model", "input_tokens", "output_tokens" and optionally "cached_tokens" (default 0). Cached tokens are part of input_tokens but billed at the cached rate instead of the normal one."total" (the cost in dollars, rounded to 6 decimal places) and "by_model" (cost per model, also rounded to 6 places).KeyError for a model that isn't in prices.Example: 1,000,000 input tokens, 400,000 of them cached, plus 100,000 output tokens, at 0.30 cached / 3 + 400k × 15 = 0.12 + 3.42**.