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.
Step 2 of 5
retrieve span
Record the query, the k value and the scores as attributes, so a bad answer can be traced back to what was fetched.
Step 3 of 5
call_llm span
Record the model, token count and latency. Cost derived from tokens becomes a number you can sum across traces.
Step 4 of 5
tool span
Each tool call gets its own span. A failure is recorded as an event with the exception, so the error sits on the step that caused it.
Step 5 of 5
Export trace
A processor ships finished spans to a backend such as Jaeger or LangSmith, where you view the nested timeline.
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
~137 tokens
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.