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.2.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.2-py3-none-any.whl (42.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: genai_prices-0.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 692f60b5d7ee86dfbbe27c7466cc850f23778aa2220ac3b405d6d88f7fc875a3
MD5 811998e2dddc0b864909722c59b6ee1d
BLAKE2b-256 1e465e486eeda6d648fb24c4079edb91afdd90eff9d2bfa234c42f783aae5504

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for genai_prices-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f331748d767ee75c6a12369edc315e273f628b50d3e60a62041c1cd0d33b7daa
MD5 09fcd3ed8f3b7e9aa3e157d326d164fe
BLAKE2b-256 7437681df1d839edc4b71a355231c5b0e4789fd216f22b7cb85d01069deca243

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