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 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:

  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

Release files for genai-pricing 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for genai-pricing 1.0.0
File Size Uploaded
genai_pricing-1.0.0.tar.gz 9.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for genai-pricing 1.0.0
File Interpreter ABI Platform
genai_pricing-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.3 kB

Release files / genai_pricing-1.0.0.tar.gz

Download URL genai_pricing-1.0.0.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
0bfe74c7491578c842abccde9b7a4a51f4dcd4a43c06286e541964552c50b412
BLAKE2b-256 checksum
How to use checksums
cbb5a02892a3bb0933f2086e64c3d9229bc70192517e821aa3b8fc8dfd720357
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.12

Release files / genai_pricing-1.0.0-py3-none-any.whl

Download URL genai_pricing-1.0.0-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
625c1b598a912cbdb5fd5bcbdc9468119261e08572f858065842b03111a72332
BLAKE2b-256 checksum
How to use checksums
6f81d0888b42a68ad814562af69efa966f5a0466564efe659a7f4a75b8836b9e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.12

Release history Release notifications | RSS feed

1.0.2

2 release files

1.0.1

2 release files

This release

1.0.0 This release

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page