OpenAI Cost Tracker
A simple light weight wrapper for OpenAI's API that tracks the cost of each request. It also provides simulation mode to test the cost of a request without actually sending it to OpenAI.
Installation
pip install openai-cost-tracker
Usage
Import the query_openai function from openai_cost_tracker:
from openai_cost_tracker import query_openai
Use query_openai function as a drop-in replacement for openAI's completion function, such as openai.Completion.create(). The query_openai will return the same response as the original function, but will also print the cost of the request.
Turn on simulation to test the cost of a request without actually sending it to OpenAI. Turn on print_cost to print the cost of each request.
import openai
from openai_cost_tracker import query_openai
openai.api_key = "<YOUR_OPENAI_API_KEY_HERE>"
prompt = "Hello World!" # your prompt here
response = query_openai(
model="gpt-4-0125-preview", # support gpt-4-0125-preview, gpt-3.5-turbo-1106, gpt-4
messages=[{'role': 'user', 'content': prompt}],
max_tokens=5,
# rest of your OpenAI params here ...
simulation=True, # set to True to test the cost of a request without actually sending it to OpenAI
print_cost=True # set to True to print the cost of each request
)
print(response["choices"][0]["message"]["content"])
Output
Input tokens: 10 | Output tokens: 5 | Cost: $0.0003 | Total: $0.0003
Hello! How can I
Notes
- Under the simulation model, the cost of request is calculated based on given
max_tokensand number of input tokens using the OpenAI tokenization scheme. This is an approximation of the upper bond of the actual cost. - The code is mainly implemented for GPT-3.5 and GPT-4 models, but it will work for other openAI models by customizing the
utils.pyandestimator.pyfiles. Feel free to open an issue or PR if you need help with that. - The implementation is based on this repo.
Release files for openai-cost-tracker 0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| openai_cost_tracker-0.5.tar.gz | 4.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| openai_cost_tracker-0.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.8 kB
Release files / openai_cost_tracker-0.5.tar.gz
| Download URL | openai_cost_tracker-0.5.tar.gz |
|---|---|
| Size | 4.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6733c688826bb24b2e0fe9662490117fc85683c0f71e39ca182f9e3ccb06f3ef
|
|
BLAKE2b-256 checksum How to use checksums |
7d2565427a0552ecda1d7bbdf7cdd687ef048378dbb62ca0e5f2bbac0197784f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.11.5
|
Release files / openai_cost_tracker-0.5-py3-none-any.whl
| Download URL | openai_cost_tracker-0.5-py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ffde4ac39cf774ebeea7750b4e0697e0b2c59157976963e7ebf597d6c6580827
|
|
BLAKE2b-256 checksum How to use checksums |
eecf31437121ce1cf0e45aef61dff0e2051ec16475101adfbebe0112366f9067
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.11.5
|