Skip to main content

genai_pricing

Core package badges:

Codecov (with branch) Python package Lint and type-check License Release PYPI Downloads

Quality and tooling:

Code style: black Ruff mypy

Project/community:

Issues PRs Stars

Docs:

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_URL or a local file
  • Computes costs from a usage dictionary with prompt_tokens and completion_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

Key constant:

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

genai_pricing-0.2.1.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

genai_pricing-0.2.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

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

Hashes for genai_pricing-0.2.1.tar.gz
Algorithm Hash digest
SHA256 63057cf3b92d308ab0da864c92812781c9650ce8da50ac432ddb9bb257183e1d
MD5 2c35635723983da9c86799efa4c6b5d7
BLAKE2b-256 67d84576dbd64110edecba621ff4f71c87d8aa19a98dce3952f351a575ff7fb9

See more details on using hashes here.

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

Hashes for genai_pricing-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 82804b79423e9059956827ed8003db98566b5dc11019238d82464a013c1f0304
MD5 17dd664162e8fa4b0804bd84232edaf4
BLAKE2b-256 44ecffe92f14f5736daef6f26f0b49230f59fd97048bc04a3e5e279918776a59

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page