Part 3 of 12 · 2 min read
A roll-up sums an attribute across a whole subtree; self time isolates one span's own cost
A span's own duration counts time its children also claim, so the useful numbers are either the sum across everything underneath it, or what's left over once the children are subtracted out.
Think of a company's total expense report versus one department's own discretionary spending. The total includes every sub-team's budget. The department's own number is what is left after subtracting what its sub-teams spent.
In plain words: a roll-up sums an attribute, such as tokens or cost, over an entire span's subtree, which is how a single run's total cost is computed from many separate model-call attributes, while self time is a span's own duration minus its children's durations, which is what isolates the cost of a step itself rather than the work it delegated to things underneath it.
A worked example. The example run's cost is computed by rolling up gen_ai.usage.input_tokens and gen_ai.usage.output_tokens separately across the whole trace -- 5,700 input tokens summed across all three chat spans -- then applying an assumed price of $3 per million input tokens and $15 per million output tokens, giving $0.027 total. Separately, the root invoke_agent span's own self time (its 3.28-second duration minus the combined duration of its five children) is near zero, because nearly all of its time is spent inside the children it delegates to, which is exactly what you'd expect from a span whose entire job is orchestration.
The key lines. From code/mini-tracing/tracing.py:
# from code/mini-tracing/tracing.py
def rollup(span, key):
return sum(s.attrs.get(key, 0) for s in walk(span))
def cost(span):
return rollup(span, "gen_ai.usage.input_tokens") * PRICE_IN + rollup(span, "gen_ai.usage.output_tokens") * PRICE_OUT
def self_time(span):
"""Duration not covered by (sequential) child spans."""
return span.duration - sum(c.duration for c in span.children)
Read it as: rollup walks the entire subtree and sums one attribute, while self_time only ever looks at one span's immediate children. The docstring's own parenthetical, "(sequential)," is the honest caveat. This subtraction is only correct when children ran one after another, not in parallel. That is the limitation the module's own exercises ask you to fix.
Self time, roll-up, critical path
Self time is a span's duration minus its children's. A roll-up sums an attribute (tokens, cost) over a span's subtree. The critical path is the chain of spans that determines the total duration.
Example: self_time silently assuming sequential children is a real limitation stated by the module itself -- for two overlapping tool calls, the correct "what actually drove the total duration" question needs the critical path, the single chain of spans whose combined duration explains the root's duration, not a flat subtraction.