Span Class
Span Class
Details
Represents a single operation within a trace, such as an LLM call, tool execution, guardrail check, or custom operation.
Public fields
nameName of the span.
span_idUnique identifier for the span.
typeType of operation: "llm", "tool", "guardrail", or "custom".
parent_idID of the parent span, if any.
metadataArbitrary metadata attached to the span.
statusCurrent status: "running", "ok", or "error".
input_tokensNumber of input tokens recorded.
output_tokensNumber of output tokens recorded.
modelModel name used for this span (if LLM).
Methods
Method new()
Create a new span.
Method end()
Record the end time and set status.
Examples
# Create a span for an LLM call
span <- Span$new("gpt-call", type = "llm")
span$start()
span$set_model("gpt-4o")
span$set_tokens(input = 500L, output = 200L)
span$add_metric("latency", 1.23, unit = "seconds")
# Add an event
evt <- trace_event("prompt_sent", data = list(length = 42L))
span$add_event(evt)
span$end()
span$status
#> [1] "ok"
span$duration()
#> [1] 0.003454685
span$to_list()
#> $span_id
#> [1] "c6be45eee35844e7"
#>
#> $name
#> [1] "gpt-call"
#>
#> $type
#> [1] "llm"
#>
#> $status
#> [1] "ok"
#>
#> $parent_id
#> NULL
#>
#> $metadata
#> list()
#>
#> $attributes
#> list()
#>
#> $start_time
#> [1] "2026-03-10T16:23:38.139Z"
#>
#> $end_time
#> [1] "2026-03-10T16:23:38.143Z"
#>
#> $duration_secs
#> [1] 0.003454685
#>
#> $input_tokens
#> [1] 500
#>
#> $output_tokens
#> [1] 200
#>
#> $model
#> [1] "gpt-4o"
#>
#> $error
#> NULL
#>
#> $events
#> $events[[1]]
#> $events[[1]]$name
#> [1] "prompt_sent"
#>
#> $events[[1]]$data
#> $events[[1]]$data$length
#> [1] 42
#>
#>
#> $events[[1]]$timestamp
#> [1] "2026-03-10T16:23:38.142Z"
#>
#>
#>
#> $metrics
#> $metrics[[1]]
#> $metrics[[1]]$name
#> [1] "latency"
#>
#> $metrics[[1]]$value
#> [1] 1.23
#>
#> $metrics[[1]]$unit
#> [1] "seconds"
#>
#>
#>