Monitor your AI agents. Track cost, latency, and errors in two lines of code.
Project description
agentmetrics — Python SDK
Production observability for AI agents. Track cost, latency, tokens, failures, and tool calls — one decorator, zero infrastructure.
→ agentmetrics.dev/docs/sdk/python
Install
pip install agentmetrics
Requires Python 3.9 or later.
Quickstart
import os
import agentmetrics
agentmetrics.configure(api_key=os.environ["AGENTMETRICS_API_KEY"])
agentmetrics.instrument() # auto-patches OpenAI, Anthropic, LiteLLM, and more
@agentmetrics.track(agent_id="my-agent")
def run(task: str) -> str:
answer = call_llm(task)
return answer
Every call to run() reports to your dashboard: duration, cost, token usage, success/failure, and error messages.
API
agentmetrics.configure()
Call once at startup before any @track decorators execute.
agentmetrics.configure(
api_key="am_live_...", # required
environment="production", # optional — tags every run
sample_rate=1.0, # optional — float 0.0–1.0
base_url="https://...", # optional — override for self-hosted
batch_size=20, # optional — events per batch
flush_interval=2.0, # optional — seconds between flushes
)
agentmetrics.instrument()
Patches installed LLM SDKs to auto-capture token counts and model names on every call. Idempotent.
agentmetrics.instrument()
Supported: OpenAI (+ Azure, Groq, Together AI) · Anthropic · LiteLLM · Google Gemini · Cohere · Mistral · LangChain / LangGraph / CrewAI · LlamaIndex
@agentmetrics.track()
Decorator for sync and async agent functions.
@agentmetrics.track(agent_id="my-agent", metadata={"env": "prod"})
def run(task: str) -> str:
return call_llm(task)
@agentmetrics.track(agent_id="async-agent")
async def run_async(task: str) -> str:
return await call_llm_async(task)
| Parameter | Type | Description |
|---|---|---|
agent_id |
str |
Identifier shown in the dashboard |
metadata |
dict |
Optional key-value pairs attached to every run |
agentmetrics.step()
Context manager for timing a named phase within a tracked agent.
@agentmetrics.track(agent_id="pipeline")
def run(query: str) -> str:
with agentmetrics.step("retrieve"):
docs = vector_search(query)
with agentmetrics.step("generate"):
return call_llm(query, docs)
agentmetrics.tool()
Context manager for tracking individual tool calls.
@agentmetrics.track(agent_id="research-agent")
def run(query: str) -> str:
with agentmetrics.tool("web_search"):
results = web_search(query)
return summarize(results)
agentmetrics.flush()
Blocks until all queued events are sent. Call before process exit in scripts.
agentmetrics.flush(timeout=10.0)
agentmetrics.trace_id
Returns the active trace ID from inside a tracked function.
@agentmetrics.track(agent_id="my-agent")
def run(task: str) -> str:
print(agentmetrics.trace_id) # e.g. "a3f1c2d4-..."
return call_llm(task)
License
Project details
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 agentmetrics-0.1.2.tar.gz.
File metadata
- Download URL: agentmetrics-0.1.2.tar.gz
- Upload date:
- Size: 10.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
58c7440768db69fc30e23daa721d06379fe5769cd67273720c1cecaceefc91b8
|
|
| MD5 |
5b1c1b8226de7c1042fc27a6ca3cef47
|
|
| BLAKE2b-256 |
25843d65748019e654e059daae5d6233c1d6509fc137ac13b0f2ea2faea5cf57
|
File details
Details for the file agentmetrics-0.1.2-py3-none-any.whl.
File metadata
- Download URL: agentmetrics-0.1.2-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c27b84716f30a74bafda3676d9907f99aa577a84dacbd7c1c2ad2a4fe12af1d
|
|
| MD5 |
649a979fd2e00e5cc7b6ff4da204aea1
|
|
| BLAKE2b-256 |
a472a23bf64a18334118eba4920aea3eeac53b175d75859473f9d0d10b84a1ff
|