genai-prices

0.0.56 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This example demonstrates how to use `get_model_price` to calculate the cost for a given model and token usage. It prints the per-1k-token costs and the total cost. If the model is not found, it prints a message.

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

view raw JSON →