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.17.tar.gz (41.2 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.17-py3-none-any.whl (43.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for genai_prices-0.0.17.tar.gz
Algorithm Hash digest
SHA256 21feba73de5dd739114d98960fd89ea30fcddb473fa0cf8520a04a5bb1131f0b
MD5 ac00e1dfd264e96eef626474eb632c4d
BLAKE2b-256 9ea80ccf6eecfaa4db101c4948181c08ba7141b272805f52916d53acc4ff9ef1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for genai_prices-0.0.17-py3-none-any.whl
Algorithm Hash digest
SHA256 fe96d7edd1a7d3301c062ef8e58b4f1de9196f90e81247e9c7b5346cb9085ea2
MD5 5a937cde69c27c2d030112a4fa3b502a
BLAKE2b-256 9df98e01bdc111b80e9b8ce4228c4008e1bf618401194f1fe6a494ed50436670

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