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 a local LiteLLM pricing snapshot when present, otherwise falls back to
genai_pricing.PRICING_URL - Computes costs from prompt, completion, cache-creation, and cache-read token usage
- 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 an args-like object and token usage dictionary using genai_pricing.estimate_costs. See example.py.
# Minimal example
import os
from openai import OpenAI
from types import SimpleNamespace
from genai_pricing import estimate_costs
"""Estimate the cost of an OpenAI prompt using genai_pricing."""
api_key = os.environ.get("OPENAI_API_KEY")
client = OpenAI(api_key=api_key)
model = "gpt-5.6-sol"
prompt = "Why is the sky blue?"
resp = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}],
max_completion_tokens=50,
)
answer = resp.choices[0].message.content
usage = {
"prompt_tokens": resp.usage.prompt_tokens,
"completion_tokens": resp.usage.completion_tokens,
"cache_read_input_tokens": resp.usage.prompt_tokens_details.cached_tokens,
}
args = SimpleNamespace(model=model)
estimate = estimate_costs(args, usage) # <- use this line in your project
print("Cost (USD):", estimate["total_cost"])
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. prompt_tokens is the provider-reported total input count; cache creation and cache read counts are subsets charged in place of regular input:
$$ C = \frac{(t_\text{in} - t_\text{create} - t_\text{read})p_\text{in} + t_\text{create}p_\text{create} + t_\text{read}p_\text{read} + t_\text{out}p_\text{out}}{10^6} $$
- $t_\text{in}$: prompt tokens
- $t_\text{create}$:
cache_creation_input_tokens, when reported - $t_\text{read}$:
cache_read_input_tokens, when reported - $t_\text{out}$: completion tokens
- $p$: the corresponding USD price per 1M tokens
The result includes prompt_cost, cache_creation_cost, cache_read_cost, and completion_cost when applicable, plus total_cost. If a model has no cache-specific rate, cached tokens fall back to its regular input rate.
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 resolved in this order:
model_prices_and_context_window_backup.jsonin the current working directorymodel_prices_and_context_window_backup.jsonnear the package/repository locationgenai_pricing.PRICING_URL, the remote LiteLLM JSON source
The parsed pricing table is cached. Call clear_pricing_cache() after changing the local snapshot or when you want the remote source fetched again.
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 an object with a.modelattribute and a usage dictionary
- 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.3.tar.gz.
File metadata
- Download URL: genai_pricing-0.2.3.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f00fc915653d5ba47fdc5f1864eeab47b7ee235e370fe00fd1ae032e77fcad3
|
|
| MD5 |
7e66cb2358abba891c41a4efdd455c82
|
|
| BLAKE2b-256 |
22f8f49dc47ffa1c6426abb62b998249cefce15066b68779a312c89a8acb957b
|
File details
Details for the file genai_pricing-0.2.3-py3-none-any.whl.
File metadata
- Download URL: genai_pricing-0.2.3-py3-none-any.whl
- Upload date:
- Size: 9.1 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 |
6eb3e0db7e8d0c8a3db5934ddb7871530e6db68b354dd8c2bf6ca994e2de9a35
|
|
| MD5 |
fc067b4101d0fdc1a4af4177bb235cbc
|
|
| BLAKE2b-256 |
179e23893a53e15beceb101d1f6d5e2e1caa1e0f34dd1af14dee4ab31bd51d87
|