Skip to main content

Calculate prices for calling LLM inference APIs.

Project description

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)

Warning: these prices will not be 100% accurate

See the project README for more information.

Usage

The library provides separated input and output pricing, giving you detailed breakdown of costs:

  • price_data.total_price - Total cost for the request
  • price_data.input_price - Cost for input/prompt tokens
  • price_data.output_price - Cost for output/completion tokens

Since this library may need to make a network call to download prices, both sync and async veriants of calc_price are provided.

Sync API Example

from genai_prices import Usage, calc_price_sync

price_data = calc_price_sync(
    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})")

Async API Example

import asyncio

from genai_prices import Usage, calc_price_async

async def main():
    price_data = await calc_price_async(
        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})")

if __name__ == '__main__':
    asyncio.run(main())

Auto Update

Both calc_price_sync and calc_price_async can be configured to auto-update by passing auto_update=True as an argument. This will cause the library to periodically check for updates to the price data.

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/data.json) so we don't and can't monitor requests or gather telemetry

You may also pass a custom source to auto_update to customize auto-update behavior.

At the time of writing, the data.json file downloaded by auto-update is around 26KB when compressed, so is generally very quick to download.

None-the-less, the library tries hard to avoid making a network call when the user calls calc_price_sync or calc_price_async:

  • data is cached for one hour by default
  • when the cached data is 30minutes old, the library will attempt to update the cache in the background
  • You may pre-fetch data at program startup using genai_prices.prefetch_async() and genai_prices.prefetch_sync(), these are both sync methods which return immediately and update the cache in the background, the only difference is that calc_price_async will wait for the prefetch_async task to complete when it is first called, and calc_price_sync will wait for the prefetch_sync concurrent future to complete when it is first called.

CLI Usage

Run the CLI with:

uvx genai-prices --help

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

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.

Project details


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.0.20.tar.gz (41.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.0.20-py3-none-any.whl (43.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: genai_prices-0.0.20.tar.gz
  • Upload date:
  • Size: 41.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.8

File hashes

Hashes for genai_prices-0.0.20.tar.gz
Algorithm Hash digest
SHA256 87f167123d6d79c918caea6fe44b674131cb83b46d32db3d48fb337320b0a89a
MD5 434686dfa51eb1376e84e0969755c479
BLAKE2b-256 2312b1009aa81c2d08a8b2e77c2b37d5215abfcc3539f90b4e32b2eff8efa406

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for genai_prices-0.0.20-py3-none-any.whl
Algorithm Hash digest
SHA256 ed1b308a6545d1bf84e5ee38215c1a3fc86813738c970401bec2d6745cd37bdc
MD5 ef5afdc76c9dc362edc4287f8bb1cdec
BLAKE2b-256 c850f061da959e203596e404bc76cad336606dd19b488705820cb9c09fabf6dd

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