Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 11.3 — Tracing LLM Calls
LLM Engineering · Example 2 of 2

Trace context across two services, and self time

The traceparent header is version-traceid-spanid-flags. Here the ids are counters, so the zero padding is easy to see.

A trace shows one request as a tree of timed steps: retrieval, model calls, tools. It answers where the time went and links the services a request crosses. OpenTelemetry is the standard way to produce traces.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write self_times(spans) for spans given as (id, parent, name, start, end) that returns a list of "name self_ms" strings in input order. Self time is total minus the total of direct children.

02

Write parse_traceparent(header) that returns (trace_id, span_id) as integers, and raises ValueError if the header does not have exactly four parts or the trace id is all zeros.

Explain it without notes

01

What is a span, and how does a set of spans become a trace?

02

What is the difference between a span's total time and its self time, and why does it matter?

03

How does a second service join the trace of the first one?

04

What should and should not go into span attributes of a model call?

Trace context across two services, and self time
Sign in to run this example in your browser.

Expected output

header sent:   00-00000000000000000000000000000007-0000000000000001-01
worker joined trace 7 as span 2, child of span 1
gateway: total 900 ms, self 400 ms
worker: total 500 ms, self 500 ms