Skip to main content

Span Class

The Span class represents a specific operation within a trace. Spans are the building blocks of observability, tracking individual steps like LLM calls, tool executions, database queries, or any custom operation in your LLM pipeline.

Overview

A span captures:
  • Operation details: name, timing, status
  • Content: input/output data for different span types (Model, Tool, Retrieval, etc.)
  • Metadata: tags, attributes for filtering and analysis
  • Hierarchy: parent-child relationships for nested operations
  • Evaluation: optional evaluator execution
Common span types:
  • Model - LLM inference calls
  • ModelStream - Streaming LLM responses
  • Tool - Function/API executions
  • Retrieval - RAG and vector searches
  • Embeddings - Embedding generation
  • Function - Custom application logic
  • Guardrail - Safety/compliance checks
  • Other - Any custom operation

Creation

Create spans from a trace or parent span:

Properties

span

The underlying span request object containing all span data. Structure:

Methods

logSpan()

Create a nested child span under this span.
This allows you to create hierarchical span relationships like:

Parameters

options
object
required
Same parameters as Trace.logSpan(). See Trace.logSpan() for details.

Returns

span
Span
A new child Span instance linked to this parent span.

Example

update()

Update span metadata and content.

Parameters

updates
object
required

Returns

Returns this for method chaining.

Examples

end()

Mark the span as complete and ready to be flushed.

Behavior

  1. Sets endedAt timestamp
  2. Marks the span as ready in the monitor’s buffer
  3. Recursively ends all child spans
  4. Returns the span’s reference ID
Always call end() on your spans! Spans that are never ended will never be flushed to the API.

Returns

referenceId
string | undefined
The span’s reference ID for correlation.

Example

Span Content Types

Different content types for different operations. See LogSpanContent Type for complete documentation.

Model (LLM Inference)

For standard LLM completions (LogSpanModelContent).
The variables field is typed as LogSpanVariable | null in the SDK. LogSpanVariable is a record type, so you can pass a plain object as shown above.

ModelStream (Streaming LLM)

For streaming LLM responses.

Tool (Function/API Call)

For tool executions and API calls.
For document retrieval and vector searches.

Embeddings

For embedding generation.

Function (Custom Logic)

For custom application logic.

Guardrail (Safety Check)

For safety and compliance checks.

Other (Custom)

For any other operation type.

Complete Examples

Example 1: LLM Call with Deployment

Example 2: Nested RAG Pipeline

Example 3: Guardrail Check

Type Definitions

Best Practices

1. Use Appropriate Content Types

2. Always End Spans

3. Use Attributes and Tags for Filtering