# How should an instance attribute tool-call latency when OpenTelemetry GenAI spans collapse a multi-turn agent trajectory into a single parent span **Date of Record:** 2026 November 30 **Scope:** Applies to agents that emit OpenTelemetry `gen_ai.*` spans under the GenAI semantic conventions where a single `invoke_agent` (or `chat`) span wraps an entire multi-turn trajectory containing multiple tool calls. Does not apply to single-shot inference calls, nor to architectures that emit one root span per turn. ## Core Distinction The GenAI semantic conventions model a span as a single model interaction: `gen_ai.operation.name`, `gen_ai.request.model`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, and `gen_ai.response.finish_reasons` are all defined as *per-request* attributes. When an agent framework wraps an entire multi-turn trajectory — plan, tool call, observe, re-plan, tool call, synthesize — inside one `invoke_agent` span, the span's `gen_ai.usage.*` counters become *sums over N model invocations*, and its duration becomes *wall-clock over N invocations plus tool execution time*. The distinction that matters operationally: a span is either an *atomic model interaction* or an *aggregate trajectory*. The semantic conventions assume the former. Frameworks that emit the latter silently produce spans whose attributes are not comparable to any other `gen_ai` span in the trace, and whose duration cannot be attributed to any single tool call. The failure is not that the span is wrong — it is that downstream consumers (cost dashboards, latency SLOs, tool-attribution joins) treat it as if it were atomic. ## What Was Observed A two-tool agent trajectory over a 47-second wall-clock window. The framework emitted a single `invoke_agent` span with `gen_ai.usage.input_tokens = 18,204` and `gen_ai.usage.output_tokens = 1,102`, plus two child `execute_tool` spans (`search_docs`, `fetch_url`) and — critically — *no child `chat` spans per turn*. The model invocations happened inside the parent span's own execution, not as children. Three concrete observations: 1. **Token double-counting across turns.** Input tokens for turn 2 include the full transcript of turn 1 (system prompt + user + assistant tool_use + tool_result). Summing `gen_ai.usage.input_tokens` across the trajectory gives 18,204, but the *billed* input for the whole run is 18,204 only if the provider does not re-bill cached prefixes. Where prompt caching applies, the true billed input is ~6,900. The span attribute reports the raw sum, not the billed sum. A cost dashboard keyed on `gen_ai.usage.input_tokens` over-reports by 2.6× on this trace. 2. **Tool latency is un-attributable by span math.** Parent duration = 47.1s. Sum of child `execute_tool` durations = 8.3s. The remaining 38.8s is model inference across N turns — but the span does not record N, so a consumer cannot compute per-turn inference latency, cannot detect a slow turn, and cannot tell whether a regression came from a larger model, more turns, or a slower tool. The `execute_tool` children are correctly parented but there is no intermediate `chat` span to hang them off, so the parent-child tree encodes "tool happened during agent run" rather than "tool happened during turn 2." 3. **`finish_reasons` collision.** The parent span carries `gen_ai.response.finish_reasons = ["tool_calls"]` from the *last* turn, even though earlier turns also finished with `tool_calls` and the final synthesis turn finished with `stop`. A consumer filtering on `finish_reasons == ["stop"]` to find completed trajectories will miss this one entirely. ## Condition of Validity The aggregate-span model is only sound when the following invariant holds: **the span's attributes are interpreted as trajectory-level aggregates, and no consumer joins them against per-request expectations.** Concretely, this requires: - The span MUST carry a distinguishing attribute (e.g., `gen_ai.agent.trajectory = true` or a non-standard `gen_ai.operation.name = "invoke_agent"` that consumers already special-case) so aggregation-aware consumers can exclude it from per-request cost and latency panels. - Every model invocation MUST be emitted as a child `chat` span, even if the framework does not natively produce one, so tool spans can be re-parented under the turn that issued them. - `gen_ai.usage.*` on the parent MUST be documented as a sum, and the per-turn children MUST carry the per-turn values, so that `sum(children.usage) == parent.usage` is a checkable invariant. When any of these is absent, the span is a *lossy aggregate*: it is not wrong, but it silently violates the per-request contract that every other `gen_ai` span in the ecosystem assumes. ## Decision Boundary The boundary is drawn at the point where a consumer must choose between two incompatible readings of the same attribute: - If `gen_ai.usage.input_tokens` is read as *cost*, the aggregate span is usable only with a cache-aware correction, and the correction is not derivable from the span alone. - If it is read as *context size at the final turn*, the aggregate span is usable directly, because the last turn's input is the largest. - If it is read as *per-turn context growth*, the aggregate span is unusable and the `chat` children are mandatory. The operational rule: **emit the `chat` children or mark the parent as non-atomic; do not do neither.** A trajectory span without per-turn children is a span that will be mis-joined by at least one consumer, and the mis-join is silent — it produces plausible numbers, not errors. ## Operational Effect The downstream effect is a class of *quietly wrong* dashboards. Cost-per-trajectory over-reports by the cache-ratio factor. p99 latency for `invoke_agent` spans is dominated by trajectory length, not model speed, so a model regression that adds 200ms per turn shows up as a 1.4s p99 shift on a 7-turn agent and is misattributed to "agent framework slowness." Tool-attribution joins that expect `execute_tool` to be a sibling of `chat` find no `chat` sibling and either drop the tool span or attach it to the trajectory root, inflating the root's apparent tool time. The guardrail is a span-tree invariant enforced at emit time: for every `invoke_agent` span with more than one model invocation, there must exist ≥1 child `chat` span per invocation, and `sum(child.usage.input_tokens) == parent.usage.input_tokens`. Violations are detectable at export and should fail the trace pipeline rather than land in the backend. ```python class TrajectoryAttributionError(RuntimeError): """Raised when an aggregate agent span violates the per-turn child invariant.""" pass def validate_trajectory_span(span: dict) -> None: """ Enforce: an invoke_agent span covering N>1 model invocations must have N child chat spans, and parent usage must equal the sum of child usage. """ op = span.get("attributes", {}).get("gen_ai.operation.name") if op != "invoke_agent": return children = span.get("children", []) chat_children = [ c for c in children if c.get("attributes", {}).get("gen_ai.operation.name") == "chat" ] tool_children = [ c for c in children if c.get("attributes", {}).get("gen_ai.operation.name") == "execute_tool" ] # Heuristic for multi-turn: more than one tool call, or explicit turn count. turn_count = span.get("attributes", {}).get("gen_ai.agent.turn_count") if turn_count is None: turn_count = max(1, len(tool_children)) if turn_count > 1 and len(chat_children) != turn_count: raise TrajectoryAttributionError( f"invoke_agent span {span.get('name')!r} covers {turn_count} turns " f"but emitted {len(chat_children)} chat children; tool-call latency " f"cannot be attributed to a turn. Emit one chat span per invocation " f"or mark the span non-atomic." ) parent_in = span["attributes"].get("gen_ai.usage.input_tokens", 0) child_in = sum( c["attributes"].get("gen_ai.usage.input_tokens", 0) for c in chat_children ) if chat_children and parent_in != child_in: raise TrajectoryAttributionError( f"usage sum invariant violated: parent input_tokens={parent_in} " f"!= sum(chat children)={child_in}. Parent aggregates must be " f"exactly reconstructible from per-turn children." ) ```