Sub topic 1
Why LLM apps and agents need tracing
Prerequisites
This chapter assumes you have:
- Basic Python, including functions and context managers.
- Experience calling an LLM API such as a chat completion endpoint.
- A general idea of what logs and metrics are.
- Basic understanding of HTTP and running a local service.
You do not need to install anything yet. Later chapters set up the OpenTelemetry SDK.
What goes wrong without visibility
A single user question to an LLM application often triggers many steps. The app may retrieve documents, call a model, run a tool, feed the result back to the model, and repeat. If the final answer is slow, wrong, or expensive, the question is which step caused it.
Logs and metrics each answer part of that question, but neither answers it fully:
| Signal | What it tells you | What it misses |
|---|---|---|
| Logs | Individual events with messages | Which events belonged to the same request, and how long each step took relative to the others |
| Metrics | Aggregates such as p95 latency or tokens per minute | Why one particular request was slow or costly |
| Traces | The ordered, timed steps of one request, with parent-child links | Long-term trends, unless you also record metrics |
A trace is the record of one request as it moves through your system. It is made of spans, and each span is one timed step. The next chapter defines both terms precisely.
Why LLM apps are hard to debug
LLM applications have properties that make tracing more useful than in a typical web service:
- Non-deterministic output. The same input can produce different results, so you cannot always reproduce a failure. A recorded trace preserves what happened.
- Variable latency. A model call can take 300 ms or 30 s depending on output length and load.
- Cost tied to usage. Spend depends on input and output tokens, which differ per call.
- Loops. An agent decides at run time how many steps to take. A bug can cause it to call the same tool repeatedly.
- Silent failures. A tool can return an error, the model can work around it, and the user sees a plausible but wrong answer.
What a trace of an agent run shows
The diagram shows one agent run as a tree of timed steps.
From a trace like this you can read off the answers directly:
- Which step took the most time.
- Whether the tool call failed and the model continued anyway.
- How many model calls the run made.
- How many tokens each call used.
Why OpenTelemetry
OpenTelemetry is an open standard and set of libraries for producing traces, metrics, and logs. You instrument your code once and can send the data to many backends. It also defines GenAI semantic conventions, which are agreed attribute names for model calls, tools, and agents. Using those names keeps your traces readable by any compatible tool.
What you will build
Over the following chapters you will set up the Python SDK, export spans through a Collector, instrument model and tool calls, and structure an agent run as a span tree. You will also decide what prompt content to capture and how to control trace volume.
Exercise
A user reports that your agent "sometimes takes a minute to answer." Your metrics show a healthy average latency. Why is a trace more useful than the metric here, and what would you look for in it?
Show answer
An average hides rare slow requests, and it cannot show which step was slow. A trace of one slow request shows each step's duration and parent-child relationships. You would look for a long model call, a slow or failed tool call, or many repeated model and tool spans that indicate an agent loop.