Skip to main content

Drop-in token + cost tracker for OpenAI / LiteLLM / Gemini with caching awareness

Project description

llm-usage-tracker

A drop-in token and cost tracker for popular LLM libraries with caching awareness. Automatically tracks usage across multiple LLM providers by monkey-patching their API calls.

Features

  • Zero-config tracking: Just import and start tracking
  • Multi-provider support: OpenAI, LiteLLM, Google Gemini
  • Cost calculation: Automatic cost computation based on current pricing
  • Session management: Track usage across different sessions
  • Caching awareness: Handles cached responses appropriately

Supported Libraries

  • OpenAI v1 (client + module-level)
  • OpenAI v0 (legacy ChatCompletion.create)
  • LiteLLM (optional)
  • Google google-generativeai (optional)

Installation

pip install -e .

# With optional dependencies:
pip install -e .[litellm]  # For LiteLLM support
pip install -e .[gemini]   # For Google Gemini support
pip install -e .[all]      # All optional dependencies

Quick Start

from llm_usage_tracker import setup_patch, print_usage_costs

# Enable tracking
setup_patch()

# Your existing OpenAI/LiteLLM/Gemini code works unchanged
import openai
client = openai.OpenAI()
response = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": "Hello!"}]
)

# Print usage summary
print_usage_costs()

Usage with Sessions

from llm_usage_tracker import UsageSession

with UsageSession("my-session") as session:
    # Your LLM calls here
    pass

# Get session costs
costs = session.compute_costs()
print(f"Total cost: ${costs['total_cost']:.4f}")

License

MIT

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

llm_usage_tracker-0.1.0.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

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

llm_usage_tracker-0.1.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: llm_usage_tracker-0.1.0.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for llm_usage_tracker-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ccf46f2393cbae4c8f3bd3a3e5c7b2a361131032cc87ae872c880487c0bac10f
MD5 6048ef5f2519e01b7e77fcb839264cc4
BLAKE2b-256 f9a3adf834c8775133c702161f7f4717cef4a9d4bcbe025d207da98136da3101

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for llm_usage_tracker-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d6b1b081b0970b4a6043576012449f72fec7e7bf6379a5e0a7420fe57433a518
MD5 4be650270d46a3212447a92e1aee64e9
BLAKE2b-256 1f3d2151e44c316cc4c09957090f257b370dde5f18a41ea1ec53739cbf79673b

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