Skip to main content

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 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openai_request_tracker-0.1.0.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openai_request_tracker-0.1.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file openai_request_tracker-0.1.0.tar.gz.

File metadata

  • Download URL: openai_request_tracker-0.1.0.tar.gz
  • Upload date:
  • Size: 8.1 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

Hashes for openai_request_tracker-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5a8973cd972b73ddba6c52bc1a9e083300f1d4e2c15a7e1ddbd43809962ba277
MD5 04a5f50e4f4d5bd76cb600c199efcee9
BLAKE2b-256 845d587cf9b5f5b1ea6faaa08834b013043a41fd1dd055ced9cb2e0345bc3101

See more details on using hashes here.

File details

Details for the file openai_request_tracker-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for openai_request_tracker-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9ea292a28dc9c7db46ec00be332d285bd09e1d6f5bab460508ba0bbf3b606d19
MD5 67f96ab81540a2dd6206305bbd796472
BLAKE2b-256 957a8e4594b8a65b9ce095019250d1364c6a9998e8601555a1a53ad1507ef2cf

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