OpenAI cost tracker that logs OpenAI requests using opentelemetry
Project description
openai-cost-tracker
Cost tracker that logs OpenAI requests using opentelemetry. Once enabled, it will log every request that is being made via the openai python client to a file in the execution directory. The log file is called "traces_<\datetime>.log".
Installation
Install from PYPI
pip install openai-request-tracker
Please note the difference between pypi package name and github repository name.
Install from source
Install with poetry:
poetry install
or install with pip:
pip install .
Usage
Record requests
CLI wrapper
The command track-costs is a wrapper that initializes the tracker and then runs the given CLI command.
track-costs <your usual command>
For example, if your are running usually an indexing run with microsoft/graphRAG with
python -m graphrag.index --root foo
you only need to change it to
track-costs graphrag.index --root foo
to record all openai requests in a log file.
Similarly, for calling the graphrag query module you can do
track-costs graphrag.query --root foo --method local "My query"
In code usage
You can just add a call to cost_tracker.init_tracker() at the very beginning of your script:
from cost_tracker import init_tracker
init_tracker()
... # your script
For example, to make it work for the indexing phase of microsoft/graphRAG, modify the file main.py such that it looks like:
# Copyright (c) 2024 Microsoft Corporation.
# Licensed under the MIT License
"""The Indexing Engine package root."""
from cost_tracker import init_tracker
init_tracker()
import argparse
from graphrag.logging import ReporterType
from graphrag.utils.cli import dir_exist, file_exist
from .cli import index_cli
from .emit.types import TableEmitterType
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="python -m graphrag.index",
description="The graphrag indexing engine",
)
... # rest of the unmodified script
Run your script as before.
Show costs
The script "display-costs" can be used to show the openai costs for all requests recorded in a given log file:
display-costs --file <YOUR_LOG_FILE>
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
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_request_tracker-0.1.1.tar.gz.
File metadata
- Download URL: openai_request_tracker-0.1.1.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.11.10 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7d0cdb5ec45593e007b0cc0b57a8f91829b454eb58d6ecd90061c666eb7ffb0b
|
|
| MD5 |
00d9007e1f049a0ac1ed9d8b6895fc1b
|
|
| BLAKE2b-256 |
b44b894152ca34d10c18ed944e2b0c021881c12318eb05aa8f557f84663c6963
|
File details
Details for the file openai_request_tracker-0.1.1-py3-none-any.whl.
File metadata
- Download URL: openai_request_tracker-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.11.10 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12055108436039f0a7a3302da9299c6dac4392ef573cc3deb4f12ae89c597aa7
|
|
| MD5 |
a49d8e2eb8d7c950176892e413c3c55f
|
|
| BLAKE2b-256 |
6c7b30004b8421df1187c83230673d0a32691717a48dac437f1f7c15829d4746
|