Skip to main content

Simple Library for OpenAI Usage

Project description

openai-usage

PyPI Tests Docs License: MIT

One usage model for every OpenAI API.

The OpenAI Python SDK reports usage in a different shape for almost every endpoint — Chat Completions, Responses, Agents SDK, Realtime, Batch, Embeddings, Images, Transcription. openai-usage collapses them all into a single, additive Usage object and estimates what it costs against live model pricing.

📖 Full documentation: https://allen2c.github.io/openai-usage/

Installation

pip install openai-usage

Requires Python 3.11+.

Quickstart

from openai_usage import Usage

# Normalize usage from any OpenAI API into one shape
total = Usage()
total.add(Usage.from_openai(chat_completion.usage))          # Chat Completions
total.add(Usage.from_openai(response.usage))                 # Responses API
total.add(Usage.from_openai(realtime_event.response.usage))  # Realtime

print(total.input_tokens, total.output_tokens, total.total_tokens)

# Estimate the cost against any model
print(f"${total.estimate_cost('gpt-4o'):.6f}")

What it does

  • UnifyUsage.from_openai(...) accepts Chat Completions, Responses, Agents SDK, Realtime, Batch, Embeddings, Images, Transcription, and Assistants run usage.
  • AggregateUsage.add() accumulates requests, tokens, and per-modality detail (cached, cache-write, reasoning, audio, image, text) across calls.
  • EstimateUsage.estimate_cost() prices usage against any model using OpenRouter pricing, with separate rates for cached, reasoning, audio, and image tokens.
  • SerializeUsage is a Pydantic model with full backward compatibility for older payloads.

See the Supported Usage Types table for the full compatibility matrix, and Estimating Costs for pricing details and limitations.

Documentation

The docs are built with Material for MkDocs and deployed to GitHub Pages on every push to main.

make mkdocs        # serve locally at http://localhost:8000

License

MIT — see LICENSE.

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_usage-0.9.1.tar.gz (76.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_usage-0.9.1-py3-none-any.whl (81.3 kB view details)

Uploaded Python 3

File details

Details for the file openai_usage-0.9.1.tar.gz.

File metadata

  • Download URL: openai_usage-0.9.1.tar.gz
  • Upload date:
  • Size: 76.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.11.13 Darwin/25.5.0

File hashes

Hashes for openai_usage-0.9.1.tar.gz
Algorithm Hash digest
SHA256 e194301fec7e5ba6403a66e5a69d801453b0803d9f74754013a82a49e79fb2e5
MD5 29af1178b20549a6ac3ee43e05d4302f
BLAKE2b-256 ea49ea75be1b889be8fcf77f18d58af43317b905568a26f0f8be639f97d6df2d

See more details on using hashes here.

File details

Details for the file openai_usage-0.9.1-py3-none-any.whl.

File metadata

  • Download URL: openai_usage-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 81.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.11.13 Darwin/25.5.0

File hashes

Hashes for openai_usage-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 878cce4f263dc7c6f37c40492614dd3b35be88a18ffbdc52d0e952b09801430a
MD5 4e24b172e421155aae1df7ea0a2b39d1
BLAKE2b-256 ac3cbbe49e64d467cca67260935fd7c50c15c40afb432fadbc6cc67cd902a8f6

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