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

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(price_data.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(price_data.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.3.tar.gz (40.0 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.3-py3-none-any.whl (42.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for genai_prices-0.0.3.tar.gz
Algorithm Hash digest
SHA256 9a6a11f64d51e825223613f40dd4ed3312d4b5b3ddec8c030eb814e52ea0f54d
MD5 c978273d8fb6a9eef0acb8d2120247b2
BLAKE2b-256 4f06398237fceebab444e8f91c974cf88e138e3a9b43d98835df3d2997751ef7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for genai_prices-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 dcae66dec82ccc609e2d4365c3b8b6f192115a39f378d9e5fd5728f21edad80d
MD5 9fd9ef1355224cd7f43a93ca06ffa109
BLAKE2b-256 c8ee7266e6b3b19c9544af4069b94301df2c484fd65e8c1bad4334c9a9b9e1c8

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