Skip to content
All labs
Advanced50 minProduction AI systems

Add tracing and observability

Instrument an LLM app with OpenTelemetry traces for every LLM call, tool call and retrieval. See your system in production.

How one request becomes a trace of nested spans

Step 1 of 5

Root span

The request opens a parent span. Every step below it hangs from this one, which is how a tree appears in the trace viewer.

Attributes make spans queryable, so you can ask which calls were slow or expensive.

Scenario

Your LLM app is a black box in production. Add tracing so you can see every step, latency and token cost.

Objective

Instrument call_llm, retrieve and tool_call with spans. Export to a trace backend.

Starter code
Implement the TODOs to complete the lab.
tracing.py
from opentelemetry import trace

tracer = trace.get_tracer("llm-app")

@tracer.start_as_current_span("call_llm")
def call_llm(messages, model="<small-model>"):  # Model names change often; check your provider's model list (as of 28 October 2026). Replace <...-model> with a model id from that list.
    # TODO: add attributes (model, token_count, latency)
    # TODO: add event on error
    pass

@tracer.start_as_current_span("retrieve")
def retrieve(query, k=5):
    # TODO: add attributes (query, k, scores)
    pass
Solution hints
  • 1Use span.set_attribute for queryable fields
  • 2Add token_count and cost as attributes
  • 3Use span.add_event for errors
  • 4Record exception on failure
Validation steps
Your implementation should pass these checks.
  • Trace shows nested spans for call_llm, retrieve, tool
  • Each span has model, latency, token_count attributes
  • Errors are recorded as events
  • Trace exports to backend (Jaeger/LangSmith)

Run validation

This is a simulated validation environment. In production, this would run your code against the validation steps.