trAIce Python SDK
The traice-sdk distribution records LLM model usage, tokens, cost, latency, status, and product attribution, then sends events to trAIce on a background thread. Provider responses and exceptions pass through unchanged. Its Python import name is traice.
Install
pip install traice-sdk
Python 3.9 or newer is supported. The core package has no runtime dependencies. OpenAI, Anthropic, and LangChain remain optional application dependencies. PyPI does not support scoped package names, and the unrelated traice distribution is already registered, so installation uses traice-sdk while imports use traice.
Five-minute quickstart
Configure the client once when your process starts:
import os
from traice import configure
configure(
api_key=os.environ["TRAICE_API_KEY"],
endpoint="https://runtraice.com/api/v1/events",
)
Decorate a sync or async function that returns an OpenAI or Anthropic response:
from openai import OpenAI
from traice import track
openai = OpenAI()
@track(
feature="support-summary",
tenant_id="customer_42",
user_id="user_123",
workflow_id="support",
)
def summarize_ticket():
return openai.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Summarize this ticket"}],
)
completion = summarize_ticket()
The decorator reads usage from current OpenAI Chat Completions, OpenAI Responses, and Anthropic Messages response shapes. It captures provider-reported cache tokens when present.
Context manager
Use a context manager when a decorator does not fit. Attach the response with span.record() so token usage can be extracted:
from traice import track
with track(feature="answer", tenant_id="customer_42") as span:
response = span.record(openai.responses.create(model="gpt-4o-mini", input="Hello"))
Async context managers are supported too:
async with track(feature="answer", tenant_id="customer_42") as span:
response = span.record(await async_openai.responses.create(model="gpt-4o-mini", input="Hello"))
Attribution dimensions
track() accepts the same collection dimensions as @traice/sdk:
| Python argument | Event field | Use |
|---|---|---|
feature |
feature |
Product feature or request path |
user_id |
userId |
End user |
tenant_id |
tenantId |
Paying customer or account |
agent_id |
agentId |
Agent identity |
workflow_id |
workflowId |
Workflow identity |
run_id |
runId |
One workflow or agent execution |
step_id |
stepId |
Step within a run |
tool_name |
toolName |
Tool used by an agent |
retry_count |
retryCount |
Retry attempt number |
outcome |
outcome |
Product or workflow outcome |
metadata |
metadata |
JSON-serializable structured context |
metadata.sdk is always python and metadata.sdkVersion contains the package version.
Batching and shutdown
Events are appended to a bounded in-memory queue. A daemon thread sends batches every five seconds or when 50 events accumulate. A failed batch is retried once, then dropped. Collection failures do not enter the application request path.
Tune this behavior at startup:
configure(
api_key=os.environ["TRAICE_API_KEY"],
batch_size=100,
flush_interval=2.0,
timeout=5.0,
max_queue_size=5_000,
)
The SDK registers an atexit flush. Explicitly flush short-lived scripts and serverless handlers:
from traice import flush
flush(timeout=2.0)
configure() returns a TraiceClient. Call client.stats() to inspect enqueued, sent, dropped, failed-batch, and queued counts.
Errors
Provider exceptions are re-raised unchanged. The SDK queues an error event with zero tokens, measured latency, and a truncated error message in metadata.
Calling track() before configure() leaves the provider call unchanged and records nothing. configure() rejects a missing API key immediately. It uses TRAICE_API_KEY when api_key is omitted.
Custom endpoint and pricing
endpoint accepts either the site base URL or the full /api/v1/events URL. Unknown models are sent with costUsd: 0 while their token counts remain intact. Add local pricing in USD per million tokens:
from traice import configure_pricing
configure_pricing(
"openai",
"my-fine-tuned-model",
input_per_million=1.25,
output_per_million=5.0,
)
LangChain and LangGraph
The callback handler has no hard LangChain dependency:
from traice.integrations import TraiceCallbackHandler
handler = TraiceCallbackHandler(feature="research", tenant_id="customer_42")
result = chain.invoke({"topic": "unit economics"}, config={"callbacks": [handler]})
The handler captures the token usage and model information that LangChain exposes through llm_output. LangGraph accepts the same callback configuration.
Privacy
The SDK sends usage metadata, attribution dimensions, and error text. It does not send prompts or model outputs. Do not place secrets or sensitive content in attribution fields or metadata.
Development
Run the dependency-free test suite:
PYTHONPATH=src python -m unittest discover -s tests -v
Build the package with python -m build. Release artifacts are source distributions and universal Python wheels.
Metadata
Release files for traice-sdk 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| traice_sdk-0.1.1.tar.gz | 23.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| traice_sdk-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 36.3 kB
Release files / traice_sdk-0.1.1.tar.gz
| Download URL | traice_sdk-0.1.1.tar.gz |
|---|---|
| Size | 23.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e6e92c89187e821214f841ceeaf7198cb0e58dd7814ce25949d89cab281aee18
|
|
BLAKE2b-256 checksum How to use checksums |
fb94435ef903ff0ddbf4de49564221ffe4b3c6cea2f918136f1fe3badbb56dcb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 19, 2026.
Transparency logRelease files / traice_sdk-0.1.1-py3-none-any.whl
| Download URL | traice_sdk-0.1.1-py3-none-any.whl |
|---|---|
| Size | 12.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a9a5fd8058a1c02ed06b4a42b80457ce546d8a025a1d0a2b135bbac0d24c9f68
|
|
BLAKE2b-256 checksum How to use checksums |
1424d29020b87c570ac12a4830f4c407417920d51a519734858c7d8234f6e3d1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 19, 2026.
Transparency log