By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A teammate recorded a Waypoint request as a list of raw events and asked you to turn it into the trace an on-call engineer would want. The raw run contains the gateway, one retrieval call, one model call and one pending approval. Your trace must answer: how long did the request take, which span was slowest, how many input tokens were spent and what is the approval state?
pending is a value, not an absence.import json
fake_run = {
"request_id": "req_lab30",
"prompt_version": "wp-update@v8",
"events": [
{"span": "gateway", "start": 0, "end": 2100, "attrs": {"tenant": "northline"}},
{
"span": "retrieval",
"start": 30,
"end": 280,
"attrs": {"policy_version": "northline-policy@v3"},
},
{
"span": "model",
"start": 300,
"end": 1900,
"attrs": {"input_tokens": 1180, "output_tokens": 240},
},
{
"span": "approval",
"start": 1950,
"end": 2100,
"attrs": {"action": "propose_reroute", "decision": "pending"},
},
],
}
def build_trace(run: dict) -> dict:
spans = run["events"]
start = min(span["start"] for span in spans)
end = max(span["end"] for span in spans)
# The gateway span is the request envelope; compare dependency spans only.
dependencies = [span for span in spans if span["span"] != "gateway"]
slowest = max(dependencies, key=lambda span: span["end"] - span["start"])
return {
"request_id": run["request_id"],
"prompt_version": run["prompt_version"],
"duration_ms": end - start,
"slowest_span": slowest["span"],
"span_count": len(spans),
"total_input_tokens": sum(
span["attrs"].get("input_tokens", 0) for span in spans
),
"approvals": [
span["attrs"]["decision"]
for span in spans
if span["span"] == "approval"
],
}
print(json.dumps(build_trace(fake_run), indent=2, sort_keys=True))pending is exactly the state an auditor asks about.Key takeaways
Build traces from the request envelope, not by summing span durations.
Name the slowest span and collect approval state as first-class trace data.
Sum token usage per span and keep raw prompt text out of the trace.
A trace is done when it answers the next on-call question without a deploy.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.