# 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
```bash
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
```python
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
```python
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
```python
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
```python
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
Raw data
{
"_id": null,
"home_page": null,
"name": "llm-usage-tracker",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "llm, openai, token-tracking, cost-tracking, litellm, gemini",
"author": null,
"author_email": "Gobi <gobivellingiri@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/91/43/10b10b61c222098e8684ca4c94df84d8d6d3e4a7c5c905186a2e7059b1d2/llm_usage_tracker-0.1.1.tar.gz",
"platform": null,
"description": "# llm-usage-tracker\n\nA 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.\n\n## Features\n\n- **Zero-config tracking**: Just import and start tracking\n- **Multi-provider support**: OpenAI, LiteLLM, Google Gemini\n- **Cost calculation**: Automatic cost computation based on current pricing\n- **Session management**: Track usage across different sessions\n- **Caching awareness**: Handles cached responses appropriately\n\n## Supported Libraries\n\n- OpenAI v1 (client + module-level)\n- OpenAI v0 (legacy `ChatCompletion.create`)\n- LiteLLM (optional)\n- Google `google-generativeai` (optional)\n\n## Installation\n\n```bash\npip install llm-usage-tracker\n\n# With optional dependencies:\npip install llm-usage-tracker[litellm] # For LiteLLM support\npip install llm-usage-tracker[gemini] # For Google Gemini support\npip install llm-usage-tracker[all] # All optional dependencies\n```\n\n## Quick Start\n\n```python\nfrom llm_usage_tracker import setup_patch, print_usage_costs\n\n# Enable tracking\nsetup_patch()\n\n# Your existing OpenAI/LiteLLM/Gemini code works unchanged\nimport openai\nclient = openai.OpenAI()\nresponse = client.chat.completions.create(\n model=\"gpt-3.5-turbo\",\n messages=[{\"role\": \"user\", \"content\": \"Hello!\"}]\n)\n\n# Print usage summary\nprint_usage_costs(OPENAI_DEFAULT)\n```\n\n## Usage with Sessions\n\n```python\nfrom llm_usage_tracker import UsageSession, OPENAI_DEFAULT\n\nwith UsageSession(OPENAI_DEFAULT, label=\"my-session\") as session:\n # Your LLM calls here\n response = client.chat.completions.create(\n model=\"gpt-4o-mini\",\n messages=[{\"role\": \"user\", \"content\": \"Analyze this data\"}]\n )\n\n# Session results automatically saved\nprint(f\"Session cost: ${session.result['grand_total_usd']:.4f}\")\n```\n\n## Advanced Features\n\n### Per-Agent Cost Tracking\n```python\nfrom llm_usage_tracker import setup_patch, set_scope, print_usage_costs_by_scope, OPENAI_DEFAULT\n\nsetup_patch()\n\n# Track costs per agent/component\nset_scope(\"agent:researcher\")\n# ... LLM calls for research agent\n\nset_scope(\"agent:writer\")\n# ... LLM calls for writer agent\n\n# See breakdown by scope\nprint_usage_costs_by_scope(OPENAI_DEFAULT)\n```\n\n### CrewAI Integration\n```python\nfrom llm_usage_tracker.decorators import setup_agent_logging, setup_tool_logging\nfrom llm_usage_tracker.trace_log import TraceLogger\n\n# Automatic logging for CrewAI agents and tools\nsetup_agent_logging(\"Research Agent\", tracer)(MyAgent)\nsetup_tool_logging(\"Web Search\", tracer)(MyTool)\n```\n\n## Use Cases\n\n- **Multi-Agent Systems**: Track costs per agent in CrewAI/AutoGen workflows\n- **Production Monitoring**: Monitor LLM costs in production applications\n- **Development**: Understand token usage during development\n- **Cost Optimization**: Identify expensive operations\n- **Billing/Reporting**: Generate detailed usage reports\n\n## License\n\nMIT\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Drop-in token + cost tracker for OpenAI / LiteLLM / Gemini with caching awareness",
"version": "0.1.1",
"project_urls": null,
"split_keywords": [
"llm",
" openai",
" token-tracking",
" cost-tracking",
" litellm",
" gemini"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "adc5ecaee71e52a87afe25237ee9fad9bd2fc5c3d8412dcdd41476a2b44d69cf",
"md5": "f096debb7aa142c6c85d7c3a81bbb7da",
"sha256": "9ad3c57b363f5174bed3daffe17f022c66a5e27da3bded5651afcb468dc38dcb"
},
"downloads": -1,
"filename": "llm_usage_tracker-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f096debb7aa142c6c85d7c3a81bbb7da",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 11294,
"upload_time": "2025-09-05T17:45:11",
"upload_time_iso_8601": "2025-09-05T17:45:11.630892Z",
"url": "https://files.pythonhosted.org/packages/ad/c5/ecaee71e52a87afe25237ee9fad9bd2fc5c3d8412dcdd41476a2b44d69cf/llm_usage_tracker-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "914310b10b61c222098e8684ca4c94df84d8d6d3e4a7c5c905186a2e7059b1d2",
"md5": "5eadba488ff95613dcb3b72cc32c767c",
"sha256": "31e664478a64f4490d261592adeb8b1bb198c6d293edac7397c199460523ef88"
},
"downloads": -1,
"filename": "llm_usage_tracker-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "5eadba488ff95613dcb3b72cc32c767c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 10819,
"upload_time": "2025-09-05T17:45:13",
"upload_time_iso_8601": "2025-09-05T17:45:13.334369Z",
"url": "https://files.pythonhosted.org/packages/91/43/10b10b61c222098e8684ca4c94df84d8d6d3e4a7c5c905186a2e7059b1d2/llm_usage_tracker-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-09-05 17:45:13",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "llm-usage-tracker"
}