TypeScript SDK
Complete reference for the Retrace TypeScript SDK.
Installation
npm install retraceRequires Node.js 20+. This is an ESM-only package with zero runtime dependencies beyond the Node.js standard library.
Configuration
Configure the SDK at application startup:
import { configure } from "retrace";
configure({
apiKey: "rt_live_...",
baseUrl: "https://api.retrace.yashbogam.me",
projectId: "my-project",
});Or rely on environment variables:
| 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 tracing |
record() Function
Create a recorder instance for manual control over the trace lifecycle:
import { record } from "retrace";
const recorder = record({ name: "my-agent" });
recorder.start();
// ... your agent logic ...
recorder.end(result);If an error occurs, call recorder.fail(error) to mark the trace as failed and record the exception.
trace() Helper
Wrap any async function for automatic trace recording. Input arguments and return values are captured:
import { trace } from "retrace";
const myAgent = trace(async (prompt: string) => {
const response = await ai.models.generateContent({
model: "gemini-3.1-pro-preview",
messages: [{ role: "user", content: prompt }],
});
return response.choices[0].message.content;
}, { name: "my-agent" });
const result = await myAgent("hello");Exceptions thrown inside the wrapped function are recorded and re-thrown.
Manual Spans
Add child spans for fine-grained observability into individual steps:
import { record, SpanType } from "retrace";
const recorder = record({ name: "agent" });
recorder.start();
const span = recorder.startSpan("web_search", SpanType.TOOL_CALL, { query: "test" });
const results = await search("test");
recorder.endSpan(span, results);
recorder.end();Supported span types: LLM_CALL, TOOL_CALL, RETRIEVAL, CUSTOM.
Gemini Interceptor
Automatically capture all Gemini SDK calls as spans:
import { installGeminiInterceptor } from "retrace";
installGeminiInterceptor((span) => recorder.addSpan(span));Records model, messages, token usage, latency, and estimated cost per call.
Transport Modes
| Mode | Protocol | Use Case |
|---|---|---|
auto | WebSocket with HTTP fallback | Default for most environments |
ws | WebSocket only | Real-time streaming, persistent processes |
http | HTTP batch (native fetch) | Serverless (Vercel, Cloudflare Workers) |
Configure explicitly:
configure({ apiKey: "rt_live_...", transport: "http" });Metadata and Tags
Attach custom metadata to any trace for filtering and search:
const recorder = record({
name: "agent",
metadata: { environment: "production", version: "2.1.0" },
tags: ["customer-facing", "high-priority"],
});Disabling in Tests
process.env.RETRACE_ENABLED = "false";Or pass the flag directly:
configure({ apiKey: "rt_live_...", enabled: false });[!NOTE] The TypeScript SDK is ESM-only. For CommonJS projects, use a dynamic import:
const retrace = await import("retrace").