🤖lauren-ai
← Home
Export this page

Tracing

Observability spans, exporters, and the @traced decorator.

Decorator

traced

Decorate an async function to create a trace span.

The decorator requires parentheses — bare @traced usage raises DecoratorUsageError immediately.

If a global TraceStore has been set via set_trace_store(), the completed Trace is recorded there after each invocation. If a trace is already active in the current async task the new span is attached as a child of the active root span.

Usage:

python
@traced(name="fetch_user", kind=SpanKind.CUSTOM)
async def fetch_user(user_id: str) -> dict:
    ...

Parameters:

NameTypeDescription
namestr | NoneOverride the span name. Defaults to the function name.
kindSpanKindSpan classification. Defaults to SpanKind.CUSTOM.
metadatadict[str, Any] | NoneExtra key-value metadata to attach to the span.

Returns: Callable — A decorator that wraps the target async function with span recording.

Raises:

ExceptionDescription
DecoratorUsageErrorWhen used bare without parentheses.

Span types

SpanKind

Classification of traced operations.

Span

A single traced operation within an agent run.

Parameters:

NameTypeDescription
span_idstrUnique identifier for this span (random hex).
parent_idstr | NoneIdentifier of the parent span, or None for root spans.
namestrHuman-readable name for the operation.
kindSpanKindClassification of the operation.
inputsdict[str, Any]Input arguments recorded at span start.
outputsdict[str, Any] | NoneOutput values recorded at span finish.
errorstr | NoneError message if the operation failed, else None.
started_atfloatMonotonic timestamp when the span started.
ended_atfloat | NoneMonotonic timestamp when the span ended, or None if still open.
metadatadict[str, Any]Arbitrary key-value metadata.

Trace

A hierarchical tree of spans for one agent run.

Parameters:

NameTypeDescription
trace_idstrUnique identifier for this trace (random hex).
run_idstrCorrelation identifier linking this trace to an agent run.
spanslist[Span]Ordered list of all spans belonging to this trace.

Store & config

TraceStore

In-memory store of recent traces, injectable as a DI service.

Usage:

python
store = TraceStore()
# ... traces are added by TracingConfig's exporter ...
recent = await store.last(10)

Parameters:

NameTypeDescription
max_tracesintMaximum number of traces to retain in memory.

TracingConfig

Configuration for tracing in LLMConfig.

Parameters:

NameTypeDescription
enabledboolWhether tracing is active.
exporterAnyA TraceExporter instance, or None to use the default in-memory store.
sample_ratefloatFraction of traces to export (0.0–1.0).
include_inputsboolWhether to record input arguments in spans.
include_outputsboolWhether to record output values in spans.

set_trace_store

Register store as the global destination for completed traces.

Subsequent traced()-decorated calls will export finished traces to all exporters attached to store.

Parameters:

NameTypeDescription
storeTraceStoreThe TraceStore instance to activate.

get_trace_store

Return the currently-registered global TraceStore, or None when none has been set.

Returns: TraceStore | None — The active trace store, or None.

Exporters

TraceExporter

Protocol satisfied by all trace exporter implementations.

InMemoryTraceExporter

Collect traces in memory for testing.

Usage:

python
exporter = InMemoryTraceExporter()
config = TracingConfig(enabled=True, exporter=exporter)
# ... run agent ...
assert len(exporter.traces) == 1

ConsoleTraceExporter

Pretty-print trace trees to stdout.

Usage:

python
exporter = ConsoleTraceExporter()
config = TracingConfig(enabled=True, exporter=exporter)

FileTraceExporter

Write traces as NDJSON to a file.

Usage:

python
exporter = FileTraceExporter("/var/log/traces.ndjson")

Parameters:

NameTypeDescription
pathstr | PathPath to the output NDJSON file. Appended to on each export.