Tracing and Observability for AI Systems › From Logs to Traces: Why Flat Records Are Not Enough
Language AI HandbookAll chapters Traditional application logging emits flat lines: a timestamp, a level, a message. For a request-scoped system, flat logs fail in a specific way. If your agent issues three tool calls and two model calls, the log lines are interleaved with lines from other requests, and reconstructing which lines belong to a single user request requires a correlation ID that you then have to join manually. Worse, timing relationships (which call blocked on which) are absent, so you cannot tell whether the 4-second latency came from the model, the retriever, or a serialized tool chain that never overlapped.
It is worth being precise about why the correlation ID alone is insufficient. A correlation ID lets you group lines into a request, but it does not tell you the structure within the request. Given five lines tagged with the same request ID, you still do not know which line is the parent of which, whether any of them ran concurrently, or which one the request was waiting on when it stalled. You have recovered the set of operations but lost the shape of the computation. Structure is exactly what you need to localize a bug, because a bug is almost always at a specific edge of the execution graph: the retriever returned the wrong document to the generator, the tool returned a malformed result to the planner, the reranker reordered candidates before truncation. Only a representation that preserves those edges shows you the bug.
Within the trace, a span’s parent pointer records the operation that created or directly caused it. Start and end times show which recorded intervals overlap. Sibling operations can still depend on each other, so parent pointers alone do not identify the operations that blocked progress.
The contrast is easiest to see with two small interval schedules. In the serial case, each step waits for the previous one, so the request's wall-clock time is the sum of the step durations. In the parallel case, two independent steps share a time window, so the wall-clock time is driven by the longer one plus whatever must still follow. We compute both schedules from fixed numbers so the comparison is reproducible.
PythonIn [3]Expand
serial_steps = [
("embed", 0, 35),
("retrieve_a", 35, 435),
("retrieve_b", 435, 735),
("merge", 735, 755),
("generate", 755, 1555),
]
parallel_steps = [
("embed", 0, 35),
("retrieve_a", 35, 435),
("retrieve_b", 35, 335),
("merge", 435, 455),
("generate", 455, 1255),
]
serial_total_ms = max(end for _, _, end in serial_steps)
serial_sum_ms = sum(end - start for _, start, end in serial_steps)
parallel_total_ms = max(end for _, _, end in parallel_steps)
parallel_sum_ms = sum(end - start for _, start, end in parallel_steps)
parallel_saving_ms = serial_total_ms - parallel_total_ms
assert serial_sum_ms == parallel_sum_ms == 1555
assert parallel_total_ms == 1255 and parallel_saving_ms == 300
serial_labels = [f"serial: {name}" for name, _, _ in serial_steps]
serial_lefts = [start for _, start, _ in serial_steps]
serial_widths = [end - start for _, start, end in serial_steps]
parallel_labels = [f"parallel: {name}" for name, _, _ in parallel_steps]
parallel_lefts = [start for _, start, _ in parallel_steps]
parallel_widths = [end - start for _, start, end in parallel_steps]ConsoleOut [4]Expand
Serial wall-clock (ms): 1555 Serial sum of durations (ms): 1555 Parallel wall-clock (ms): 1255 Parallel sum of durations (ms): 1555 Wall-clock saved by overlap (ms): 300
Both schedules execute the same five operations. In the parallel schedule, the two retrieval calls start at 35 ms, and the merge waits for both to finish. The later retrieval ends at 435 ms. The merge and generation then finish at 1,255 ms, which is 300 ms earlier than the serial schedule.
VisualizationOut [5]Expand

The example uses its own Python records. The OpenTelemetry Tracing API defines span context and links. Links can connect spans in the same trace or different traces, for example when a queued batch depends on multiple originating requests. Under W3C Trace Context, the transmitted parent-id identifies the caller's current operation. Instrumentation uses real trace and span IDs when it injects context. The example identifiers are local labels.
The anatomy of a span
Before writing code, fix the conceptual distinctions. A span is a timed operation, not a message. It has:
- Identity:
trace_id(shared by all spans in one request),span_id(unique),parent_span_id(null for the root). - Timing: start and end timestamps for placement in the trace. For elapsed time within one process, use a monotonic clock. Compare timestamps across hosts only with adequate clock alignment and an understanding of clock error.
- Operation category: what type of work the local demo records. Standard OpenTelemetry span kinds represent relationships such as client and server. Common kinds are
chain(control flow),llm(model call),retriever,embedding,tool, andreranker. - Attributes: a dictionary of string keys to values under a versioned schema. The example accepts Python structures, while an exporter must map them to its supported attribute types. Token counts, model names, document IDs, and scores live here.
- Status: a recorded outcome. The demo uses success/error labels. OpenTelemetry has a distinct unset state, and exporter rules govern status mapping. A failed span should record the error without leaking sensitive exception text.
- Events: optional timestamped points within a span (for example, "first token received" for a streaming generation), which we will mention but keep out of the minimal implementation.
Each field supports a different query. Use IDs to join spans, timing to compare intervals, categories to group similar work, and outcome codes to count errors. Span events can mark a point inside an operation, such as the first response token, without making it a separate timed span.
In this example, attributes describe operations and payloads hold content. Either can contain personal information. A tenant ID or document ID can reveal sensitive relationships even when the raw text is absent. Audit both categories before allowing a field into telemetry.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!