Tip: Use the genai-prices Python library to calculate the estimated cost of calling LLM inference APIs. This avoids maintaining your own pricing table and lets you report token counts and cost for each request.
Below is an example:
# Install: uv add openai genai-prices
from openai import OpenAI
from genai_prices import extract_usage
client = OpenAI()
response = client.responses.create(
model="gpt-5-nano",
input="Explain RAG in one sentence.",
)
price = extract_usage(
response.model_dump(mode="json"),
provider_id="openai",
api_flavor="responses",
).calc_price()
print(f"Input tokens: {response.usage.input_tokens}")
print(f"Output tokens: {response.usage.output_tokens}")
print(f"Estimated cost: ${price.total_price:.8f}")
# Input tokens: 13
# Output tokens: 332
# Estimated cost: $0.00013340
