Skip to main content

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

Project description

Supports OpenAI GPT, Google Gemini, Anthropic Claude and xAI Grok APIs

To install:

pip install gpt-token-tracker

Examples:

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)
from gpt_token_tracker.token_logger import TokenLogger
from gpt_token_tracker.writers.csv_writer import CSVWriter
from gpt_token_tracker.pricing_gemini import PricingGemini

csv_writer = CSVWriter("gemini_usage.csv")
costs = {
        "text_in": 0.10,
        "image_in": 0.10,
        "video_in": 0.10,
        "audio_in": 0.30,
        "cached_text_in": 0.01,
        "cached_video_in": 0.01,
        "cached_image_in": 0.01,
        "cached_audio_in": 0.03,
        "text_out": 0.40,
        "audio_out": 0.40,
        "thinking": 0.40
    }


csv_token_logger = TokenLogger(csv_writer, PricingGemini(costs))

MODEL_NAME = "gemini-2.5-flash-native-audio"

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

To run tests:

python -m pip install -r requirements.txt -r requirements_tests.txt --upgrade
python -m pytest tests

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.4.0.tar.gz (10.6 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.4.0-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: gpt_token_tracker-0.4.0.tar.gz
  • Upload date:
  • Size: 10.6 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.4.0.tar.gz
Algorithm Hash digest
SHA256 770409aa88ff4d84a9a510b9e0a5550343e1b546bdb46ee9bb7ae75c9dd30778
MD5 8e06d86bc3f5eac61471278e884e7afb
BLAKE2b-256 d04d0f6b30f990a3c8e8a9d3d1a88df381b869d9bc63aacf6b1e088e24fa1949

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gpt_token_tracker-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 01f9ba11e040942f9fc15025403fc8d8ad2e00144ce7f1f8fe0c505aa786e42d
MD5 faca0ad882d5f01a11b62515c39a42e5
BLAKE2b-256 43cbf29e18e64e6fa23cc44b638474d7887023b6f245ceb2f574a291fa1a66ce

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