genai-prices


Namegenai-prices JSON
Version 0.0.25 PyPI version JSON
download
home_pageNone
SummaryCalculate prices for calling LLM inference APIs.
upload_time2025-09-01 17:30:42
maintainerNone
docs_urlNone
authorSamuel Colvin
requires_python>=3.9
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <div align="center">
  <h1>genai-prices</h1>
</div>
<div align="center">
  <a href="https://github.com/pydantic/genai-prices/actions/workflows/ci.yml?query=branch%3Amain"><img src="https://github.com/pydantic/genai-prices/actions/workflows/ci.yml/badge.svg?event=push" alt="CI"></a>
  <a href="https://coverage-badge.samuelcolvin.workers.dev/redirect/pydantic/genai-prices"><img src="https://coverage-badge.samuelcolvin.workers.dev/pydantic/genai-prices.svg" alt="Coverage"></a>
  <a href="https://pypi.python.org/pypi/genai-prices"><img src="https://img.shields.io/pypi/v/genai-prices.svg" alt="PyPI"></a>
  <a href="https://github.com/pydantic/genai-prices"><img src="https://img.shields.io/pypi/pyversions/genai-prices.svg" alt="versions"></a>
  <a href="https://github.com/pydantic/genai-prices/blob/main/LICENSE"><img src="https://img.shields.io/github/license/pydantic/genai-prices.svg" alt="license"></a>
  <a href="https://logfire.pydantic.dev/docs/join-slack/"><img src="https://img.shields.io/badge/Slack-Join%20Slack-4A154B?logo=slack" alt="Join Slack" /></a>
</div>

<br/>
<div align="center">
  Python package for <a href="https://github.com/pydantic/genai-prices">github.com/pydantic/genai-prices</a>.
</div>
<br/>

## Installation

```bash
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](https://github.com/pydantic/genai-prices?tab=readme-ov-file#warning) for more information.

## Usage

### `calc_price`

```python
from genai_prices import Usage, calc_price

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

### `extract_usage`

`extract_usage` can be used to extract usage data and the `model_ref` from response data,
which in turn can be used to calculate prices:

```py
from genai_prices import extract_usage

response_data = {
    'model': 'claude-sonnet-4-20250514',
    'usage': {
        'input_tokens': 504,
        'cache_creation_input_tokens': 123,
        'cache_read_input_tokens': 0,
        'output_tokens': 97,
    },
}
extracted_usage = extract_usage(response_data, provider_id='anthropic')
price = extracted_usage.calc_price()
print(price.total_price)
```

or with OpenAI where there are two API flavors:

```py
from genai_prices import extract_usage

response_data = {
    'model': 'gpt-5',
    'usage': {'prompt_tokens': 100, 'completion_tokens': 200},
}
extracted_usage = extract_usage(response_data, provider_id='openai', api_flavor='chat')
price = extracted_usage.calc_price()
print(price.total_price)
```

### `UpdatePrices`

`UpdatePrices` can be used to periodically update the price data by downloading it from GitHub

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

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

By default `UpdatePrices` downloads price data immediately after it's started in the background, then every hour after that.

Usage with `UpdatePrices` as as context manager:

```py
from genai_prices import UpdatePrices, Usage, calc_price

with UpdatePrices() as update_prices:
    update_prices.wait()  # optionally wait for prices to have updated
    p = calc_price(Usage(input_tokens=123, output_tokens=456), 'gpt-5')
    print(p)
```

Usage with `UpdatePrices` as a simple class:

```py
from genai_prices import UpdatePrices, Usage, calc_price

update_prices = UpdatePrices()
update_prices.start(wait=True)  # start updating prices, optionally wait for prices to have updated
p = calc_price(Usage(input_tokens=123, output_tokens=456), 'gpt-5')
print(p)
update_prices.stop()  # stop updating prices
```

Only one `UpdatePrices` instance can be running at a time.

If you'd like to wait for prices to be updated without access to the `UpdatePrices` instance, you can use the `wait_prices_updated_sync` function:

```py
from genai_prices import wait_prices_updated_sync

wait_prices_updated_sync()
...
```

Or it's async variant, `wait_prices_updated_async`.

### CLI Usage

