Skip to main content

A library to estimate OpenAI API costs based on token usage.

Project description

openai_cost_calculator

PyPI version License

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: CostBreakdown dataclass with Decimal precision
  • 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
)

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 → ensure include_usage_details=True (classic) or stream_options={"include_usage": True} (streaming).
  • Model string has no date → the latest row with date ≤ today is used.

Links


License

MIT © 2025 Orkun Kınay & Murat Barkın Kınay

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openai_cost_calculator-1.1.1.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openai_cost_calculator-1.1.1-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file openai_cost_calculator-1.1.1.tar.gz.

File metadata

  • Download URL: openai_cost_calculator-1.1.1.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for openai_cost_calculator-1.1.1.tar.gz
Algorithm Hash digest
SHA256 2c582ee31ce4fd122e42f6d306f0d88fe2791414b225a6a7e3dca348f85f3227
MD5 7449715e48a55995aa51b215a53e135b
BLAKE2b-256 601631984fac82b7dcc9e97b9d381cb3dbdc174d5eed92e17ef1ffe1440daafd

See more details on using hashes here.

File details

Details for the file openai_cost_calculator-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for openai_cost_calculator-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 64420f24c786d493e5944d446c61723af4d397219f9e3837c66867459601e27e
MD5 1f67121cf97525cc6c900c82409bab49
BLAKE2b-256 199e8b4fde2e1af652874f5770b804781eed9739108dd5e1b7e7b2ef6e472bdb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page