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
Release files for agentmeter-sdk 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| agentmeter_sdk-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Release files / agentmeter_sdk-0.1.0-py3-none-any.whl
| Download URL | agentmeter_sdk-0.1.0-py3-none-any.whl |
|---|---|
| Size | 9.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a3f4c5dbdc1c833cede7ef9f950960bca148b0e1bdd4515876fe0844af1c859f
|
|
BLAKE2b-256 checksum How to use checksums |
2b8b8d4d137f9c3c50e9a5db5677386b740f175bac7401cfb852fb8cdfa10755
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.8
|