chatbot1-7 Exercise 1: Cost by Hand and by Report ==================================================== The prices are EXAMPLES (per million tokens), not any vendor's. Copy real ones from your provider's pricing page and note the date. ex1.py (save in the project root): import asyncio from bot.core import Bot from bot.costs import Price, cost_of from bot.limits import Limits from bot.providers import FakeProvider, Reply, Usage # EXAMPLE prices (per million tokens), not any vendor's PRICES = {"example-small": Price(1.0, 5.0), "example-large": Price(4.0, 20.0)} CALLS = [("example-small", 1200, 300), ("example-large", 3000, 800), ("example-small", 800, 150)] by_hand = 0.0 for model, tin, tout in CALLS: price = PRICES[model] cost = tin / 1e6 * price.input_per_mtok + tout / 1e6 * price.output_per_mtok by_hand += cost print(f"{model:<14} {tin:>5} in {tout:>4} out = {cost:.6f}") print(f"by hand total: {by_hand:.6f}") async def main(): fake = FakeProvider([Reply("ok", Usage(i, o), model=m) for m, i, o in CALLS]) bot = Bot(fake, limits=Limits(burst=100, prices=PRICES)) for n in range(3): await bot.handle("s", f"question {n}") print() print(bot.report()) print(f"report total : {sum(r[5] for r in bot.store.usage_report(0)):.6f}") asyncio.run(main()) Run with: py ex1.py Output: example-small 1200 in 300 out = 0.002700 example-large 3000 in 800 out = 0.028000 example-small 800 in 150 out = 0.001550 by hand total: 0.032250 served by model calls in tok out tok cost fake example-large 1 3000 800 0.028000 fake example-small 2 2000 450 0.004250 report total : 0.032250 WHY THIS WORKS AS AN ANSWER --------------------------- The cost of a call is input tokens x input price + output tokens x output price, each per million. By hand the three calls cost 0.002700, 0.028000 and 0.001550, total 0.032250. The Bot recorded each call as it was answered, keyed by the model name the reply reported, and the report grouped them: the two small-model calls add up to 0.004250 and the large-model call is 0.028000, and the total is again 0.032250. Both routes agree because they use the same arithmetic on the same recorded tokens. If a call's model had not been in the price table, the report would have shown "unpriced" for that row instead of quietly leaving it out.