Run the CLI with:

```bash
uvx genai-prices --help
```

To list providers and models, run:

```bash
uvx genai-prices list
```

To calculate the price of models, run for example:

```bash
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.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "genai-prices",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": null,
    "author": "Samuel Colvin",
    "author_email": "Samuel Colvin <samuel@pydantic.dev>",
    "download_url": "https://files.pythonhosted.org/packages/02/9e/f292acaf69bd209b354ef835cab4ebe845eced05c4db85e3b31585429806/genai_prices-0.0.25.tar.gz",
    "platform": null,
    "description": "<div align=\"center\">\n  <h1>genai-prices</h1>\n</div>\n<div align=\"center\">\n  <a href=\"https://github.com/pydantic/genai-prices/actions/workflows/ci.yml?query=branch%3Amain\"><img src=\"https://github.com/pydantic/genai-prices/actions/workflows/ci.yml/badge.svg?event=push\" alt=\"CI\"></a>\n  <a href=\"https://coverage-badge.samuelcolvin.workers.dev/redirect/pydantic/genai-prices\"><img src=\"https://coverage-badge.samuelcolvin.workers.dev/pydantic/genai-prices.svg\" alt=\"Coverage\"></a>\n  <a href=\"https://pypi.python.org/pypi/genai-prices\"><img src=\"https://img.shields.io/pypi/v/genai-prices.svg\" alt=\"PyPI\"></a>\n  <a href=\"https://github.com/pydantic/genai-prices\"><img src=\"https://img.shields.io/pypi/pyversions/genai-prices.svg\" alt=\"versions\"></a>\n  <a href=\"https://github.com/pydantic/genai-prices/blob/main/LICENSE\"><img src=\"https://img.shields.io/github/license/pydantic/genai-prices.svg\" alt=\"license\"></a>\n  <a href=\"https://logfire.pydantic.dev/docs/join-slack/\"><img src=\"https://img.shields.io/badge/Slack-Join%20Slack-4A154B?logo=slack\" alt=\"Join Slack\" /></a>\n</div>\n\n<br/>\n<div align=\"center\">\n  Python package for <a href=\"https://github.com/pydantic/genai-prices\">github.com/pydantic/genai-prices</a>.\n</div>\n<br/>\n\n## Installation\n\n```bash\nuv add genai-prices\n```\n\n(or `pip install genai-prices` if you're old school)\n\n## Warning: these prices will not be 100% accurate\n\nSee [the project README](https://github.com/pydantic/genai-prices?tab=readme-ov-file#warning) for more information.\n\n## Usage\n\n### `calc_price`\n\n```python\nfrom genai_prices import Usage, calc_price\n\nprice_data = calc_price(\n    Usage(input_tokens=1000, output_tokens=100),\n    model_ref='gpt-4o',\n    provider_id='openai',\n)\nprint(f\"Total Price: ${price_data.total_price} (input: ${price_data.input_price}, output: ${price_data.output_price})\")\n```\n\n### `extract_usage`\n\n`extract_usage` can be used to extract usage data and the `model_ref` from response data,\nwhich in turn can be used to calculate prices:\n\n```py\nfrom genai_prices import extract_usage\n\nresponse_data = {\n    'model': 'claude-sonnet-4-20250514',\n    'usage': {\n        'input_tokens': 504,\n        'cache_creation_input_tokens': 123,\n        'cache_read_input_tokens': 0,\n        'output_tokens': 97,\n    },\n}\nextracted_usage = extract_usage(response_data, provider_id='anthropic')\nprice = extracted_usage.calc_price()\nprint(price.total_price)\n```\n\nor with OpenAI where there are two API flavors:\n\n```py\nfrom genai_prices import extract_usage\n\nresponse_data = {\n    'model': 'gpt-5',\n    'usage': {'prompt_tokens': 100, 'completion_tokens': 200},\n}\nextracted_usage = extract_usage(response_data, provider_id='openai', api_flavor='chat')\nprice = extracted_usage.calc_price()\nprint(price.total_price)\n```\n\n### `UpdatePrices`\n\n`UpdatePrices` can be used to periodically update the price data by downloading it from GitHub\n\nPlease note:\n\n- this functionality is explicitly opt-in\n- 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\n\nAt the time of writing, the `data.json` file\ndownloaded by `UpdatePrices` is around 26KB when compressed, so is generally very quick to download.\n\nBy default `UpdatePrices` downloads price data immediately after it's started in the background, then every hour after that.\n\nUsage with `UpdatePrices` as as context manager:\n\n```py\nfrom genai_prices import UpdatePrices, Usage, calc_price\n\nwith UpdatePrices() as update_prices:\n    update_prices.wait()  # optionally wait for prices to have updated\n    p = calc_price(Usage(input_tokens=123, output_tokens=456), 'gpt-5')\n    print(p)\n```\n\nUsage with `UpdatePrices` as a simple class:\n\n```py\nfrom genai_prices import UpdatePrices, Usage, calc_price\n\nupdate_prices = UpdatePrices()\nupdate_prices.start(wait=True)  # start updating prices, optionally wait for prices to have updated\np = calc_price(Usage(input_tokens=123, output_tokens=456), 'gpt-5')\nprint(p)\nupdate_prices.stop()  # stop updating prices\n```\n\nOnly one `UpdatePrices` instance can be running at a time.\n\nIf you'd like to wait for prices to be updated without access to the `UpdatePrices` instance, you can use the `wait_prices_updated_sync` function:\n\n```py\nfrom genai_prices import wait_prices_updated_sync\n\nwait_prices_updated_sync()\n...\n```\n\nOr it's async variant, `wait_prices_updated_async`.\n\n### CLI Usage\n\nRun the CLI with:\n\n```bash\nuvx genai-prices --help\n```\n\nTo list providers and models, run:\n\n```bash\nuvx genai-prices list\n```\n\nTo calculate the price of models, run for example:\n\n```bash\nuvx genai-prices calc --input-tokens 100000 --output-tokens 3000 o1 o3 claude-opus-4\n```\n\n## Further Documentation\n\nWe do not yet build API documentation for this package, but the source code is relatively simple and well documented.\n\nIf you need further information on the API, we encourage you to read the source code.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Calculate prices for calling LLM inference APIs.",
    "version": "0.0.25",
    "project_urls": {
        "Changelog": "https://github.com/pydantic/genai-prices/releases",
        "Homepage": "https://github.com/pydantic/genai-prices",
        "Source": "https://github.com/pydantic/genai-prices"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "861241fcfba4ae0f6b4805f09d11f0e6d6417df2572cea13208c0f439170ee0c",
                "md5": "7790c82e1172e2fd848c9904a7c62bc4",
                "sha256": "47b412e6927787caa00717a5d99b2e4c0858bed507bb16473b1bcaff48d5aae9"
            },
            "downloads": -1,
            "filename": "genai_prices-0.0.25-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7790c82e1172e2fd848c9904a7c62bc4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 47002,
            "upload_time": "2025-09-01T17:30:41",
            "upload_time_iso_8601": "2025-09-01T17:30:41.012683Z",
            "url": "https://files.pythonhosted.org/packages/86/12/41fcfba4ae0f6b4805f09d11f0e6d6417df2572cea13208c0f439170ee0c/genai_prices-0.0.25-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "029ef292acaf69bd209b354ef835cab4ebe845eced05c4db85e3b31585429806",
                "md5": "1915cdcf0b2aa533df71fa3ce8dccf97",
                "sha256": "caf5fe2fd2248e87f70b2b44bbf8b3b52871abfc078a5e35372c40aca4cc4450"
            },
            "downloads": -1,
            "filename": "genai_prices-0.0.25.tar.gz",
            "has_sig": false,
            "md5_digest": "1915cdcf0b2aa533df71fa3ce8dccf97",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 44693,
            "upload_time": "2025-09-01T17:30:42",
            "upload_time_iso_8601": "2025-09-01T17:30:42.185533Z",
            "url": "https://files.pythonhosted.org/packages/02/9e/f292acaf69bd209b354ef835cab4ebe845eced05c4db85e3b31585429806/genai_prices-0.0.25.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-09-01 17:30:42",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "pydantic",
    "github_project": "genai-prices",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "genai-prices"
}
        
Elapsed time: 1.29065s