llmcosts


Namellmcosts JSON
Version 0.2.5 PyPI version JSON
download
home_pageNone
SummaryA generic, provider-agnostic SDK wrapper that intercepts LLM API responses and extracts usage information for cost tracking with structured payload formatting.
upload_time2025-07-08 19:40:18
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseNone
keywords llm ai cost-tracking openai anthropic gemini bedrock usage-monitoring
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # LLMCosts

[![PyPI version](https://img.shields.io/pypi/v/llmcosts.svg)](https://pypi.org/project/llmcosts/)
[![Python Support](https://img.shields.io/pypi/pyversions/llmcosts.svg)](https://pypi.org/project/llmcosts/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

**LLMCosts** is a comprehensive LLM cost tracking and management platform that helps developers and agencies monitor, analyze, and optimize their AI spending across all major providers. **[Sign up for a free account at llmcosts.com](https://llmcosts.com)** to access real-time analytics, budget alerts, client billing tools, and accounting integrations.

A universal Python wrapper that intercepts LLM API responses and extracts usage information for comprehensive cost tracking. Works as a drop-in replacement for your existing LLM clients with zero code changes to your API calls.

**๐Ÿ”’ Privacy-First**: LLMCosts NEVER sees your API keys, requests, or responses. We only extract usage data (tokens, costs, model info) from responses. Unlike other frameworks that capture everything, we prioritize your privacy and security above all else.

**๐Ÿ”„ Universal Compatibility**: One tracking proxy works with ANY LLM provider's SDK. No need for different wrappers per provider - the same `LLMTrackingProxy` works with OpenAI, Anthropic, Google, AWS Bedrock, and any other provider.

**๐ŸŽฏ Supports**: OpenAI (any OpenAI-compatible APIs -- DeepSeek, Grok, etc.), Anthropic, Google Gemini, AWS Bedrock, and LangChain.

## ๐Ÿš€ Quick Start

But wait! Just a quick note: 

> **๐Ÿ”‘ CRITICAL: API Key Required**
>
> Before using LLMCosts, you **MUST** have an LLMCosts API key. **[Sign up for a free account at llmcosts.com](https://llmcosts.com)** to get your API key.
>
> **Without an API key, none of the LLMCosts tracking will work!**

### Installation

```bash
# Core library only (minimal dependencies)
pip install llmcosts

# To install llmcosts along with the SDKs from specific 
# providers (quote for zsh compatibility)  (for testing, etc)
pip install "llmcosts[openai]"      # OpenAI + compatible APIs (DeepSeek, Grok, etc.)
pip install "llmcosts[anthropic]"   # Anthropic Claude
pip install "llmcosts[google]"      # Google Gemini
pip install "llmcosts[bedrock]"     # AWS Bedrock
pip install "llmcosts[langchain]"   # LangChain integration

# All providers at once
pip install "llmcosts[all]"

# Using uv (recommended) - no quotes needed
uv add llmcosts                   # Core only
uv add llmcosts[openai]           # With OpenAI
uv add llmcosts[langchain]        # With LangChain
uv add llmcosts[all]              # All providers
```

### Basic Usage

```python
import os
from llmcosts import LLMTrackingProxy, Provider
import openai

# Create OpenAI client
client = openai.OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))

# Wrap with LLMCosts tracking
tracked_client = LLMTrackingProxy(
    client, 
    provider=Provider.OPENAI,  # REQUIRED: Specifies the LLM service
    api_key=os.environ.get("LLMCOSTS_API_KEY"),  # Your LLMCosts API key
    debug=True
)

# Use exactly as before - zero changes to your API calls
response = tracked_client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello!"}]
)

# Usage automatically logged as structured JSON
# ๐Ÿ”’ Privacy: Only usage metadata is extracted - never your API keys, requests, or responses
```

### Environment Setup

LLMCosts only needs its own API key to function. You can pass it in as a parameter or LLMTrackingProxy can get it from an environment variable.

```bash
# Set your LLMCosts API key (get this from llmcosts.com)
export LLMCOSTS_API_KEY="your-llmcosts-api-key-here"
```

**๐Ÿ”’ Important**: Your existing LLM provider API keys (OpenAI, Anthropic, etc.) remain yours and are never shared with LLMCosts. You continue to use them exactly as before - LLMCosts only extracts usage metadata from responses.

> **๐Ÿ’ก Recommended Pattern**: Always create `LLMTrackingProxy` directly - it handles global tracker creation, API key management, and background processing automatically.

## ๐Ÿ“‹ Key Features

- **๐Ÿ”„ Universal Compatibility**: One proxy works with ANY LLM provider's SDK - OpenAI, Anthropic, Google, AWS, and more
- **๐Ÿ”’ Privacy-First Design**: NEVER sees API keys, requests, or responses - only usage data (tokens, costs, model info)
- **๐Ÿข Client-Level Tracking**: Seamlessly track costs per customer, user, project, or department with rich context data
- **๐Ÿ“Š Automatic Usage Tracking**: Captures tokens, costs, model info, and timestamps from response metadata
- **๐Ÿท๏ธ Rich Context Data**: Add any metadata - project names, user IDs, billing info, session data, or custom tags
- **๐Ÿ’ฐ Multi-Tenant Ready**: Perfect for agencies, SaaS platforms, and enterprise cost allocation
- **๐ŸŽ›๏ธ Dynamic Configuration**: Change settings on-the-fly without restarting
- **๐Ÿ’พ Smart Delivery**: Resilient background delivery with retry logic
- **๐Ÿ”” Response Callbacks**: Built-in SQLite/text file callbacks plus custom handlers
- **๐Ÿ” Debug Mode**: Synchronous operation for testing and debugging
- **๐Ÿ“ค Structured Output**: Clean JSON format for easy parsing
- **โ™ป๏ธ Auto-Recovery**: Automatically restarts failed delivery threads
- **๐Ÿšซ Non-Intrusive**: Original API responses remain completely unchanged

## ๐ŸŽฏ Supported Providers

| Provider | Provider Enum | Framework | Installation |
|----------|---------------|-----------|-------------|
| **OpenAI** | `Provider.OPENAI` | `None` (default) | `pip install "llmcosts[openai]"` |
| **Anthropic** | `Provider.ANTHROPIC` | `None` (default) | `pip install "llmcosts[anthropic]"` |
| **Google Gemini** | `Provider.GOOGLE` | `None` (default) | `pip install "llmcosts[google]"` |
| **AWS Bedrock** | `Provider.AMAZON_BEDROCK` | `None` (default) | `pip install "llmcosts[bedrock]"` |
| **DeepSeek** | `Provider.DEEPSEEK` | `None` (default) | `pip install "llmcosts[openai]"` |
| **Grok/xAI** | `Provider.XAI` | `None` (default) | `pip install "llmcosts[openai]"` |
| **LangChain + OpenAI** | `Provider.OPENAI` | `Framework.LANGCHAIN` | `pip install "llmcosts[langchain]"` |

## ๐Ÿ“– Documentation

### Core Guides

- **[๐Ÿข Client Tracking & Context Data](docs/client-tracking.md)** - **โญ ESSENTIAL** - Track costs per client, user, project with rich context data
- **[๐Ÿ”ง Configuration](docs/configuration.md)** - All configuration options, environment variables, and advanced settings
- **[๐ŸŽฏ Providers](docs/providers.md)** - Provider-specific integration guides with examples
- **[๐Ÿ”— LangChain Integration](docs/langchain.md)** - Complete LangChain integration guide
- **[๐Ÿ’ฐ Pricing & Models](docs/pricing.md)** - Model discovery, pricing info, and cost calculation
- **[๐Ÿ› ๏ธ SDK Helper Functions](docs/sdk-functions.md)** - Complete reference for all SDK helper functions
- **[๐Ÿ” Troubleshooting](docs/troubleshooting.md)** - Common issues and solutions
- **[๐Ÿงช Testing](docs/testing.md)** - Comprehensive testing documentation
- **[๐Ÿš€ Deployment](docs/deployment.md)** - Deployment guide for PyPI and version management

### Quick Links

- **Getting Started**: See [Basic Usage](#basic-usage) above
- **๐Ÿข Multi-Tenant Apps**: [Client Tracking Guide](docs/client-tracking.md) - Perfect for agencies & SaaS
- **Provider Setup**: [Providers Guide](docs/providers.md)
- **LangChain Users**: [LangChain Integration](docs/langchain.md)
- **Advanced Config**: [Configuration Guide](docs/configuration.md)
- **Having Issues?**: [Troubleshooting Guide](docs/troubleshooting.md)

## ๐Ÿ’ป Quick Examples

### OpenAI

```python
from llmcosts import LLMTrackingProxy, Provider
import openai

client = openai.OpenAI(api_key="your-key")
tracked_client = LLMTrackingProxy(client, provider=Provider.OPENAI)

response = tracked_client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello!"}]
)
```

### Anthropic

```python
from llmcosts import LLMTrackingProxy, Provider
import anthropic

client = anthropic.Anthropic(api_key="your-key")
tracked_client = LLMTrackingProxy(client, provider=Provider.ANTHROPIC)

response = tracked_client.messages.create(
    model="claude-3-haiku-20240307",
    max_tokens=1000,
    messages=[{"role": "user", "content": "Hello!"}]
)
```

### LangChain

```python
from llmcosts import LLMTrackingProxy, Provider, Framework
from langchain_openai import ChatOpenAI
import openai

# Key difference: specify framework=Framework.LANGCHAIN
openai_client = openai.OpenAI(api_key="your-key")
tracked_client = LLMTrackingProxy(
    openai_client,
    provider=Provider.OPENAI,
    framework=Framework.LANGCHAIN  # Enable LangChain integration
)

chat_model = ChatOpenAI(client=tracked_client.chat.completions)
response = chat_model.invoke([{"role": "user", "content": "Hello!"}])
```

### Client Tracking (Multi-Tenant)

```python
from llmcosts import LLMTrackingProxy, Provider
import openai

client = openai.OpenAI(api_key="your-key")
tracked_client = LLMTrackingProxy(
    client,
    provider=Provider.OPENAI,
    client_customer_key="customer_acme_corp",  # Track costs per customer
    context={
        "user_id": "user_123",
        "project": "chatbot_v2", 
        "department": "support",
        "environment": "production"
    }
)

response = tracked_client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello!"}]
)
# โ†’ Automatically tracked with customer and context data for billing/analytics
```

> **See [Provider Integration Guide](docs/providers.md) for complete examples of all supported providers.**

## ๐Ÿ” Model Discovery & Pricing

```python
from llmcosts import list_models, get_model_pricing, calculate_cost_from_tokens, Provider

# Discover available models
models = list_models()
print(f"Total models: {len(models)}")

# Get pricing information
pricing = get_model_pricing(Provider.OPENAI, "gpt-4o-mini")
print(f"Input: ${pricing['costs'][0]['cost_per_million']}/M tokens")

# Calculate costs
cost = calculate_cost_from_tokens(
    Provider.OPENAI, "gpt-4o-mini", 
    input_tokens=1000, output_tokens=500
)
print(f"Total cost: ${cost['costs']['total_cost']}")
```

> **See [Pricing & Models Guide](docs/pricing.md) for complete model discovery and cost calculation features.**

## ๐Ÿ› ๏ธ Development

### Setup

```bash
# Clone repository
git clone https://github.com/llmcosts/llmcosts-python.git
cd llmcosts-python

# Using uv (recommended)
uv sync --extra dev

# Using pip
pip install -e ".[dev]"
```

### Testing

```bash
# Quick test (requires your own OpenAI API key)
uv run python tests/check.py openai gpt-4o-mini

# Full test suite
uv run pytest

# With coverage
uv run pytest --cov=llmcosts --cov-report=html
```

**๐Ÿ”’ Testing Privacy**: Tests use YOUR provider API keys locally to verify functionality. These keys never leave your machine - they're only used for local testing.

> **See [Testing Guide](docs/testing.md) for comprehensive testing documentation including how to set up provider API keys for local testing.**

## ๐Ÿค Contributing

1. Fork the repository
2. Create a feature branch: `git checkout -b feature-name`
3. Make changes and add tests
4. Run the test suite: `uv run pytest`
5. Ensure code quality: `uv run black llmcosts/ tests/` and `uv run isort llmcosts/ tests/`
6. Submit a pull request

## ๐Ÿ“ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## ๐Ÿ”— Links

- **๐ŸŒ Website**: [llmcosts.com](https://llmcosts.com)
- **๐Ÿ“ฆ PyPI**: [pypi.org/project/llmcosts](https://pypi.org/project/llmcosts/)
- **๐Ÿ™ GitHub**: [github.com/llmcosts/llmcosts-python](https://github.com/llmcosts/llmcosts-python)
- **๐Ÿ› Issues**: [github.com/llmcosts/llmcosts-python/issues](https://github.com/llmcosts/llmcosts-python/issues)
- **๐Ÿ“ง Support**: [help@llmcosts.com](mailto:help@llmcosts.com)

## ๐Ÿ“ˆ Changelog

### v0.2.2 (Current)
- New SDK helper functions with comprehensive documentation
- Enhanced events management with filtering, search, and export
- Added `list_threshold_events()` function for threshold event tracking
- Improved network resilience in tests with retry logic
- **Breaking changes**: Removed `limits.py`, `customers.py`, and `alerts.py` modules
- Migration guide available in [CHANGELOG.md](CHANGELOG.md)

### v0.1.0
- Universal LLM provider support
- Dynamic configuration with property setters
- Context tracking for user/session data
- Response callbacks for custom processing
- Synchronous mode for testing
- Resilient background delivery
- Comprehensive test coverage
- Thread-safe global tracker management

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "llmcosts",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "Keyton Weissinger <keyton@llmcosts.com>",
    "keywords": "llm, ai, cost-tracking, openai, anthropic, gemini, bedrock, usage-monitoring",
    "author": null,
    "author_email": "Keyton Weissinger <keyton@llmcosts.com>",
    "download_url": "https://files.pythonhosted.org/packages/95/43/39c678d3f3c3ff05f1767ca413dc584a80d58df06aa27e2a07dafb5e255d/llmcosts-0.2.5.tar.gz",
    "platform": null,
    "description": "# LLMCosts\n\n[![PyPI version](https://img.shields.io/pypi/v/llmcosts.svg)](https://pypi.org/project/llmcosts/)\n[![Python Support](https://img.shields.io/pypi/pyversions/llmcosts.svg)](https://pypi.org/project/llmcosts/)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\n**LLMCosts** is a comprehensive LLM cost tracking and management platform that helps developers and agencies monitor, analyze, and optimize their AI spending across all major providers. **[Sign up for a free account at llmcosts.com](https://llmcosts.com)** to access real-time analytics, budget alerts, client billing tools, and accounting integrations.\n\nA universal Python wrapper that intercepts LLM API responses and extracts usage information for comprehensive cost tracking. Works as a drop-in replacement for your existing LLM clients with zero code changes to your API calls.\n\n**\ud83d\udd12 Privacy-First**: LLMCosts NEVER sees your API keys, requests, or responses. We only extract usage data (tokens, costs, model info) from responses. Unlike other frameworks that capture everything, we prioritize your privacy and security above all else.\n\n**\ud83d\udd04 Universal Compatibility**: One tracking proxy works with ANY LLM provider's SDK. No need for different wrappers per provider - the same `LLMTrackingProxy` works with OpenAI, Anthropic, Google, AWS Bedrock, and any other provider.\n\n**\ud83c\udfaf Supports**: OpenAI (any OpenAI-compatible APIs -- DeepSeek, Grok, etc.), Anthropic, Google Gemini, AWS Bedrock, and LangChain.\n\n## \ud83d\ude80 Quick Start\n\nBut wait! Just a quick note: \n\n> **\ud83d\udd11 CRITICAL: API Key Required**\n>\n> Before using LLMCosts, you **MUST** have an LLMCosts API key. **[Sign up for a free account at llmcosts.com](https://llmcosts.com)** to get your API key.\n>\n> **Without an API key, none of the LLMCosts tracking will work!**\n\n### Installation\n\n```bash\n# Core library only (minimal dependencies)\npip install llmcosts\n\n# To install llmcosts along with the SDKs from specific \n# providers (quote for zsh compatibility)  (for testing, etc)\npip install \"llmcosts[openai]\"      # OpenAI + compatible APIs (DeepSeek, Grok, etc.)\npip install \"llmcosts[anthropic]\"   # Anthropic Claude\npip install \"llmcosts[google]\"      # Google Gemini\npip install \"llmcosts[bedrock]\"     # AWS Bedrock\npip install \"llmcosts[langchain]\"   # LangChain integration\n\n# All providers at once\npip install \"llmcosts[all]\"\n\n# Using uv (recommended) - no quotes needed\nuv add llmcosts                   # Core only\nuv add llmcosts[openai]           # With OpenAI\nuv add llmcosts[langchain]        # With LangChain\nuv add llmcosts[all]              # All providers\n```\n\n### Basic Usage\n\n```python\nimport os\nfrom llmcosts import LLMTrackingProxy, Provider\nimport openai\n\n# Create OpenAI client\nclient = openai.OpenAI(api_key=os.environ.get(\"OPENAI_API_KEY\"))\n\n# Wrap with LLMCosts tracking\ntracked_client = LLMTrackingProxy(\n    client, \n    provider=Provider.OPENAI,  # REQUIRED: Specifies the LLM service\n    api_key=os.environ.get(\"LLMCOSTS_API_KEY\"),  # Your LLMCosts API key\n    debug=True\n)\n\n# Use exactly as before - zero changes to your API calls\nresponse = tracked_client.chat.completions.create(\n    model=\"gpt-4o-mini\",\n    messages=[{\"role\": \"user\", \"content\": \"Hello!\"}]\n)\n\n# Usage automatically logged as structured JSON\n# \ud83d\udd12 Privacy: Only usage metadata is extracted - never your API keys, requests, or responses\n```\n\n### Environment Setup\n\nLLMCosts only needs its own API key to function. You can pass it in as a parameter or LLMTrackingProxy can get it from an environment variable.\n\n```bash\n# Set your LLMCosts API key (get this from llmcosts.com)\nexport LLMCOSTS_API_KEY=\"your-llmcosts-api-key-here\"\n```\n\n**\ud83d\udd12 Important**: Your existing LLM provider API keys (OpenAI, Anthropic, etc.) remain yours and are never shared with LLMCosts. You continue to use them exactly as before - LLMCosts only extracts usage metadata from responses.\n\n> **\ud83d\udca1 Recommended Pattern**: Always create `LLMTrackingProxy` directly - it handles global tracker creation, API key management, and background processing automatically.\n\n## \ud83d\udccb Key Features\n\n- **\ud83d\udd04 Universal Compatibility**: One proxy works with ANY LLM provider's SDK - OpenAI, Anthropic, Google, AWS, and more\n- **\ud83d\udd12 Privacy-First Design**: NEVER sees API keys, requests, or responses - only usage data (tokens, costs, model info)\n- **\ud83c\udfe2 Client-Level Tracking**: Seamlessly track costs per customer, user, project, or department with rich context data\n- **\ud83d\udcca Automatic Usage Tracking**: Captures tokens, costs, model info, and timestamps from response metadata\n- **\ud83c\udff7\ufe0f Rich Context Data**: Add any metadata - project names, user IDs, billing info, session data, or custom tags\n- **\ud83d\udcb0 Multi-Tenant Ready**: Perfect for agencies, SaaS platforms, and enterprise cost allocation\n- **\ud83c\udf9b\ufe0f Dynamic Configuration**: Change settings on-the-fly without restarting\n- **\ud83d\udcbe Smart Delivery**: Resilient background delivery with retry logic\n- **\ud83d\udd14 Response Callbacks**: Built-in SQLite/text file callbacks plus custom handlers\n- **\ud83d\udd0d Debug Mode**: Synchronous operation for testing and debugging\n- **\ud83d\udce4 Structured Output**: Clean JSON format for easy parsing\n- **\u267b\ufe0f Auto-Recovery**: Automatically restarts failed delivery threads\n- **\ud83d\udeab Non-Intrusive**: Original API responses remain completely unchanged\n\n## \ud83c\udfaf Supported Providers\n\n| Provider | Provider Enum | Framework | Installation |\n|----------|---------------|-----------|-------------|\n| **OpenAI** | `Provider.OPENAI` | `None` (default) | `pip install \"llmcosts[openai]\"` |\n| **Anthropic** | `Provider.ANTHROPIC` | `None` (default) | `pip install \"llmcosts[anthropic]\"` |\n| **Google Gemini** | `Provider.GOOGLE` | `None` (default) | `pip install \"llmcosts[google]\"` |\n| **AWS Bedrock** | `Provider.AMAZON_BEDROCK` | `None` (default) | `pip install \"llmcosts[bedrock]\"` |\n| **DeepSeek** | `Provider.DEEPSEEK` | `None` (default) | `pip install \"llmcosts[openai]\"` |\n| **Grok/xAI** | `Provider.XAI` | `None` (default) | `pip install \"llmcosts[openai]\"` |\n| **LangChain + OpenAI** | `Provider.OPENAI` | `Framework.LANGCHAIN` | `pip install \"llmcosts[langchain]\"` |\n\n## \ud83d\udcd6 Documentation\n\n### Core Guides\n\n- **[\ud83c\udfe2 Client Tracking & Context Data](docs/client-tracking.md)** - **\u2b50 ESSENTIAL** - Track costs per client, user, project with rich context data\n- **[\ud83d\udd27 Configuration](docs/configuration.md)** - All configuration options, environment variables, and advanced settings\n- **[\ud83c\udfaf Providers](docs/providers.md)** - Provider-specific integration guides with examples\n- **[\ud83d\udd17 LangChain Integration](docs/langchain.md)** - Complete LangChain integration guide\n- **[\ud83d\udcb0 Pricing & Models](docs/pricing.md)** - Model discovery, pricing info, and cost calculation\n- **[\ud83d\udee0\ufe0f SDK Helper Functions](docs/sdk-functions.md)** - Complete reference for all SDK helper functions\n- **[\ud83d\udd0d Troubleshooting](docs/troubleshooting.md)** - Common issues and solutions\n- **[\ud83e\uddea Testing](docs/testing.md)** - Comprehensive testing documentation\n- **[\ud83d\ude80 Deployment](docs/deployment.md)** - Deployment guide for PyPI and version management\n\n### Quick Links\n\n- **Getting Started**: See [Basic Usage](#basic-usage) above\n- **\ud83c\udfe2 Multi-Tenant Apps**: [Client Tracking Guide](docs/client-tracking.md) - Perfect for agencies & SaaS\n- **Provider Setup**: [Providers Guide](docs/providers.md)\n- **LangChain Users**: [LangChain Integration](docs/langchain.md)\n- **Advanced Config**: [Configuration Guide](docs/configuration.md)\n- **Having Issues?**: [Troubleshooting Guide](docs/troubleshooting.md)\n\n## \ud83d\udcbb Quick Examples\n\n### OpenAI\n\n```python\nfrom llmcosts import LLMTrackingProxy, Provider\nimport openai\n\nclient = openai.OpenAI(api_key=\"your-key\")\ntracked_client = LLMTrackingProxy(client, provider=Provider.OPENAI)\n\nresponse = tracked_client.chat.completions.create(\n    model=\"gpt-4o-mini\",\n    messages=[{\"role\": \"user\", \"content\": \"Hello!\"}]\n)\n```\n\n### Anthropic\n\n```python\nfrom llmcosts import LLMTrackingProxy, Provider\nimport anthropic\n\nclient = anthropic.Anthropic(api_key=\"your-key\")\ntracked_client = LLMTrackingProxy(client, provider=Provider.ANTHROPIC)\n\nresponse = tracked_client.messages.create(\n    model=\"claude-3-haiku-20240307\",\n    max_tokens=1000,\n    messages=[{\"role\": \"user\", \"content\": \"Hello!\"}]\n)\n```\n\n### LangChain\n\n```python\nfrom llmcosts import LLMTrackingProxy, Provider, Framework\nfrom langchain_openai import ChatOpenAI\nimport openai\n\n# Key difference: specify framework=Framework.LANGCHAIN\nopenai_client = openai.OpenAI(api_key=\"your-key\")\ntracked_client = LLMTrackingProxy(\n    openai_client,\n    provider=Provider.OPENAI,\n    framework=Framework.LANGCHAIN  # Enable LangChain integration\n)\n\nchat_model = ChatOpenAI(client=tracked_client.chat.completions)\nresponse = chat_model.invoke([{\"role\": \"user\", \"content\": \"Hello!\"}])\n```\n\n### Client Tracking (Multi-Tenant)\n\n```python\nfrom llmcosts import LLMTrackingProxy, Provider\nimport openai\n\nclient = openai.OpenAI(api_key=\"your-key\")\ntracked_client = LLMTrackingProxy(\n    client,\n    provider=Provider.OPENAI,\n    client_customer_key=\"customer_acme_corp\",  # Track costs per customer\n    context={\n        \"user_id\": \"user_123\",\n        \"project\": \"chatbot_v2\", \n        \"department\": \"support\",\n        \"environment\": \"production\"\n    }\n)\n\nresponse = tracked_client.chat.completions.create(\n    model=\"gpt-4o-mini\",\n    messages=[{\"role\": \"user\", \"content\": \"Hello!\"}]\n)\n# \u2192 Automatically tracked with customer and context data for billing/analytics\n```\n\n> **See [Provider Integration Guide](docs/providers.md) for complete examples of all supported providers.**\n\n## \ud83d\udd0d Model Discovery & Pricing\n\n```python\nfrom llmcosts import list_models, get_model_pricing, calculate_cost_from_tokens, Provider\n\n# Discover available models\nmodels = list_models()\nprint(f\"Total models: {len(models)}\")\n\n# Get pricing information\npricing = get_model_pricing(Provider.OPENAI, \"gpt-4o-mini\")\nprint(f\"Input: ${pricing['costs'][0]['cost_per_million']}/M tokens\")\n\n# Calculate costs\ncost = calculate_cost_from_tokens(\n    Provider.OPENAI, \"gpt-4o-mini\", \n    input_tokens=1000, output_tokens=500\n)\nprint(f\"Total cost: ${cost['costs']['total_cost']}\")\n```\n\n> **See [Pricing & Models Guide](docs/pricing.md) for complete model discovery and cost calculation features.**\n\n## \ud83d\udee0\ufe0f Development\n\n### Setup\n\n```bash\n# Clone repository\ngit clone https://github.com/llmcosts/llmcosts-python.git\ncd llmcosts-python\n\n# Using uv (recommended)\nuv sync --extra dev\n\n# Using pip\npip install -e \".[dev]\"\n```\n\n### Testing\n\n```bash\n# Quick test (requires your own OpenAI API key)\nuv run python tests/check.py openai gpt-4o-mini\n\n# Full test suite\nuv run pytest\n\n# With coverage\nuv run pytest --cov=llmcosts --cov-report=html\n```\n\n**\ud83d\udd12 Testing Privacy**: Tests use YOUR provider API keys locally to verify functionality. These keys never leave your machine - they're only used for local testing.\n\n> **See [Testing Guide](docs/testing.md) for comprehensive testing documentation including how to set up provider API keys for local testing.**\n\n## \ud83e\udd1d Contributing\n\n1. Fork the repository\n2. Create a feature branch: `git checkout -b feature-name`\n3. Make changes and add tests\n4. Run the test suite: `uv run pytest`\n5. Ensure code quality: `uv run black llmcosts/ tests/` and `uv run isort llmcosts/ tests/`\n6. Submit a pull request\n\n## \ud83d\udcdd License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## \ud83d\udd17 Links\n\n- **\ud83c\udf10 Website**: [llmcosts.com](https://llmcosts.com)\n- **\ud83d\udce6 PyPI**: [pypi.org/project/llmcosts](https://pypi.org/project/llmcosts/)\n- **\ud83d\udc19 GitHub**: [github.com/llmcosts/llmcosts-python](https://github.com/llmcosts/llmcosts-python)\n- **\ud83d\udc1b Issues**: [github.com/llmcosts/llmcosts-python/issues](https://github.com/llmcosts/llmcosts-python/issues)\n- **\ud83d\udce7 Support**: [help@llmcosts.com](mailto:help@llmcosts.com)\n\n## \ud83d\udcc8 Changelog\n\n### v0.2.2 (Current)\n- New SDK helper functions with comprehensive documentation\n- Enhanced events management with filtering, search, and export\n- Added `list_threshold_events()` function for threshold event tracking\n- Improved network resilience in tests with retry logic\n- **Breaking changes**: Removed `limits.py`, `customers.py`, and `alerts.py` modules\n- Migration guide available in [CHANGELOG.md](CHANGELOG.md)\n\n### v0.1.0\n- Universal LLM provider support\n- Dynamic configuration with property setters\n- Context tracking for user/session data\n- Response callbacks for custom processing\n- Synchronous mode for testing\n- Resilient background delivery\n- Comprehensive test coverage\n- Thread-safe global tracker management\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A generic, provider-agnostic SDK wrapper that intercepts LLM API responses and extracts usage information for cost tracking with structured payload formatting.",
    "version": "0.2.5",
    "project_urls": {
        "Bug Reports": "https://github.com/llmcosts/llmcosts-python/issues",
        "Documentation": "https://github.com/llmcosts/llmcosts-python#readme",
        "Homepage": "https://github.com/llmcosts/llmcosts-python",
        "Source": "https://github.com/llmcosts/llmcosts-python"
    },
    "split_keywords": [
        "llm",
        " ai",
        " cost-tracking",
        " openai",
        " anthropic",
        " gemini",
        " bedrock",
        " usage-monitoring"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "890882ec3e0eaeb5a0d1dc96773fb69efb6a44c88799d35c74b6a41d73221563",
                "md5": "75a2955fcd9e5819fa23124efc697792",
                "sha256": "1a5c6bf2c7542f9372fd3297fcbc6d4602c57c383efad9c1d865f6d84306aa06"
            },
            "downloads": -1,
            "filename": "llmcosts-0.2.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "75a2955fcd9e5819fa23124efc697792",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 52673,
            "upload_time": "2025-07-08T19:40:16",
            "upload_time_iso_8601": "2025-07-08T19:40:16.946893Z",
            "url": "https://files.pythonhosted.org/packages/89/08/82ec3e0eaeb5a0d1dc96773fb69efb6a44c88799d35c74b6a41d73221563/llmcosts-0.2.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "954339c678d3f3c3ff05f1767ca413dc584a80d58df06aa27e2a07dafb5e255d",
                "md5": "2ec8710baf7ee01b48cc4d986ba846d7",
                "sha256": "5f66aa21d68c1babbcd6f203ce7644be1d1dad0be6f24487d4548663c3a4e88e"
            },
            "downloads": -1,
            "filename": "llmcosts-0.2.5.tar.gz",
            "has_sig": false,
            "md5_digest": "2ec8710baf7ee01b48cc4d986ba846d7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 109675,
            "upload_time": "2025-07-08T19:40:18",
            "upload_time_iso_8601": "2025-07-08T19:40:18.278802Z",
            "url": "https://files.pythonhosted.org/packages/95/43/39c678d3f3c3ff05f1767ca413dc584a80d58df06aa27e2a07dafb5e255d/llmcosts-0.2.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-08 19:40:18",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "llmcosts",
    "github_project": "llmcosts-python",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "llmcosts"
}
        
Elapsed time: 1.46138s