Skip to main content

Token usage and cost tracking for OpenAI responses (another AI providers in future)

Project description

Install dependencies:

python -m pip install -r requirements.txt --upgrade

To run tests:

python -m pip install -r requirements_tests.txt --upgrade
from gpt_token_tracker.token_logger import TokenLogger
from gpt_token_tracker.writers.log_writer import LogWriter
from gpt_token_tracker.pricing import PricingRealtime

log_writer = LogWriter("realtime_tokens")   ### Name of logger
realtime_costs = {
        "text_in": 0.60,
        "cached_text_in": 0.06,
        "text_out": 2.40,
        "audio_in": 10.00,
        "audio_out": 20.00,
        "image_in": 5.00,
        "cached_image_in": 0.50,
        "cached_audio_in": 0.40,
    }
token_logger = TokenLogger(log_writer, PricingRealtime(realtime_costs))

token_logger_realtime.record(realtime_model, realtime_result, fake_realtime_usage)

MODEL_NAME = "gpt-realtime-mini"
response = ...
realtime_result = "This is a fake realtime result"
usage = response.usage
token_logger.record(MODEL_NAME, realtime_result, usage)
from gpt_token_tracker.token_logger import TokenLogger
from gpt_token_tracker.writers.csv_writer import CSVWriter
from gpt_token_tracker.pricing import PricingTextCompletion

csv_writer = CSVWriter("completion_usage.csv")
text_completion_costs = {
        "text_in": 0.25,
        "cached_text_in": 0.025,
        "text_out": 2.00,
    }
csv_token_logger = TokenLogger(csv_writer, PricingTextCompletion(text_completion_costs))

MODEL_NAME = "gpt-5-mini"

response = ...
completion_result = "This is a fake completion result"
usage = response.usage
csv_token_logger.record(MODEL_NAME, completion_result, usage)

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

gpt_token_tracker-0.1.1.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

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

gpt_token_tracker-0.1.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file gpt_token_tracker-0.1.1.tar.gz.

File metadata

  • Download URL: gpt_token_tracker-0.1.1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for gpt_token_tracker-0.1.1.tar.gz
Algorithm Hash digest
SHA256 02785625968c521fe93faa06a58af436174f0606699677dd718f5a5a0bf8873b
MD5 d17c2f5f172dce2c1e86aa94bbae4e34
BLAKE2b-256 2e7b466fdaba2500f6c19403bcd63df14f93c4c6c02b1846f36fafe054ac42a8

See more details on using hashes here.

File details

Details for the file gpt_token_tracker-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for gpt_token_tracker-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d6904fbd3624ecdcecfd5fa32b5a12c3cb3f2822605c85463a5bab8c5359ff5a
MD5 4fb6fedcb5682df810155d99642a56d4
BLAKE2b-256 0980649955b2e07500f8b735915c1acb235f0dbd38a8d1adfd5c3ce9670656ce

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