Metadata-Version: 2.4
Name: metrxbot
Version: 0.2.0
Summary: Metrx Python SDK — LLM cost tracking and attribution
Project-URL: Homepage, https://metrxbot.com
Project-URL: Repository, https://github.com/ckpark123/Metrx
Author-email: Metrx <eng@metrxbot.com>
License: MIT
Requires-Python: >=3.9
Requires-Dist: httpx>=0.24.0
Provides-Extra: all
Requires-Dist: anthropic>=0.20; extra == 'all'
Requires-Dist: openai>=1.0; extra == 'all'
Provides-Extra: anthropic
Requires-Dist: anthropic>=0.20; extra == 'anthropic'
Provides-Extra: dev
Requires-Dist: pytest-asyncio>=0.21; extra == 'dev'
Requires-Dist: pytest>=7.0; extra == 'dev'
Requires-Dist: respx>=0.20; extra == 'dev'
Provides-Extra: openai
Requires-Dist: openai>=1.0; extra == 'openai'
Description-Content-Type: text/markdown

# metrxbot

Python SDK for [Metrx](https://metrxbot.com) — 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`:

```bash
pip install metrxbot
```

With provider extras:

```bash
pip install metrxbot[openai]       # OpenAI support
pip install metrxbot[anthropic]    # Anthropic support
pip install metrxbot[all]          # All providers
```

## Quick Start

```python
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:

```python
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:

```python
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:

```python
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

```python
with Metrx(api_key="sk_metrx_...") as m:
    m.instrument()
    # ... your code ...
# Transport shuts down automatically
```

## License

MIT
