Python SDK

The trakr-monitor package patches Anthropic and OpenAI clients at import time. Sync and async clients are both supported.

Installation

pip install trakr-monitor

# With Anthropic support
pip install trakr-monitor[anthropic]

# With OpenAI support
pip install trakr-monitor[openai]

Initialization

import trakr_monitor as trakr

trakr.init(api_key="tk_live_...")
# or using TRAKR_API_KEY environment variable:
trakr.init()

Automatic instrumentation

import trakr_monitor as trakr
import anthropic

trakr.init()

client = anthropic.Anthropic()

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
)

Async clients (anthropic.AsyncAnthropic, openai.AsyncOpenAI) are patched automatically.

Decorator pattern

Track a complete workflow with the @track_agent decorator:

import trakr_monitor as trakr

trakr.init()

@trakr.track_agent(name="invoice-extractor")
def extract_invoice(document: str) -> dict:
    return do_extraction(document)

The decorator creates a run, tracks all LLM calls inside the function, and ends the run with success or error status.

Context manager

For more control, use the context manager:

import trakr_monitor as trakr

trakr.init()

with trakr.start_run("invoice-extractor") as run:
    result = do_extraction(document)
    run.set_metadata("pages", 3)

Async support

import trakr_monitor as trakr
import anthropic

trakr.init()

client = anthropic.AsyncAnthropic()

async def process():
    message = await client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Hello"}],
    )
    return message

Graceful shutdown

For scripts that exit after completion:

import trakr_monitor as trakr

trakr.init()
# ... do work ...
trakr.flush()