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 llm-usage-tracker
# With optional dependencies:
pip install llm-usage-tracker[litellm] # For LiteLLM support
pip install llm-usage-tracker[gemini] # For Google Gemini support
pip install llm-usage-tracker[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(OPENAI_DEFAULT)
Usage with Sessions
from llm_usage_tracker import UsageSession, OPENAI_DEFAULT
with UsageSession(OPENAI_DEFAULT, label="my-session") as session:
# Your LLM calls here
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Analyze this data"}]
)
# Session results automatically saved
print(f"Session cost: ${session.result['grand_total_usd']:.4f}")
Advanced Features
Per-Agent Cost Tracking
from llm_usage_tracker import setup_patch, set_scope, print_usage_costs_by_scope, OPENAI_DEFAULT
setup_patch()
# Track costs per agent/component
set_scope("agent:researcher")
# ... LLM calls for research agent
set_scope("agent:writer")
# ... LLM calls for writer agent
# See breakdown by scope
print_usage_costs_by_scope(OPENAI_DEFAULT)
CrewAI Integration
from llm_usage_tracker.decorators import setup_agent_logging, setup_tool_logging
from llm_usage_tracker.trace_log import TraceLogger
# Automatic logging for CrewAI agents and tools
setup_agent_logging("Research Agent", tracer)(MyAgent)
setup_tool_logging("Web Search", tracer)(MyTool)
Use Cases
- Multi-Agent Systems: Track costs per agent in CrewAI/AutoGen workflows
- Production Monitoring: Monitor LLM costs in production applications
- Development: Understand token usage during development
- Cost Optimization: Identify expensive operations
- Billing/Reporting: Generate detailed usage reports
License
MIT
Project details
Release history Release notifications | RSS feed
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.1.tar.gz
(10.8 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llm_usage_tracker-0.1.1.tar.gz.
File metadata
- Download URL: llm_usage_tracker-0.1.1.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
31e664478a64f4490d261592adeb8b1bb198c6d293edac7397c199460523ef88
|
|
| MD5 |
5eadba488ff95613dcb3b72cc32c767c
|
|
| BLAKE2b-256 |
914310b10b61c222098e8684ca4c94df84d8d6d3e4a7c5c905186a2e7059b1d2
|
File details
Details for the file llm_usage_tracker-0.1.1-py3-none-any.whl.
File metadata
- Download URL: llm_usage_tracker-0.1.1-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ad3c57b363f5174bed3daffe17f022c66a5e27da3bded5651afcb468dc38dcb
|
|
| MD5 |
f096debb7aa142c6c85d7c3a81bbb7da
|
|
| BLAKE2b-256 |
adc5ecaee71e52a87afe25237ee9fad9bd2fc5c3d8412dcdd41476a2b44d69cf
|