genai_pricing
Core package badges:
Quality and tooling:
Project/community:
Docs:
Estimate GenAI prompt costs from a unified, auto-updated pricing table. This repo provides a small usage-based cost estimator plus parsers for LiteLLM JSON and markdown pricing tables.
- Parses the pricing table at
genai_pricing.PRICING_URLor a local file - Computes costs from a usage dictionary with
prompt_tokensandcompletion_tokens - Includes internal helpers for OpenAI/Gemini-style usage extraction and fallback token counting
Installation
- Python 3.8+
- Packages:
- tiktoken
pip install tiktoken
Quick start
The included example shows how to estimate cost from a model name and token usage dictionary using genai_pricing.estimate_costs. See example.py.
# Minimal example
from genai_pricing import estimate_costs
model = "gpt-4.1"
usage = {"prompt_tokens": 21_549, "completion_tokens": 7_091}
estimate = estimate_costs(model, usage)
print("Cost (USD):", estimate["total_cost"])
print("Details:", estimate)
Run the example:
python example.py
How cost is computed
Prices are looked up by model name in the pricing table, then applied to token counts:
$$ C = \frac{t_\text{in}}{10^6}. p_\text{in} + \frac{t_\text{out}}{10^6}. p_\text{out} $$
- $t_\text{in}$: prompt tokens
- $t_\text{out}$: completion tokens
- $p_\text{in}$: USD per 1M input tokens
- $p_\text{out}$: USD per 1M output tokens
Provide token counts from your model provider when available. Internal helpers can extract OpenAI- and Gemini-style usage metadata and fall back to tiktoken or a lightweight heuristic when needed.
Pricing table
By default, prices are read from genai_pricing.PRICING_URL, the remote LiteLLM JSON source.
To pin a specific table, pass pricing_source as a raw URL or local file path. For a local LiteLLM snapshot, use a path ending in model_prices_and_context_window_backup.json.
Testing
The project uses Python’s built-in unittest.
- Run all tests (discovery):
python -m unittest discover -s test -p "*_test.py" -v
API surface
genai_pricing.estimate_costs- Computes a dict with prompt/completion costs and
total_costfrom a model name, a usage dictionary, and an optionalpricing_source
- Computes a dict with prompt/completion costs and
genai_pricing.clear_pricing_cache- Clears cached pricing data so the configured source is fetched or read again
Key constant:
genai_pricing.PRICING_URL— remote table to fetch by default
License
MIT © 2025 Roberto Rossi
Acknowledgements
Pricing data sourced from the AgentOps tokencost table and mirrored (22 Oct 2025) locally at data/pricing_table.md for testing purposes.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file genai_pricing-0.2.1.tar.gz.
File metadata
- Download URL: genai_pricing-0.2.1.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
63057cf3b92d308ab0da864c92812781c9650ce8da50ac432ddb9bb257183e1d
|
|
| MD5 |
2c35635723983da9c86799efa4c6b5d7
|
|
| BLAKE2b-256 |
67d84576dbd64110edecba621ff4f71c87d8aa19a98dce3952f351a575ff7fb9
|
File details
Details for the file genai_pricing-0.2.1-py3-none-any.whl.
File metadata
- Download URL: genai_pricing-0.2.1-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
82804b79423e9059956827ed8003db98566b5dc11019238d82464a013c1f0304
|
|
| MD5 |
17dd664162e8fa4b0804bd84232edaf4
|
|
| BLAKE2b-256 |
44ecffe92f14f5736daef6f26f0b49230f59fd97048bc04a3e5e279918776a59
|