Skip to main content

Simple Library for OpenAI Usage

Project description

openai-usage

PyPI

Utilities to track and estimate API usage costs.

Installation

pip install openai-usage

Usage

Tracking Token Usage

Track usage manually or create Usage objects from OpenAI's response objects.

from openai_usage import Usage
from openai.types.completion_usage import CompletionUsage

# Manually create a Usage object
manual_usage = Usage(requests=1, input_tokens=100, output_tokens=200, total_tokens=300)

# Create from an OpenAI CompletionUsage object
openai_completion_usage = CompletionUsage(prompt_tokens=50, completion_tokens=150, total_tokens=200)
usage_from_openai = Usage.from_openai(openai_completion_usage)

# Add usage objects together
combined_usage = Usage()
combined_usage.add(manual_usage)
combined_usage.add(usage_from_openai)

print(f"Total requests: {combined_usage.requests}")
print(f"Total input tokens: {combined_usage.input_tokens}")
print(f"Total output tokens: {combined_usage.output_tokens}")

Estimating Costs

Estimate the cost of your API usage for any supported model.

from openai_usage import Usage

# Create a usage object
usage = Usage(requests=1, input_tokens=1000, output_tokens=2000, total_tokens=3000)

# Estimate cost for a model
# The library fetches pricing data from an external API
cost = usage.estimate_cost("anthropic/claude-3-haiku")
print(f"Estimated cost for the model: ${cost:.6f}")

# For the most up-to-date pricing, use realtime_pricing=True
cost_realtime = usage.estimate_cost("anthropic/claude-3-haiku", realtime_pricing=True)
print(f"Estimated real-time cost for the model: ${cost_realtime:.6f}")

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.3.3.tar.gz (68.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.3.3-py3-none-any.whl (70.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: openai_usage-0.3.3.tar.gz
  • Upload date:
  • Size: 68.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.11.13 Darwin/24.6.0

File hashes

Hashes for openai_usage-0.3.3.tar.gz
Algorithm Hash digest
SHA256 d6c7ae2ad6c8e21d84683409742964717c971e237ea29933da4574836d6caa8c
MD5 8314f0802aa81d728e5cd5f93670557d
BLAKE2b-256 be4350a524bd41afef80a606cc1d6b6d5d0f68766542de255ccadc727f20262c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: openai_usage-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 70.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.11.13 Darwin/24.6.0

File hashes

Hashes for openai_usage-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 89ecfcf366d0821a54070ed825c32c17b9a08e93d6743e360cf62d4cdf199f38
MD5 f9ecd9e950a3758640976016c79d739c
BLAKE2b-256 0b5b7965a6074b6fe3eedb0d76c8d64a4b610edb5f6b16b48d2fd8ec13bafebf

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