Python SDK
Complete reference for the Retrace Python SDK.
Installation
pip install retraceRequires Python 3.10+. The package has minimal dependencies: websocket-client for streaming and requests for HTTP transport.
Configuration
Configure the SDK at application startup:
import retrace
retrace.configure(
api_key="rt_live_...",
base_url="https://api.retrace.yashbogam.me",
project_id="my-project",
)Alternatively, set environment variables and call retrace.configure() with no arguments:
| Variable | Default | Description |
|---|---|---|
RETRACE_API_KEY | — | Your API key (required) |
RETRACE_BASE_URL | https://api.retrace.yashbogam.me | API endpoint |
RETRACE_PROJECT_ID | — | Default project identifier |
RETRACE_ENABLED | true | Set to false to disable all tracing |
@record Decorator
The simplest way to trace a function. Input arguments and return values are captured automatically:
@retrace.record(name="research-agent")
def research(topic: str) -> str:
response = client.models.generate_content(
model="gemini-3.1-pro-preview",
messages=[{"role": "user", "content": f"Research {topic}"}]
)
return response.choices[0].message.contentIf the function raises an exception, the trace is marked as failed and the error is recorded.
Context Manager
For more control over trace lifecycle, use the context manager form:
with retrace.record(name="agent", input={"prompt": "hello"}) as t:
result = agent.run("hello")
t.output = resultYou can attach metadata, set custom status, or add tags within the block:
with retrace.record(name="pipeline") as t:
t.metadata = {"environment": "staging", "version": "1.2.0"}
result = run_pipeline()
t.output = resultManual Spans
Create child spans for granular visibility into individual steps:
with retrace.record(name="agent") as t:
span = t._recorder.start_span(
name="web_search",
span_type=retrace.SpanType.TOOL_CALL,
input={"query": "quantum computing"}
)
results = search("quantum computing")
t._recorder.end_span(span.id, output=results)Supported span types: LLM_CALL, TOOL_CALL, RETRIEVAL, CUSTOM.
Gemini Interceptor
Automatically captures all Gemini API calls as spans:
from retrace.interceptors.gemini import install_gemini_interceptor
install_gemini_interceptor()Captured data includes model, messages, token usage, latency, and cost.
Transport Modes
| Mode | Protocol | Use Case |
|---|---|---|
auto | WebSocket with HTTP fallback | Default, recommended for most environments |
ws | WebSocket only | Real-time streaming, long-running agents |
http | HTTP batch | Serverless (Lambda, Cloud Functions) |
Configure transport explicitly:
retrace.configure(api_key="rt_live_...", transport="http")Disabling in Tests
Prevent trace emission during test runs:
import os
os.environ["RETRACE_ENABLED"] = "false"[!TIP] Async support is coming soon. Currently the SDK uses synchronous WebSocket (
websocket-client) or HTTP (requests). For async frameworks, usetransport="http"to avoid blocking the event loop.