Tracing
The@mutagent/sdk/tracing module provides custom instrumentation primitives for tracing LLM calls, agent workflows, tool usage, and more. Use it when the Integration packages do not cover your framework, or when you need fine-grained control over span creation.
For concepts, span kinds, and the overall tracing architecture, see the Tracing Overview. For the full server-side API reference, see the Tracing API.
Installation
The tracing module is included in@mutagent/sdk. No additional packages are required.
Quick Start
Lazy Initialization
If you do not callinitTracing() explicitly, the module automatically initializes from the MUTAGENT_API_KEY environment variable on the first startSpan() call. This is a one-time attempt — if the variable is not set, tracing remains disabled for the rest of the process lifecycle.
Architecture
The tracing module uses a singleton architecture:- SpanManager — Creates and manages spans with
AsyncLocalStoragefor parent-child propagation - BatchCollector — Buffers finished spans in memory and flushes when either the batch size (default 10) or the flush interval (default 5s) is reached. Retries with exponential backoff on failure (up to 3 attempts).
- TraceHTTPClient — Sends grouped span payloads to
POST /api/traces
Instrumentation Approaches
The SDK provides three levels of instrumentation, from highest to lowest abstraction.1. withTrace() Wrapper
The primary instrumentation API. Wraps any function with a tracing span and provides a SpanHandle for manual enrichment during execution.
withTrace runs the function directly with a no-op SpanHandle — your code executes normally without any overhead.
Wrapper Options
SpanHandle Methods
| Method | Description |
|---|---|
setAttributes(attrs) | Add custom key-value attributes to the span |
addEvent(name, attrs?) | Record a lifecycle event with timestamp and optional attributes |
setOutput(output) | Set structured output (SpanIO) |
setInput(input) | Set or override structured input (SpanIO) |
setMetrics(metrics) | Set token counts, cost, latency, and model info |
spanId | Read-only span ID |
traceId | Read-only trace ID |
2. @trace() Decorator
For class-based code. Automatically captures method arguments as input and the return value as output. Requires experimentalDecorators: true in tsconfig.json.
- Creates a span with the specified
kindandname - If
nameis omitted, falls back to the method name, then thekind - Captures arguments as
input.rawand the return value asoutput.raw - Sets
status: 'ok'on success,status: 'error'on exceptions (with message) - Propagates parent-child span relationships via
AsyncLocalStoragecontext - Works with both sync and async methods, preserving
thiscontext - No-ops gracefully when tracing is not initialized
Decorator Options
3. startSpan() / endSpan() Manual API
The lowest-level API for integration adapters or scenarios where automatic span lifecycle does not fit.
Low-Level API Functions
| Function | Description |
|---|---|
startSpan(options) | Create a new span. Returns MutagentSpan | undefined |
endSpan(span, endOptions?) | End a span, compute duration, deliver to batch collector |
runInSpanContext(span, fn) | Run fn within the span’s async context for parent-child propagation |
getCurrentSpan() | Get the active span from async context |
getCurrentTraceId() | Get the active trace ID from async context |
SpanOptions
SpanEndOptions
Configuration Reference
TheinitTracing() function accepts a TracingConfig object:
| Property | Type | Default | Description |
|---|---|---|---|
apiKey | string | — (required) | MutagenT API key for authentication |
endpoint | string | https://api.mutagent.io | API endpoint URL |
environment | string | — | Environment name (e.g., production, staging) |
batchSize | number | 10 | Number of spans to buffer before flushing |
flushIntervalMs | number | 5000 | Flush interval in milliseconds |
debug | boolean | false | Log span start/end events to console |
source | string | sdk | Source identifier for traces |
Lifecycle Functions
| Function | Description |
|---|---|
initTracing(config) | Initialize tracing. Idempotent: calling again replaces the previous configuration (shuts down the old collector first). |
shutdownTracing() | Flush remaining spans, clear timers, and reset singleton state. Returns a Promise<void>. |
isTracingInitialized() | Check whether tracing has been initialized. Returns boolean. |
Type Reference
SpanKind
Taxonomy aligned with OTel Gen AI semantic conventions (v1.37+):| Category | Values |
|---|---|
| Generation | llm.chat, llm.completion, llm.embedding |
| Orchestration | chain, agent, graph, node, edge, workflow, middleware |
| Tools | tool |
| Retrieval | retrieval, rerank |
| Safety | guardrail |
| Other | custom |
SpanIO
Structured input/output for spans:| Field | Type | Description |
|---|---|---|
messages | Array<{ role, content, name?, toolCallId? }> | Chat messages (most common for LLM spans). Roles: system, user, assistant, tool |
toolCalls | Array<{ id, name, arguments, result? }> | Tool calls for LLM spans requesting tool use |
documents | Array<{ content, metadata?, score? }> | Retrieved documents (for retrieval spans) |
text | string | Raw text (for completions, generic spans) |
raw | unknown | Framework-specific data (escape hatch) |
SpanStatus
'ok' | 'error' | 'unset'
SpanMetrics
| Field | Type | Description |
|---|---|---|
model | string | Model identifier (e.g., gpt-4o, claude-sonnet-4) |
provider | string | Provider name (e.g., openai, anthropic) |
inputTokens | number | Input token count |
outputTokens | number | Output token count |
totalTokens | number | Total token count |
costUsd | number | Estimated cost in USD |
latencyMs | number | Total latency in milliseconds |
ttftMs | number | Time to first token in milliseconds |
SpanEvent
| Field | Type | Description |
|---|---|---|
name | string | Event name |
timestamp | Date | When the event occurred (auto-set by addEvent) |
attributes | Record<string, unknown> | Optional event attributes |
MutagentSpan
Internal mutable span representation (returned bystartSpan):
| Field | Type | Description |
|---|---|---|
id | string | Unique span ID |
traceId | string | Trace ID this span belongs to |
parentSpanId | string | undefined | Parent span ID (if nested) |
kind | SpanKind | Span kind |
name | string | Span name |
startedAt | Date | When the span started |
input | SpanIO | undefined | Structured input |
output | SpanIO | undefined | Structured output |
status | SpanStatus | Current status |
statusMessage | string | undefined | Error/status message |
metrics | SpanMetrics | Token counts, cost, etc. |
attributes | Record<string, unknown> | Custom attributes |
events | SpanEvent[] | Lifecycle events |
FinishedSpan
Immutable span ready for serialization and transport:| Field | Type | Description |
|---|---|---|
spanId | string | Unique span ID |
traceId | string | Trace ID |
parentSpanId | string | undefined | Parent span ID |
name | string | Span name |
kind | SpanKind | Span kind |
startTime | string | ISO 8601 start time |
endTime | string | ISO 8601 end time |
durationMs | number | Duration in milliseconds |
status | SpanStatus | Final status |
statusMessage | string | undefined | Error/status message |
model | string | undefined | Model used |
provider | string | undefined | Provider name |
inputTokens | number | undefined | Input token count |
outputTokens | number | undefined | Output token count |
totalTokens | number | undefined | Total token count |
costUsd | number | undefined | Estimated cost |
Exports Summary
All exports from@mutagent/sdk/tracing:
Next Steps
Tracing Overview
Concepts, span hierarchy, and tracing architecture
Tracing API Reference
Full server-side API reference for ingesting and querying traces
Integrations
Framework adapters with automatic tracing (recommended for most users)
TypeScript SDK Setup
Install and configure the TypeScript SDK