Simple Library for OpenAI Usage
Project description
openai-usage
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
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
openai_usage-0.2.3.tar.gz
(68.1 kB
view details)
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_usage-0.2.3.tar.gz.
File metadata
- Download URL: openai_usage-0.2.3.tar.gz
- Upload date:
- Size: 68.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.11.13 Darwin/24.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
08aed2702efd8a0b6cb707ec162ddb35836915d7dff7c1fd5a390d7b673ab072
|
|
| MD5 |
57f604befaca3b4ad8b24ca4c92696e7
|
|
| BLAKE2b-256 |
4e888da712159365c2c5e3ae58011aed0abd09985d28f0bc33fce3a85b661c43
|
File details
Details for the file openai_usage-0.2.3-py3-none-any.whl.
File metadata
- Download URL: openai_usage-0.2.3-py3-none-any.whl
- Upload date:
- Size: 70.2 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cff7b81b2854545c42d1cc17cfecef115c5aebfb3d6d7aceb986a636d266317f
|
|
| MD5 |
d07fa7f5906a8b9a6146ff3b1eca3eb3
|
|
| BLAKE2b-256 |
8c40ca9a14a76da46f0f5593655b25dc41e03824e2aea251ee7fbfb6fd44d554
|