Documentation menu
DocsReference

Python API Reference

Public traice-sdk functions, client lifecycle, tracker, callback handler, and data classes.

View source

Install the traice-sdk distribution and import the package as traice. The root entrypoint explicitly defines the supported public surface in traice/__init__.py.

Python
from traice import configure, flush, track

Root exports

APISignature or shapeBehaviorSource
configure(api_key=None, endpoint=DEFAULT_ENDPOINT, **options) -> TraiceClientReplace the process-wide client and return the new client_client.py
flush(timeout=None) -> boolWait for queued and active delivery; return false on timeout_client.py
shutdown(timeout=2.0) -> boolClear the global client, flush, and stop its worker thread_client.py
track(feature=None, **options) -> TrackerCreate a decorator and sync or async context manager_tracking.py
configure_pricing(provider, model, *, input_per_million, output_per_million) -> NoneAdd or replace local model pricing_pricing.py
TraiceClientBackground queue and delivery clientConfigure, enqueue, record, flush, close, and inspect one client_client.py
ClientStatsFrozen data classQueue, delivery acknowledgement, drop, failure, and retry counters_client.py
TrackerDecorator and context managerTrack provider responses and errors without changing the provider result_tracking.py
TraiceCallbackHandlerLangChain-compatible callback handlerTrack LLM callback lifecycle and usage without a hard LangChain dependencylangchain.py
__version__StringInstalled Python package version_version.py

configure() options

Python
configure(
    api_key=None,
    endpoint="https://runtraice.com/api/v1/events",
    batch_size=50,
    flush_interval=5.0,
    timeout=10.0,
    max_queue_size=1_000,
    capture_content=False,
)
ParameterTypeRules
api_keystr | NoneFalls back to TRAICE_API_KEY; a blank or missing value raises ValueError
endpointstrAccepts a base URL or full /api/v1/events URL
batch_sizeintMust be positive
flush_intervalfloatMust be positive
timeoutfloatMust be positive
max_queue_sizeintMust be positive; the oldest event is dropped when full
capture_contentboolInclude supplied prompt and output dimensions; defaults to false

Reconfiguration swaps the global client under a lock, then closes the previous client with a best-effort flush.

track() and Tracker

Python
track(
    feature=None,
    provider=None,
    model=None,
    tenant_id=None,
    user_id=None,
    agent_id=None,
    workflow_id=None,
    run_id=None,
    step_id=None,
    tool_name=None,
    retry_count=None,
    outcome=None,
    metadata=None,
)

track() accepts additional keyword dimensions and forwards non-null values to TraiceClient.record() after snake_case-to-camelCase conversion.

Tracker operationBehavior
@track(...)Wrap a sync or async callable and return its value unchanged
with track(...) as spanMeasure one synchronous block
async with track(...) as spanMeasure one asynchronous block
span.record(response, provider=None, model=None)Attach a response to a context and return it unchanged

Provider errors are re-raised. Tracking failures are swallowed so they cannot replace a provider response or exception.

TraiceClient

MethodSignatureBehavior
enqueue(event: dict) -> NoneAppend without network I/O; drop the oldest event when the queue is full
record(usage, *, latency_ms, status="success", **dimensions) -> NoneBuild a cloud event, calculate cost, add SDK metadata, and enqueue it
flush(timeout=None) -> boolWait until the queue and active send are empty
close(timeout=2.0) -> boolMark the client closing and join its worker thread
stats() -> ClientStatsRead process-local queue and delivery counters under a lock

The worker serializes { "events": [...] }, retries one failed batch once, and then increments dropped and failed-batch counters. It identifies itself with User-Agent: traice-python/<version> and X-Source: traice-python.

Usage extraction

The internal usage extractor supports object or mapping responses with current OpenAI and Anthropic usage field names. It records input, output, cache-read, and cache-write token counts where the provider exposes them.

Automatic provider detection can be overridden with provider and model on the tracker or span.record(). Unsupported response shapes produce zero-token custom usage rather than changing the provider result.

Source: _usage.py.

TraiceCallbackHandler

Construct the handler with feature plus the same snake_case attribution dimensions accepted by track().

Python
handler = TraiceCallbackHandler(
    feature="research",
    tenant_id="customer_42",
)

Public callback methods:

The handler uses a lock around its run-time map and has no hard dependency on LangChain.