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 a local LiteLLM pricing snapshot when present, otherwise falls back to genai_pricing.PRICING_URL
  • 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 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,
}
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:

$$ 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 resolved in this order:

  1. model_prices_and_context_window_backup.json in the current working directory
  2. model_prices_and_context_window_backup.json near the package/repository location
  3. genai_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

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.2.tar.gz (8.6 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.2-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: genai_pricing-0.2.2.tar.gz
  • Upload date:
  • Size: 8.6 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.2.tar.gz
Algorithm Hash digest
SHA256 17683df4aa17b9a575494061f915273c386e557e01b8df5aebd8fbefa3f6805c
MD5 b80cdab773c9759cc4fdfeaec153ce5a
BLAKE2b-256 7a5fe16167d0ac4fecfaf3c669428bb78bfba3e6189b575adaca028940ed3ae3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: genai_pricing-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 8.5 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0fb4aafe09f3eb7b2f2695be0cc447be7c7971d029448d25f589145383379500
MD5 5652a4799a50e4a802d259d8d9d1f646
BLAKE2b-256 715f09c860e42011b81fde01b6e3971ae7d141ea8d103684a0e87a0b44aafa8e

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