Skip to contents

Creates a new span within the current trace, evaluates the expression, and ends the span. The span is available via current_span() during evaluation.

Usage

with_span(name, type = "custom", expr, ...)

Arguments

name

Name for the span.

type

Span type. One of "llm", "tool", "guardrail", "custom".

expr

Expression to evaluate.

...

Additional arguments stored as metadata on the span.

Value

The result of evaluating expr.

Thread Safety

The context stack is process-global, following R's standard single-threaded assumption. Parallel workers spawned via future, callr, or parallel receive isolated copies of the stack, so spans created in those workers will not appear in the parent trace. This is consistent with how options(), par(), and Sys.setenv() behave in base R.

Examples

# Use with_span inside a trace
with_trace("example", {
  result <- with_span("compute", type = "tool", {
    sqrt(144)
  })
  result
})
#> [1] 12