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.2.0.tar.gz (15.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.2.0-py3-none-any.whl (20.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: metrxbot-0.2.0.tar.gz
  • Upload date:
  • Size: 15.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.2.0.tar.gz
Algorithm Hash digest
SHA256 a2e8d9e77bc70b82b5cad3af6e1c0886dd2d0b7e19658e8021d971c9708fbabe
MD5 b321b9531512b21d3491b3ce156fc8bf
BLAKE2b-256 b9fdef9e5ccd7526e1539ed84701b020cf08f3aec2012cf89ffb350f44663244

See more details on using hashes here.

File details

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

File metadata

  • Download URL: metrxbot-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 20.3 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9f094b3ed1b24f77600ec6436dc4b7b07d7659304cd4a576523b38ea8bc37fc4
MD5 1e84cb16c179007f44082fee290c53b0
BLAKE2b-256 7dd4b7cdd19e03edf6fd9551cdede0c344491932b370d71b39a3bb720edcccfe

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