Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

metrxbot-0.1.0.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

metrxbot-0.1.0-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file metrxbot-0.1.0.tar.gz.

File metadata

  • Download URL: metrxbot-0.1.0.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for metrxbot-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1f449c93cfdb9156fef3d1ca1e17f82c41fc2beb6bb4e6df3881b278f9a838be
MD5 e9b3ad3391eecf450d0b2afe702082c9
BLAKE2b-256 e52617a5fd5b415917434639808c7d7e1e8ba7d8aaf0bad50abcfb2cd9adc49c

See more details on using hashes here.

File details

Details for the file metrxbot-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: metrxbot-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for metrxbot-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f4eedf25a3101d0f3803714e99df3864f9b08cd65b662e65a736de890f8ee580
MD5 d065a84b1ec2ef4df262cff5fc184ca2
BLAKE2b-256 cb47c41f25e66adae1850ab72608cd7978a8fdb0eccf336fad775c5a0a1aa38f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page