Sub topic 1
Why LLM apps and agents need tracing
This chapter explains why logs and metrics alone fall short for LLM applications, what a trace adds, and what you will build over the rest of the tutorial. It contains no setup steps. Those start in later chapters.
Prerequisites
This chapter assumes you have the following:
- Basic Python, including functions and context managers (the
withstatement). - Experience calling an LLM API, such as a chat completion endpoint.
- A general idea of what logs and metrics are.
- A basic understanding of HTTP and of running a local service.
You do not need to know OpenTelemetry yet. The next chapter defines its core terms.
What makes LLM apps hard to debug
A traditional web service is mostly deterministic. The same request usually follows the same code path, takes a similar amount of time, and returns a similar result. When it fails, a stack trace and a log line usually point to the cause.
An LLM application breaks these assumptions in several ways:
- Variable latency. One model call can take 400 ms or 20 seconds, depending on output length, load, and provider queueing.
- Variable cost. You pay per token. A longer prompt or a longer answer changes the bill for that single request.
- Non-deterministic output. The same input can produce different text. A "successful" HTTP 200 response can still contain a wrong or useless answer.
- Many steps per request. A single user question can trigger retrieval, several model calls, and several tool calls.
- Control flow chosen at run time. In an agent, the model decides which tool to call next and when to stop. The code path differs from request to request.
Ordinary monitoring tells you that something is slow or expensive. It rarely tells you which step, in which request, and why.
Where logs and metrics fall short
Logs and metrics remain useful. Each answers a different question.
| Signal | Good at | Weak point for LLM apps |
|---|---|---|
| Logs | Recording what happened at one moment, with detail | Lines from one request are scattered and hard to group. Ordering and timing between steps are implicit. |
| Metrics | Showing aggregate trends such as p95 latency or tokens per minute | They hide individual requests. A spike in latency does not say which step caused it. |
| Traces | Showing the steps of one request, in order, with timing and nesting | They cost more to store at high volume. Sampling is usually needed. |
A concrete example shows the gap. A support agent takes 38 seconds to answer one question. A latency metric shows that the request was slow. A log file shows dozens of lines from many concurrent users. Neither shows that the agent called the same search tool six times, and that each call was followed by a model call that re-read a growing conversation.
A trace shows this directly, as a tree of timed steps for that one request.
What a trace gives you
A trace is the record of one request as it moves through your system. A span is one timed operation inside that trace, such as a model call or a tool call. Spans can nest, so a trace forms a tree. The next chapter covers this model in detail. For now, you only need the picture.
The diagram below shows one agent request as a tree of spans.
From this one picture you can already answer questions that logs and metrics cannot:
- Which step took the most time? Here, the second model call took 6.0 of 9.8 seconds.
- How many model calls did one request make?
- Did a tool call fail, and what happened afterward?
- Did the agent loop more times than expected?
The questions tracing answers
Tracing LLM apps is useful for four kinds of problems.
Latency
Each span has a start time and a duration, so you can see where time goes. In LLM apps the time is often not in your code. It is in the model call, in a slow tool, or in the sum of many small steps. A trace separates these cases.
Cost
Providers report how many input and output tokens each call used. If you record those counts on each model-call span, you can see which requests are expensive and why. Later chapters show how to record them. A trace can reveal that a large share of the cost comes from one step, such as a retrieval step that stuffs too many documents into the prompt.
Failures
Failures in LLM apps are often partial. A tool call times out, the agent retries, and the final answer still comes back. Without a trace, the request looks healthy. With a trace, you see the failed span, the retry, and the extra time and tokens it cost. Later chapters cover how to record errors and retries.
Behavior
Agents decide their own path. A trace is a record of the path that a given request actually took. This helps you find runaway loops, where an agent calls tools repeatedly without making progress. It also helps you check whether the agent used the tools you expected.
Why OpenTelemetry
You could write your own timing code, or use a vendor SDK. This tutorial uses OpenTelemetry (often shortened to OTel), an open standard and set of libraries for producing traces, metrics, and logs. It fits LLM work for three reasons:
- Vendor neutral. Your instrumentation exports data in a standard format, OTLP, so you can change the backend that stores and displays traces without rewriting your code.
- Shared vocabulary. OpenTelemetry defines GenAI semantic conventions, which are agreed names for attributes such as the model name and token counts. Tools that understand them can display your data consistently. Note that these conventions are still evolving, so check the current specification when you implement them.
- One system for the whole request. LLM calls are only part of a request. The same trace can include your web framework, database queries, and HTTP calls to other services, so the model call appears in context.
What you will build
Across the tutorial you will instrument a small support agent. The work follows this order:
- Learn the trace and span model, and how attributes, events, and status describe a span.
- Set up the OpenTelemetry Python SDK and send spans over OTLP to a Collector.
- Wrap a model call in a span that carries GenAI attributes and token counts.
- Structure an agent run as a parent span with child spans for model calls, tool calls, and retrieval.
- Record errors, retries, and timeouts so failures are visible.
- Decide what prompt and response content to capture, and what to redact.
- Control volume and cost with sampling, and read traces to debug agents.
Costs and limits of tracing
Tracing is not free, and it is worth knowing the trade-offs before you start.
- Overhead. Creating spans and exporting them uses some CPU, memory, and network. Batching keeps this small for most applications, but it is not zero.
- Storage volume. An agent request can produce dozens of spans. At high traffic, storing every trace becomes expensive, which is why sampling is a later topic.
- Sensitive data. Prompts and responses can contain personal or confidential information. Recording them in traces spreads that data to more systems. A later chapter covers how to decide what to capture.
- Not a quality measure. A trace tells you what happened and how long it took. It does not tell you whether the answer was good. Evaluating answer quality is a separate practice that can use traces as input.
Summary
LLM apps are variable in time and cost, non-deterministic in output, and made of many steps. Agents also choose their own path at run time. These properties make per-request visibility important. A trace records each request as a tree of timed spans, so you can find slow steps, expensive calls, partial failures, and runaway loops. OpenTelemetry provides a vendor-neutral way to produce those traces. The next chapter defines the trace and span model precisely.
Exercise
A support agent answers one question in 38 seconds. Your latency metric shows a spike, and your logs show many lines from concurrent users. Name two things a trace of that single request could show that the metric and logs do not.
Show answer
A trace could show which step consumed the time, for example one slow model call or a slow tool. It could also show how many times the agent called a tool or the model, such as the same search tool being called six times in a loop, and whether any call failed and was retried. Any two of these are correct, because a trace shows the ordered, nested, timed steps of one request.
Exercise
Why is an HTTP 200 response from an LLM provider not enough to conclude that a request went well? Give one reason related to output and one related to cost.
Show answer
On output, the model can return a fluent but wrong or unhelpful answer, since LLM output is non-deterministic and a 200 only says the call completed. On cost, the call may have used many more tokens than expected, for example from a very long prompt or output, and the status code does not show token usage. A trace with token counts on the span makes this visible.
Exercise
Give one reason to choose OpenTelemetry over writing your own timing code, and one cost of tracing that you should plan for.
Show answer
Reasons include that it is vendor neutral (OTLP lets you change backends without rewriting instrumentation), that GenAI semantic conventions give shared attribute names, and that one trace can cover the model call together with web, database, and HTTP spans. Costs include export overhead, storage volume at high traffic (which sampling addresses), and sensitive prompt or response data spreading to more systems.