A library to estimate OpenAI API costs based on token usage.
Project description
openai_cost_calculator
Instant, accurate USD cost estimates for OpenAI & Azure OpenAI API calls. Works with Chat Completions and the Responses API, streaming or not. Offers a typed Decimal-based API for finance-safe math and a legacy string API for drop-ins.
Docs: https://orkunkinay.github.io/openai_cost_calculator/
Installation
pip install openai-cost-calculator
Import name uses underscores:
import openai_cost_calculator
Quickstart
Typed (recommended)
from openai import OpenAI
from openai_cost_calculator import estimate_cost_typed
client = OpenAI()
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Hi there!"}],
)
cost = estimate_cost_typed(resp) # -> CostBreakdown (Decimal fields)
print(cost.total_cost) # Decimal('0.00000750')
print(cost.as_dict(stringify=True)) # 8-dp strings if you prefer
Legacy (string output)
from openai_cost_calculator import estimate_cost
print(estimate_cost(resp)) # dict of 8-dp strings
Responses API
resp = client.responses.create(model="gpt-4.1-mini", input=[{"role":"user","content":"Hi"}])
from openai_cost_calculator import estimate_cost_typed
print(estimate_cost_typed(resp))
Streaming
stream = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role":"user","content":"Hi"}],
stream=True,
stream_options={"include_usage": True},
)
from openai_cost_calculator import estimate_cost_typed
print(estimate_cost_typed(stream))
Highlights
- Typed API:
CostBreakdowndataclass withDecimalprecision - Drop-in legacy API: 8-decimal strings (backward compatible)
- Handles edge cases: cached tokens, undated model strings, streaming generators, Azure deployment names
- Pricing sources: Remote CSV (24h cache) + local overrides and offline mode
Pricing utilities
from openai_cost_calculator import (
refresh_pricing, set_offline_mode,
add_pricing_entry, add_pricing_entries, clear_local_pricing
)
# Force refresh (bypasses 24h cache)
refresh_pricing()
# Run fully offline (no network calls)
set_offline_mode(True)
# Teach custom prices (per 1M tokens)
add_pricing_entry(
"ollama/qwen3:30b", "2025-08-01",
input_price=0.20, output_price=0.60, cached_input_price=0.04,
minimum_tokens=0, # optional tier floor; default is 0
)
If a model has tiered pricing by prompt/input size, add multiple rows for the same
(model_name, model_date) with different minimum_tokens values. The calculator picks
the highest matching tier where minimum_tokens <= prompt_tokens.
Remote CSV (auto-fetched, cached 24h):
https://raw.githubusercontent.com/orkunkinay/openai_cost_calculator/refs/heads/main/data/gpt_pricing_data.csv
Errors
Recoverable issues raise CostEstimateError with a clear message (missing pricing row, unexpected input shape, etc.).
Troubleshooting
- “Pricing not found” → confirm row exists in the CSV; call
refresh_pricing(). cached_tokens = 0→ ensureinclude_usage_details=True(classic) orstream_options={"include_usage": True}(streaming).- Model string has no date → the latest row with
date ≤ todayis used.
Links
- Docs & examples: https://orkunkinay.github.io/openai_cost_calculator/
- Source: https://github.com/orkunkinay/openai_cost_calculator
- Issues: https://github.com/orkunkinay/openai_cost_calculator/issues
License
MIT © 2025 Orkun Kınay & Murat Barkın Kınay
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openai_cost_calculator-1.2.0.tar.gz.
File metadata
- Download URL: openai_cost_calculator-1.2.0.tar.gz
- Upload date:
- Size: 16.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
327c051439f8a9241a4a51b2eae458925adc637dc2a1144371a2ca9e97f82be2
|
|
| MD5 |
d0d6b65f185198730e6f4eace8bcfd16
|
|
| BLAKE2b-256 |
7e6ba2b48f14cdfe8cc6e611b84b48bebaf33fbb6184bed96e5a8b5bca37b1b3
|
File details
Details for the file openai_cost_calculator-1.2.0-py3-none-any.whl.
File metadata
- Download URL: openai_cost_calculator-1.2.0-py3-none-any.whl
- Upload date:
- Size: 12.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7bc3b567d9a694671bb00cac119e75e3533720a6f19d9dad2f25a5212d792073
|
|
| MD5 |
32711ffd1e4d181dbcd3722d9ea063cf
|
|
| BLAKE2b-256 |
1366b4c07fa0cc2434fc6f1a744e6dd189f01cc5fe489af8f9a9ef99d7597353
|