Track LLM costs per user. Open source SDK for AgentMeter.
Project description
AgentMeter SDK
Track LLM costs per user. The official Python SDK for AgentMeter.
AgentMeter automatically tracks token usage across OpenAI and Anthropic API calls, attributes costs to individual users, and reports them to your AgentMeter dashboard.
Installation
pip install agentmeter-sdk
With provider-specific extras:
pip install agentmeter-sdk[openai]
pip install agentmeter-sdk[anthropic]
pip install agentmeter-sdk[all]
Quick Start
import agentmeter
# Initialize before creating any LLM clients
agentmeter.init(api_key="am_live_xxx")
from openai import OpenAI
client = OpenAI()
# Wrap LLM calls with identify() to attribute usage to a user
with agentmeter.identify(user_id="user_123"):
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello!"}],
)
That's it. Token usage is automatically captured and sent to your AgentMeter dashboard.
Usage
Initialization
Call agentmeter.init() before creating any LLM client instances. This patches the OpenAI and Anthropic libraries to capture usage data.
agentmeter.init(
api_key="am_live_xxx", # Required. Get this from your dashboard.
enabled=True, # Disable to turn off tracking (e.g. in tests).
debug=False, # Print events to console instead of sending them.
)
Identifying Users
Use identify() as a context manager or decorator to associate LLM calls with a user:
# Context manager
with agentmeter.identify(user_id="user_123"):
response = client.chat.completions.create(...)
# Decorator
@agentmeter.identify(user_id="user_123")
def handle_request():
return client.chat.completions.create(...)
You can optionally pass a session_id to group multiple LLM calls into a single session:
with agentmeter.identify(user_id="user_123", session_id="sess_abc"):
response = client.chat.completions.create(...)
Supported Providers
| Provider | Sync | Async | Streaming |
|---|---|---|---|
| OpenAI | Yes | Yes | Yes |
| Anthropic | Yes | Yes | Yes |
Shutdown
Flush pending events before your process exits:
agentmeter.shutdown()
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 Distributions
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 agentmeter_sdk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: agentmeter_sdk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a3f4c5dbdc1c833cede7ef9f950960bca148b0e1bdd4515876fe0844af1c859f
|
|
| MD5 |
ca31a7e4bbe9a2a7e827053f15d0186b
|
|
| BLAKE2b-256 |
2b8b8d4d137f9c3c50e9a5db5677386b740f175bac7401cfb852fb8cdfa10755
|