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.4.tar.gz (40.9 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.4-py3-none-any.whl (42.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for genai_prices-0.0.4.tar.gz
Algorithm Hash digest
SHA256 2a9a15532d75f6be8dfd445b90688ae31416c487bd03bcc0b5010672b502c418
MD5 e90b9ca3527cd1b16db877535b2584f2
BLAKE2b-256 bfd3bceac8b1c300e4eab90e8f1ce0208266b0fbfe29b4842948cee3773fa6f9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for genai_prices-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d6648acbccda97048e0caa3a67643b7a0f10ae6e8231b63dfc1afdd750465445
MD5 fed2d1dffbb5a64db70be8de360fd1f6
BLAKE2b-256 a0101403653865298ed56a6045ced300a38fb980cbdd01649f8881380b67aac0

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