Exchange
A cost calculator
1 file2 KB
A look inside
The first lines of cost.zip. The whole file is in your library once you get it.
# ABOUTME: A small cost calculator: tokens in and out, times the price per million tokens you read today.
# ABOUTME: Usage: python3 cost.py <price_in> <price_out> <tokens_in> <tokens_out> [runs_per_day] (prices in $ per million)
import sys
# No prices are built in: they change. Read today's price per million tokens for your model on the
# provider's pricing page and pass it in. The same goes for cache and batch discounts, if you use them.
DAYS_PER_MONTH = 30
def run_cost(price_in, price_out, tokens_in, tokens_out, cached_share=0.0, cache_price=None, batch_discount=0.0):
"""Dollars for one run.
price_in, price_out: dollars per million input and output tokens, as listed today.
cached_share: the part of the input read from the cache (0 to 1).
cache_price: dollars per million cached input tokens, as listed today (needed when cached_share > 0).
batch_discount: the share taken off for batch runs, as listed today (0.5 means half off).
"""
if min(price_in, price_out, tokens_in, tokens_out) < 0:
raise ValueError("prices and token counts cannot be negative")
if not 0 <= cached_share <= 1:
raise ValueError("cached_share must be between 0 and 1")
if cached_share > 0 and cache_price is None:
raise ValueError("give cache_price (today's price per million cached input tokens) to use cached_share")
if not 0 <= batch_discount < 1:
…and 23 more lines.What is inside
- cost.zip2 KB
How to use it
A small cost calculator: tokens in and out, times the price per million tokens, for one run and for a month of runs.
No prices are built in, because they change. Read today's price for your model on your provider's pricing page, then run python3 cost.py <price_in> <price_out> 20000 2000 10.
Run before it was listed
Tested 2026-11-27: python3 -I -m unittest: 6 tests passed.