Metrx Python SDK — LLM cost tracking and attribution
Project description
metrxbot
Python SDK for Metrx — LLM cost tracking and attribution for AI agents.
Track every LLM call your agents make, attribute costs to individual agents, and log business outcomes for ROI analysis.
Installation
The PyPI package is metrxbot; the import name is metrx:
pip install metrxbot
With provider extras:
pip install metrxbot[openai] # OpenAI support
pip install metrxbot[anthropic] # Anthropic support
pip install metrxbot[all] # All providers
Quick Start
from metrx import Metrx
m = Metrx(api_key="sk_metrx_...", agent_key="my-agent")
m.instrument() # Auto-detects and patches OpenAI + Anthropic
# Use your LLM clients as normal — calls are tracked automatically
import openai
client = openai.OpenAI()
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello!"}],
)
m.shutdown()
Framework Support
instrument() automatically detects your AI framework and enriches every event with framework metadata. Supported frameworks:
- LangChain / LangGraph
- CrewAI
- AutoGen
- LlamaIndex
- Haystack
- Semantic Kernel
No extra configuration needed — since these frameworks use OpenAI or Anthropic under the hood, monkey-patching captures all LLM calls automatically.
Manual Instrumentation
If you prefer explicit control, patch individual clients:
from metrx import Metrx
import openai
import anthropic
m = Metrx(api_key="sk_metrx_...")
# Patch specific clients
oai = m.instrument_openai(openai.OpenAI())
ant = m.instrument_anthropic(anthropic.Anthropic())
# All calls through these clients are now tracked
response = oai.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Hello!"}],
)
Sessions
Group related LLM calls under a session for attribution:
with m.session(session_id="user-123-conv-1"):
# All LLM calls in this block share the same session_id
response = client.chat.completions.create(...)
Outcome Logging
Log business outcomes to measure agent ROI:
m.log_outcome(
outcome_type="sale",
value_cents=5000,
customer_id="cust-456",
reference_id="order-789",
metadata={"product": "pro-plan"},
)
Configuration
| Parameter | Default | Description |
|---|---|---|
api_key |
required | Your Metrx API key |
base_url |
https://gateway.metrxbot.com |
API endpoint |
agent_key |
None |
Default agent identifier for all events |
flush_interval |
5.0 |
Seconds between background flushes |
max_batch_size |
50 |
Events per batch |
debug |
False |
Enable debug logging |
Context Manager
with Metrx(api_key="sk_metrx_...") as m:
m.instrument()
# ... your code ...
# Transport shuts down automatically
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 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 metrxbot-0.3.0.tar.gz.
File metadata
- Download URL: metrxbot-0.3.0.tar.gz
- Upload date:
- Size: 15.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
071b1c1a8755a5f6aff51c65ff4294333f996ad1f8ec7d1908da2410e490d69b
|
|
| MD5 |
7065d4e6b88325b9cb0bcf9c5727c376
|
|
| BLAKE2b-256 |
682326901d4c375b7eede63ff4b31c434041f0e4377e07fccd1efb44a3bf4a13
|
File details
Details for the file metrxbot-0.3.0-py3-none-any.whl.
File metadata
- Download URL: metrxbot-0.3.0-py3-none-any.whl
- Upload date:
- Size: 20.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
504c2e56429587fec1708c6a4e0793c38f14dad0504c1ebe87a5be99cd5db96f
|
|
| MD5 |
1abae6960632ff3498399543ca57dd19
|
|
| BLAKE2b-256 |
d99f3dedbf57737994665f0bccb0d60dbdd19d0dbf5c682fce6e6613b07cbf96
|