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.
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.
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 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.")