Tracing Setup
This guide covers how to initialize, configure, and shut down the MutagenT tracing system in both TypeScript and Python applications.Quick Start
initTracing() Configuration
The initTracing() function accepts a TracingConfig object. Call it once at application startup before any traced operations.
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
apiKey | string | Required | Your MutagenT API key for authentication. |
endpoint | string | "https://api.mutagent.io" | The MutagenT API endpoint URL. |
environment | string | undefined | Environment label attached to all traces (e.g., "production", "staging", "development"). |
batchSize | number | 10 | Number of spans to buffer before triggering an automatic flush. |
flushIntervalMs | number | 5000 | Maximum time in milliseconds between automatic flushes. |
debug | boolean | false | When enabled, logs span start/end events to the console for debugging. |
source | string | "sdk" | Source identifier attached to traces. Integration packages override this automatically. Valid values: "sdk", "langchain", "langgraph", "vercel-ai", "openai", "otel", "manual". |
The Python SDK uses snake_case parameter names (
api_key, batch_size, flush_interval_ms). The TypeScript SDK uses camelCase (apiKey, batchSize, flushIntervalMs).Environment Variables
You can provide your API key and endpoint via environment variables instead of passing them directly:Lazy Initialization
In TypeScript, if you set theMUTAGENT_API_KEY environment variable, you can skip calling initTracing() entirely. The SDK will automatically initialize tracing on the first startSpan() call using the environment variable.
- Only attempts once per process lifecycle (controlled by an internal
lazyInitAttemptedflag) - Reads
process.env["MUTAGENT_API_KEY"]at the time of the first span creation - Uses the default endpoint (
https://api.mutagent.io) and default batch settings - Resets when
shutdownTracing()is called, allowing re-initialization
Lazy initialization is convenient for development and simple scripts. For production applications, prefer calling
initTracing() explicitly so you can configure the environment, batchSize, and debug options.Shutdown and Graceful Cleanup
Always callshutdownTracing() before your process exits. This ensures all buffered spans are flushed to the MutagenT API.
What shutdownTracing() Does
- Stops the background flush timer
- Flushes all remaining buffered spans to the API
- Clears all active span state (removes all spans from the
SpanManager) - Resets the tracing system completely, including the lazy initialization flag (can be re-initialized with a new
initTracing()call or via lazy init fromMUTAGENT_API_KEY)
Idempotency and Re-initialization
In TypeScript, callinginitTracing() a second time silently shuts down the previous configuration and replaces it with the new one. This is useful for reconfiguring tracing in tests.
In Python, calling init_tracing() a second time logs a warning and is a no-op. Call shutdown_tracing() first, then init_tracing() again to reconfigure.
Integration with Framework Packages
If you are using a MutagenT integration package (e.g.,@mutagent/langchain, @mutagent/langgraph, @mutagent/openai, @mutagent/vercel-ai, @mutagent/mastra), the integration handles tracing initialization internally. You typically do not need to call initTracing() yourself — the integration configures it when you create its callback handler or wrapper.
However, if you want to customize tracing configuration (e.g., change batch size or enable debug mode), you can call initTracing() before creating the integration handler, and the integration will use your existing configuration.
Batch Collection Behavior
The tracing system buffers spans in memory and sends them in batches to minimize network overhead. Understanding the flush behavior helps with tuning:| Trigger | Behavior |
|---|---|
Buffer reaches batchSize | Immediate async flush |
flushIntervalMs timer fires | Flush whatever is in the buffer |
shutdownTracing() called | Force flush all remaining spans |
traceId in the HTTP payload, so spans belonging to the same trace are always sent together (within a single batch).
Retry Behavior
Failed flush attempts are retried with exponential backoff:- Max retries: 3 (configurable internally via
BatchCollectorConfig) - Base delay: 1000ms (doubles each retry: 1s, 2s, 4s)
- HTTP timeout: 10 seconds per request (via
AbortController) - If all retries fail, the spans in that batch are dropped and a warning is logged in debug mode
Checking Initialization Status
You can check whether tracing has been initialized:Troubleshooting
Spans are not appearing in the dashboard
Spans are not appearing in the dashboard
- Verify your API key is correct and has write permissions
- Enable
debug: truein your tracing config to see span start/end logs in the console - Make sure you call
shutdownTracing()before your process exits — buffered spans may not have been flushed - Check that the
endpointis reachable from your environment
initTracing throws 'apiKey is required'
initTracing throws 'apiKey is required'
The
apiKey field is required and cannot be empty. Make sure your environment variable is set:Duplicate or missing parent-child links
Duplicate or missing parent-child links
Context propagation relies on
AsyncLocalStorage (TypeScript) or contextvars (Python). If you are using worker threads, forked processes, or certain test runners that reset async context, parent-child linking may not work as expected. Use the parentSpanId option to manually link spans in these cases.High memory usage from span buffering
High memory usage from span buffering
If your application produces a very high volume of spans, reduce
batchSize to flush more frequently, or reduce flushIntervalMs to flush on a shorter timer. The default settings (10 spans / 5 seconds) are designed for moderate throughput.Next Steps
Tracing API Reference
Learn about decorators, wrappers, and low-level span primitives.
Tracing Overview
Understand core concepts, SpanKind taxonomy, and architecture.