Skip to main content

genai-prices

CI Coverage PyPI versions license Join Slack

Python package for github.com/pydantic/genai-prices.

Installation

uv add genai-prices

(or pip install genai-prices if you're old school)

To use the CLI with Rich output/help, install the optional CLI dependencies:

uv add "genai-prices[cli]"

(or pip install "genai-prices[cli]")

Warning: these prices will not be 100% accurate

See the project README for more information.

Usage

calc_price

from genai_prices import Usage, calc_price

price_data = calc_price(
    Usage(input_tokens=1000, output_tokens=100),
    model_ref='gpt-4o',
    provider_id='openai',
)
print(f"Total Price: ${price_data.total_price} (input: ${price_data.input_price}, output: ${price_data.output_price})")

extract_usage

extract_usage can be used to extract usage data and the model_ref from response data, which in turn can be used to calculate prices:

from genai_prices import extract_usage

response_data = {
    'model': 'claude-sonnet-4-20250514',
    'usage': {
        'input_tokens': 504,
        'cache_creation_input_tokens': 123,
        'cache_read_input_tokens': 0,
        'output_tokens': 97,
    },
}
extracted_usage = extract_usage(response_data, provider_id='anthropic')
price = extracted_usage.calc_price()
print(price.total_price)

or with OpenAI where there are two API flavors:

from genai_prices import extract_usage

response_data = {
    'model': 'gpt-5',
    'usage': {'prompt_tokens': 100, 'completion_tokens': 200},
}
extracted_usage = extract_usage(response_data, provider_id='openai', api_flavor='chat')
price = extracted_usage.calc_price()
print(price.total_price)

UpdatePrices

UpdatePrices can be used to periodically update the price data by downloading it from GitHub

Please note:

  • this functionality is explicitly opt-in
  • we download data directly from GitHub (https://raw.githubusercontent.com/pydantic/genai-prices/refs/heads/main/prices/new_data/v2/data.json) so we don't and can't monitor requests or gather telemetry

At the time of writing, the v2 data.json file downloaded by UpdatePrices is around 51KB when compressed, so is generally very quick to download.

By default UpdatePrices downloads price data immediately after it's started in the background, then every hour after that.

Usage with UpdatePrices as as context manager:

from genai_prices import UpdatePrices, Usage, calc_price

with UpdatePrices() as update_prices:
    update_prices.wait()  # optionally wait for prices to have updated
    p = calc_price(Usage(input_tokens=123, output_tokens=456), 'gpt-5')
    print(p)

Usage with UpdatePrices as a simple class:

from genai_prices import UpdatePrices, Usage, calc_price

update_prices = UpdatePrices()
update_prices.start(wait=True)  # start updating prices, optionally wait for prices to have updated
p = calc_price(Usage(input_tokens=123, output_tokens=456), 'gpt-5')
print(p)
update_prices.stop()  # stop updating prices

Only one UpdatePrices instance can be running at a time.

If you'd like to wait for prices to be updated without access to the UpdatePrices instance, you can use the wait_prices_updated_sync function:

from genai_prices import wait_prices_updated_sync

wait_prices_updated_sync()
...

Or it's async variant, wait_prices_updated_async.

CLI Usage

Run the CLI with:

uvx genai-prices --help

Or, if installed locally, make sure CLI extras are present:

pip install "genai-prices[cli]"
genai-prices --help

If local CLI extras are not installed, the command will print an install hint for genai-prices[cli].

To list providers and models, run:

uvx genai-prices list

To calculate the price of models, run for example:

uvx genai-prices calc --input-tokens 100000 --output-tokens 3000 o1 o3 claude-opus-4

CLI output notes:

  • Rich output is the default.
  • Use --plain (-p) for legacy/plain output.
  • Use --no-color to keep rich formatting without colors.
  • Use -T / --table for wide table output.

Further Documentation

We do not yet build API documentation for this package, but the source code is relatively simple and well documented.

If you need further information on the API, we encourage you to read the source code.

Fractional usage values

Every reportable usage unit accepts finite non-negative integers or fractional values. For example, duration usage can include fractions of a second:

from decimal import Decimal

from genai_prices import Usage

duration = Usage(audio_seconds=0.1) + Usage(audio_seconds=0.2)
exact_duration = Usage(audio_seconds=Decimal('0.1')) + Usage(audio_seconds=0.2)

assert duration.audio_seconds == 0.3
assert type(duration.audio_seconds) is float
assert exact_duration.audio_seconds == Decimal('0.3')
assert type(Usage(audio_seconds=3.0).audio_seconds) is float

Python preserves supplied int, float, and Decimal values, including 3.0 as a float. Other accepted non-boolean integer implementations normalize to built-in int. Arithmetic interprets floats through their shortest round-trippable decimal spellings: a result is Decimal if any operand was Decimal, otherwise float if any operand was float, otherwise int. This cannot recover decimal precision that was already lost before a float reached Usage.

Standard JSON decoding normally supplies int and float, while callers may request Decimal parsing before extraction. Decimal-bearing usage is not serializable by Python's standard JSON encoder without a custom conversion.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

genai_prices-0.1.4.tar.gz (94.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_prices-0.1.4-py3-none-any.whl (99.2 kB view details)

Uploaded Python 3

File details

Details for the file genai_prices-0.1.4.tar.gz.

File metadata

  • Download URL: genai_prices-0.1.4.tar.gz
  • Upload date:
  • Size: 94.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for genai_prices-0.1.4.tar.gz
Algorithm Hash digest
SHA256 f3b8a0bf0c21b01f5af3ca42babcab2f17728bf3c602f87f42b72d1d2bf61d98
MD5 f13786e313f5545bba25240c3d0f3072
BLAKE2b-256 1ee323f4be7d5626878a6297c728f5f8db83bd9b135da481935d4c1bb82efc21

See more details on using hashes here.

File details

Details for the file genai_prices-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: genai_prices-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 99.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for genai_prices-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1d1b01cc7ab1adfa17c3a1121f4c13990bd0a4377640ecd37ebfde5836768240
MD5 a190145ff704696821046089ff288de8
BLAKE2b-256 e654911961e926a4c4ea30bf1fa0bf9d8ffc5c9ac5ed6a3f77b3d9e0ab5bb9a5

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 Sentry Error logging StatusPage Status page