Skip to main content

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

Metadata

Release files for llm-usage-tracker 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llm-usage-tracker 0.1.1
File Size Uploaded
llm_usage_tracker-0.1.1.tar.gz 10.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-usage-tracker 0.1.1
File Interpreter ABI Platform
llm_usage_tracker-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 22.1 kB

Release files / llm_usage_tracker-0.1.1.tar.gz

Download URL llm_usage_tracker-0.1.1.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
31e664478a64f4490d261592adeb8b1bb198c6d293edac7397c199460523ef88
BLAKE2b-256 checksum
How to use checksums
914310b10b61c222098e8684ca4c94df84d8d6d3e4a7c5c905186a2e7059b1d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / llm_usage_tracker-0.1.1-py3-none-any.whl

Download URL llm_usage_tracker-0.1.1-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9ad3c57b363f5174bed3daffe17f022c66a5e27da3bded5651afcb468dc38dcb
BLAKE2b-256 checksum
How to use checksums
adc5ecaee71e52a87afe25237ee9fad9bd2fc5c3d8412dcdd41476a2b44d69cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page