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
Metadata
Release files for metrxbot 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| metrxbot-0.3.0.tar.gz | 15.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| metrxbot-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 36.6 kB
Release files / metrxbot-0.3.0.tar.gz
| Download URL | metrxbot-0.3.0.tar.gz |
|---|---|
| Size | 15.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
071b1c1a8755a5f6aff51c65ff4294333f996ad1f8ec7d1908da2410e490d69b
|
|
BLAKE2b-256 checksum How to use checksums |
682326901d4c375b7eede63ff4b31c434041f0e4377e07fccd1efb44a3bf4a13
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.2
|
Release files / metrxbot-0.3.0-py3-none-any.whl
| Download URL | metrxbot-0.3.0-py3-none-any.whl |
|---|---|
| Size | 20.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
504c2e56429587fec1708c6a4e0793c38f14dad0504c1ebe87a5be99cd5db96f
|
|
BLAKE2b-256 checksum How to use checksums |
d99f3dedbf57737994665f0bccb0d60dbdd19d0dbf5c682fce6e6613b07cbf96
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.2
|