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.0.tar.gz (4.3 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.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file genai_pricing-0.2.0.tar.gz.

File metadata

  • Download URL: genai_pricing-0.2.0.tar.gz
  • Upload date:
  • Size: 4.3 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.0.tar.gz
Algorithm Hash digest
SHA256 1a1689e92f26af6634ef97c95f0c93a141b259812e3b4161c0c6d10fccd13d67
MD5 d10d346591ae486b1ae7a14d032c5400
BLAKE2b-256 be641f00a548617e181a7b89a4d616e55390a46e69943637b6beece2ac65c814

See more details on using hashes here.

File details

Details for the file genai_pricing-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: genai_pricing-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9e8ef2e3c113cd7928bee793f0b18bd47479b33131deda22532d4816f988416c
MD5 de3d2dec42557bc3a3ad24ed556cff75
BLAKE2b-256 6e838297f018dcba9074f335b43a40068331cfd9abc3e86007abd5d39b6ea1fd

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