Python SDK for monitoring LLM API calls with TokenWatcher
Project description
TokenWatcher Python SDK
Monitor your LLM API calls with zero code changes. Simply wrap your OpenAI or Anthropic client and get instant observability into tokens, costs, latency, and errors.
Installation
pip install tokenwatcher
For OpenAI support:
pip install tokenwatcher[openai]
For Anthropic support:
pip install tokenwatcher[anthropic]
For both:
pip install tokenwatcher[all]
Quick Start
OpenAI
from tokenwatcher import MonitoredOpenAI
from openai import OpenAI
# Wrap your client (that's it!)
client = MonitoredOpenAI(
OpenAI(api_key="sk-..."),
api_key="ym_your_monitoring_key"
)
# Use OpenAI exactly as before
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello!"}]
)
Anthropic
from tokenwatcher import MonitoredAnthropic
from anthropic import Anthropic
# Wrap your client
client = MonitoredAnthropic(
Anthropic(api_key="sk-ant-..."),
api_key="ym_your_monitoring_key"
)
# Use Anthropic exactly as before
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello!"}]
)
Configuration
from tokenwatcher import MonitoredOpenAI
client = MonitoredOpenAI(
openai_client,
api_key="ym_your_key", # Required: Your TokenWatcher API key
base_url="https://api.token-watcher.com", # Optional: Custom backend URL
context="production/chatbot", # Optional: Tag events with context
user_identifier="user-123", # Optional: Associate events with a user
buffer_size=100, # Optional: Max events before auto-flush (default: 100)
flush_interval=10, # Optional: Seconds between auto-flush (default: 10)
enabled=True, # Optional: Disable monitoring (default: True)
)
Features
- Zero Code Changes: Drop-in replacement for OpenAI and Anthropic clients
- Automatic Monitoring: Captures tokens, latency, costs, and errors
- Auto-Context Detection: Automatically detects calling module and function for better observability
- Buffered & Async: Events are buffered and sent in background, no performance impact
- Silent Failures: If monitoring fails, your LLM calls continue uninterrupted
- Multi-tenant: Each API key is isolated to your account
- Type Safe: Full type hints for Python 3.8+
How It Works
The SDK wraps your LLM client and intercepts API calls to extract:
- Provider and model used
- Input and output token counts
- Request latency
- Success/error status
- Error details (if any)
- Calling context (automatically detects module and function name)
Events are buffered and sent to your TokenWatcher backend in batches, ensuring minimal overhead.
Automatic Context Detection
When you don't provide an explicit context parameter, the SDK automatically detects the calling module and function:
# In myapp/chatbot.py
def handle_user_message(message):
client = MonitoredOpenAI(openai_client, api_key="ym_key")
response = client.chat.completions.create(...)
# Context will be auto-set to: "myapp.chatbot.handle_user_message"
This provides automatic code-level observability without any manual tagging! You can still override this by providing your own context parameter.
Requirements
- Python 3.8+
- requests >= 2.25.0
- openai >= 1.0.0 (optional, for OpenAI support)
- anthropic >= 0.18.0 (optional, for Anthropic support)
Environment Variables
You can configure the SDK using environment variables:
export TOKENWATCHER_API_KEY=ym_your_key
export TOKENWATCHER_BASE_URL=https://api.token-watcher.com
export TOKENWATCHER_CONTEXT=production
Then use without explicit configuration:
from tokenwatcher import MonitoredOpenAI
from openai import OpenAI
import os
client = MonitoredOpenAI(
OpenAI(api_key="sk-..."),
api_key=os.getenv("TOKENWATCHER_API_KEY")
)
Support
- Documentation: https://docs.token-watcher.com
- Issues: https://github.com/tokenwatcher/tokenwatcher-python/issues
- Email: hello@token-watcher.com
License
MIT License - see LICENSE file for details.
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
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 tokenwatcher-0.1.1.tar.gz.
File metadata
- Download URL: tokenwatcher-0.1.1.tar.gz
- Upload date:
- Size: 17.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6dedc032f2a919bbeff807fe6ef27b5dd26513b7fa8d650ff64e87510c35885c
|
|
| MD5 |
a860a9b055f8548f15239cf6d1a956da
|
|
| BLAKE2b-256 |
8d6f808794de6f9b1a07784acb3b0d39b31880c99de54cf52a3ed122485ce042
|
File details
Details for the file tokenwatcher-0.1.1-py3-none-any.whl.
File metadata
- Download URL: tokenwatcher-0.1.1-py3-none-any.whl
- Upload date:
- Size: 11.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf5a25039059bd2c20d028acdee2ad8351d9b966e8aff578ce24ce42cda3d77c
|
|
| MD5 |
abfe2fc9b48132c0c1cfb70e44f70fb2
|
|
| BLAKE2b-256 |
68abc02fd570a94b61052c84a7ba3318bcae37fadd4da5e61998263a6bb1bb89
|