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>
Metadata
Release files for openai-request-tracker 0.1.1
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_request_tracker-0.1.1.tar.gz | 8.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| openai_request_tracker-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.3 kB
Release files / openai_request_tracker-0.1.1.tar.gz
| Download URL | openai_request_tracker-0.1.1.tar.gz |
|---|---|
| Size | 8.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7d0cdb5ec45593e007b0cc0b57a8f91829b454eb58d6ecd90061c666eb7ffb0b
|
|
BLAKE2b-256 checksum How to use checksums |
b44b894152ca34d10c18ed944e2b0c021881c12318eb05aa8f557f84663c6963
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.11.10 Linux/6.5.0-1025-azure
|
Release files / openai_request_tracker-0.1.1-py3-none-any.whl
| Download URL | openai_request_tracker-0.1.1-py3-none-any.whl |
|---|---|
| Size | 9.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
12055108436039f0a7a3302da9299c6dac4392ef573cc3deb4f12ae89c597aa7
|
|
BLAKE2b-256 checksum How to use checksums |
6c7b30004b8421df1187c83230673d0a32691717a48dac437f1f7c15829d4746
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.11.10 Linux/6.5.0-1025-azure
|