Skip to main content

Trace Class

The Trace class represents a high-level operation or workflow in your LLM application. Traces capture the entire lifecycle of a request, batch job, or user interaction, and can contain multiple child spans for granular tracking.

Overview

A trace is the top-level unit of observability that represents:
  • A single user request to your API
  • A background job or workflow
  • A conversation turn in a chatbot
  • A complete RAG pipeline execution
  • Any end-to-end operation you want to track
Traces contain:
  • Metadata: name, status, timestamps, tags, attributes
  • Context: session ID, reference ID
  • Children: one or more spans representing sub-operations

Creation

Create a trace using the Monitor.logTrace() method:

Properties

trace

The underlying trace request object that will be sent to the API. Contains all trace metadata. Structure:

traceId

The server-assigned trace ID, set after the trace is successfully flushed to the API.

Methods

logSpan()

Create a child span within this trace.

Parameters

options
object
required

Returns

span
Span
A new Span instance. See Span Class for details.

Examples

update()

Update trace metadata in place.

Parameters

updates
object
required
Partial updates to apply to the trace.

Returns

Returns this for method chaining.

Examples

end()

Mark the trace as complete and ready to be flushed.

Behavior

  1. Sets endedAt timestamp if not already set
  2. Marks the trace as ready in the monitor’s buffer
  3. Recursively ends all child spans (and their descendants)
  4. Returns the trace’s reference ID
Always call end() on your traces! Traces that are never ended will never be flushed to the API.

Returns

referenceId
string | undefined
The trace’s reference ID for correlation with external systems.

Examples

Complete Examples

Example 1: Simple API Request

Example 2: Multi-Step Workflow

Example 3: Nested Operations (RAG Pipeline)

Example 4: Long-Running Background Job

Type Definitions

Best Practices

1. Always Use Try-Finally

2. Use Meaningful Names

3. Add Context with Attributes

4. Update Status Based on Outcome

Common Patterns

Pattern 1: Request Handler

Pattern 2: Background Job