Module 5 · Evaluation & observability

Distributed tracing with OpenTelemetry and Langfuse

5 min read

A disappointing note comes out of Atlas. Did the planner split the question badly? Did a researcher find the wrong documents? Did the writer ignore a summary? Did the critic approve too quickly? Without traces, there's no way to know: you're reduced to rerunning and hoping. Observability is the prerequisite for any improvement of a multi-agent system.

Logs, metrics, traces

SignalAnswersExample
LogsWhat happened at a given moment?"The critic requested a revision"
MetricsHow much, how often?Average cost per note, revision rate
TracesWhat path did this run follow, and where did the time and money go?The full tree of a run

For an agent system, the trace is the central signal: it reconstructs the decision tree — which agent called which model, with what, for what result and at what cost.

The data model: nested spans

A trace is a tree of spans. Each span represents an operation (a graph node, a model call, a tool call), with a start, a duration, attributes and a parent. Illustrative example (values vary with the question and the model):

trace 4f2a…
├── node.planner                 1.9 s
│   └── llm.extract (opus)        1.8 s   in 312 / out 96 tokens
├── node.researcher ×3 (parallel)
│   └── llm.extract (haiku)       0.7 s   in 1,840 / out 210 tokens
├── node.writer                  4.2 s
│   └── llm.complete (opus)
└── node.critic                  2.1 s
    └── llm.extract (opus)

Atlas's tracing

Atlas includes a minimalist tracer, atlas/tracing.py, deliberately readable. A span is a context manager; the parent is found automatically through a context variable, which also works for nodes running in parallel:

@contextmanager
def span(name: str, **attributes: Any) -> Iterator[Span]:
    parent = _current.get()
    current = Span(
        name=name,
        trace_id=parent.trace_id if parent else uuid.uuid4().hex,
        span_id=uuid.uuid4().hex[:16],
        parent_id=parent.span_id if parent else None,
        attributes=attributes,
    )
    token = _current.set(current)
    ...

Two instrumentation points are enough to cover the whole system:

  • every node, through the @guarded decorator (node.planner, node.researcher…);
  • every model call, in llm.py, with the model used and the actual token usage read from response.usage:
with tracing.span("llm.extract", model=model, schema=schema.__name__) as span:
    response = client().messages.parse(...)
    span.record_usage(model, response.usage)

Centralizing model calls in a single module (lesson 1.1) pays off here: a single change instruments the whole system.

tracing.summary() aggregates a run's spans. Its shape (illustrative values):

{"llm_calls": 8, "input_tokens": …, "output_tokens": …, "cost_usd": …, "errors": []}

The number of calls (8 for a question with 3 sub-questions and one revision) is measured; tokens and cost depend on your question and your documents.

Moving to the standard: OpenTelemetry

A home-made tracer is fine for learning. In production, adopt OpenTelemetry (OTel), the open observability standard: your traces become viewable in any compatible tool (Jaeger, Grafana Tempo, Datadog, Honeycomb…), and most LLM-focused observability platforms, such as Langfuse, accept OpenTelemetry traces.

Atlas's tracer also emits OTel spans when the library is installed (pip install -e ".[otel]") — without changing a single line of the agents:

otel_cm = _tracer.start_as_current_span(name, attributes=_primitive(attributes)) if _tracer else nullcontext()
with otel_cm as otel_span:
    current.otel = otel_span
    yield current

and sets the attributes of OpenTelemetry's GenAI semantic conventions:

self.otel.set_attribute("gen_ai.request.model", model)
self.otel.set_attribute("gen_ai.usage.input_tokens", self.input_tokens)
self.otel.set_attribute("gen_ai.usage.output_tokens", self.output_tokens)

These conventions are still being stabilized: check the attribute names your tool expects.

Configuring export

The application configures once where traces go:

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter

provider = TracerProvider()
provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))  # in production: an OTLP exporter
trace.set_tracer_provider(provider)

In production, replace ConsoleSpanExporter with an OTLP exporter (package opentelemetry-exporter-otlp) pointing to your collector or platform. Verified on Atlas in offline mode: one run produces 16 spans (5 kinds of nodes, 8 model calls), correctly nested in a single trace.

What to trace — and what not to

Always trace:

  • the agent / node name, and the run identifier (thread_id);
  • the model, input and output tokens, computed cost;
  • the duration of each step;
  • routing decisions and their reason (verdict, handoff_to, reason);
  • errors, retries, refusals, guardrail triggers.

Handle with care:

  • content (prompts, responses, documents): essential for debugging, but it often contains personal or confidential data. Decide on an explicit policy: sampling, masking, retention period, restricted access;
  • credentials (API keys, tokens): never in a trace.

The questions a good trace must answer

  1. Why did this run cost 3× the average? → Number of revisions, number of sub-questions, context sizes.
  2. Where does the time go? → Duration per node; is parallelism actually working?
  3. Which agent introduced the error? → Walk up the tree from the faulty output.
  4. Has behavior changed since yesterday? → Compare the distributions of metrics derived from traces.

Key takeaways

  • The trace is the central signal of an agent system: the decision tree, with durations, tokens and costs.
  • Instrument two points: every node, and every model call — hence the value of a centralized LLM module.
  • Adopt OpenTelemetry for portability, with the gen_ai.* attributes.
  • Define an explicit policy for traced content: useful for debugging, sensitive by nature.

Hands-on

  1. Run Atlas offline and export the trace with tracing.export_json("trace.json"). Reconstruct the tree by hand.
  2. Configure a ConsoleSpanExporter and observe the OTel spans emitted.
  3. Add an atlas.verdict attribute holding the decision to the critic's span.

Resources