genai-prices
genai-prices is a Python library for calculating the cost of interacting with various Large Language Model (LLM) inference APIs. It aggregates pricing information from major providers like OpenAI, Anthropic, Google, AWS Bedrock, and more. The library is actively maintained by Pydantic, with frequent minor releases (typically weekly or bi-weekly) to keep pricing data and model definitions up-to-date.
Common errors
-
AttributeError: 'NoneType' object has no attribute 'total_price'
cause The `calc_price` function returns `None` when it cannot find pricing information for the specified model and/or provider, and the code attempts to access attributes (like `total_price`) on this `None` result without a check.fixAlways check if the result of `calc_price` is not `None` before attempting to access its attributes. Ensure the `model_id` and `provider_id` are correct and present in the library's data, potentially using `genai_prices.list_models()` to verify. -
ValueError: Uncached text input tokens cannot be negative
cause This error occurs when the token usage parameters passed to `calc_price` result in a calculated negative value for uncached input tokens, indicating an inconsistency in how `input_tokens`, `cache_read_tokens`, and `cache_write_tokens` are provided.fixEnsure that `input_tokens` represents the total number of input tokens, and that `cache_read_tokens` and `cache_write_tokens` (if provided) are subsets of `input_tokens` such that `input_tokens - cache_read_tokens - cache_write_tokens` is not negative. If using `cache_read_tokens` and `cache_write_tokens`, `input_tokens` should generally be greater than or equal to their sum. -
ModuleNotFoundError: No module named 'genai_prices'
cause The `genai-prices` package is not installed in the Python environment, or the environment where the code is being run does not have access to the installed package.fixInstall the library using pip: `pip install genai-prices`. If using a virtual environment, activate it before installation. Ensure your IDE or script execution environment is using the correct Python interpreter where the package is installed.
Warnings
- gotcha Model names and pricing are frequently updated. Relying on exact string matches for models can be brittle. It's recommended to keep the library updated to ensure you have the latest model aliases and pricing information.
- breaking As of v0.0.54, the library introduced 'model lifecycle management' meaning models can be explicitly marked as deprecated or removed. If you rely on a model that is subsequently removed, `get_model_price` will return `None`.
- gotcha AWS Bedrock model matching logic was loosened in v0.0.55/v0.0.56 to support inference profiles and regional models. This change might alter which specific model is matched if your input string is ambiguous, potentially leading to different price calculations.
- gotcha Older versions (pre-v0.0.50) might have had incorrect tiered pricing calculations, as a fix for this was included in v0.0.50.
- breaking The `get_model_price` function is no longer directly importable from the `genai_prices` package, leading to an `ImportError`.
- breaking The `get_model_price` function is no longer directly importable from the top-level `genai_prices` package. It has likely been moved, renamed, or replaced by functions such as `get_model_price_and_alias`.
Install
-
pip install genai-prices
Imports
- get_model_price
from genai_prices import get_model_price
- TokenUsage
from genai_prices import TokenUsage
- ModelInfo
from genai_prices.model_info import ModelInfo
from genai_prices import ModelInfo
Quickstart
from genai_prices import get_model_price, TokenUsage
from decimal import Decimal
model_name = "gpt-4o"
usage = TokenUsage(
input_tokens=1000,
output_tokens=1000,
)
price_info = get_model_price(model_name, usage)
if price_info:
print(f"Model: {model_name}")
print(f"Provider: {price_info.provider}")
print(f"Input price per 1k tokens: ${price_info.input_cost_per_token_1k}")
print(f"Output price per 1k tokens: ${price_info.output_cost_per_token_1k}")
print(f"Total cost: ${price_info.total_cost:.6f}")
else:
print(f"Price for model '{model_name}' not found.")