# Why does an instance lose its own causal trace when OpenTelemetry GenAI spans re-parent across a tool-call boundary **Date of Record:** 2026 November 30 **Scope:** Applies to agents instrumented with OpenTelemetry GenAI semantic conventions (`gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`) where a parent agent span emits a tool-call child span that itself invokes a nested LLM instance. Does not apply to single-shot, non-nested inference calls or to agents whose tool invocations are pure in-process functions with no downstream model call. ## Core Distinction The failure is not that spans are missing. It is that the *causal chain* an operator reconstructs from `parent_span_id` links diverges from the causal chain the agent actually executed, because the GenAI conventions define span attributes for *what* a tool call did (name, arguments, token counts) but leave the *causal linkage across a nested LLM invocation* underspecified. When a tool call spawns a sub-agent that emits its own `gen_ai.operation.name = "chat"` span, the sub-agent's span is conventionally parented to the tool-call span. That is correct for tracing topology but wrong for token attribution: the sub-agent's `gen_ai.usage.*_tokens` now roll up under a span whose `gen_ai.operation.name` is `execute_tool`, not `chat`, so any cost aggregator that sums tokens by operation name double-counts or silently drops the nested generation. The invariant that breaks is: *every token emitted by a model must be attributable to exactly one `chat` span, and that span's ancestors must not include another token-bearing span on the same accounting path.* ## What Was Observed A planner agent (`gen_ai.operation.name = "invoke_agent"`) issued a tool call to a retrieval sub-agent. The tool-call span carried `gen_ai.tool.name = "delegate_retrieval"` and, following the convention that tool spans may record usage, was stamped with `gen_ai.usage.input_tokens = 812` — the tokens the *planner* consumed constructing the tool arguments. The retrieval sub-agent then ran its own `chat` span with `gen_ai.usage.input_tokens = 4100`, `gen_ai.usage.output_tokens = 220`, parented to the `execute_tool` span. Three observable consequences: 1. **Cost aggregation inflated by the planner's argument-construction tokens.** The cost pipeline summed `gen_ai.usage.*_tokens` over all spans in the trace and got 5132 input tokens, but the true billable input was 4100 (retrieval) + 812 (planner) = 4912, because the 812 appeared both on the tool span *and* was already counted inside the planner's own `chat` span that generated the call. The tool span's usage attributes were a *copy*, not a distinct consumption. 2. **The `chat`-only filter dropped 16.5% of spend.** A dashboard filtering `gen_ai.operation.name == "chat"` to isolate inference cost omitted the 812 tokens the planner spent, because those were recorded on the `invoke_agent` and `execute_tool` spans, not on a `chat` span. 3. **Latency attribution inverted.** The retrieval `chat` span's 2.1s duration was nested inside the `execute_tool` span's 2.3s, which was nested inside a planner `chat` span that had already returned. The trace showed the tool call as the long pole; the actual long pole was the sub-agent's generation, which the parent-child duration math hid because the parent `chat` span had closed before the tool span opened. ## Condition of Validity The divergence is bounded and reproducible only when all of the following hold: - A tool-call span carries `gen_ai.usage.*_tokens` attributes (permitted but not mandated by the conventions), *and* the enclosing agent span that produced those tokens also carries usage attributes for the same generation. - The tool call synchronously invokes a nested model instance that emits its own `gen_ai.operation.name = "chat"` span as a descendant. - The cost/telemetry consumer aggregates by summing usage attributes across the trace without deduplicating on `gen_ai.operation.name` or on span identity. When any of these is absent — e.g., tool spans omit usage attributes, or the sub-agent runs out-of-process with its own trace root — the double-count does not occur and the naive sum is correct. The invariant is therefore *conditional on the nesting topology*, not on the SDK version. ## Decision Boundary The boundary an operator must draw is between **topological parenting** (which span is the structural child) and **accounting parentage** (which span owns the token cost). These coincide for a flat agent but split at every nested-generation tool call. The correct guardrail is a two-part rule enforced at span-emission time: - A span with `gen_ai.operation.name == "execute_tool"` MUST NOT carry `gen_ai.usage.*_tokens` for tokens already recorded on an ancestor `chat`/`invoke_agent` span. If the tool span needs to record its own model consumption (e.g., the tool itself calls a model), it must emit a *child* `chat` span for that consumption rather than stamping usage onto the tool span. - Any cost aggregator MUST deduplicate by `(span_id, gen_ai.operation.name)` and MUST refuse to sum usage attributes across a parent-child pair where both spans carry usage for the same generation, flagging the trace as ambiguous. ## Operational Effect Without the guardrail, the telemetry is internally consistent (every span validates against the schema) yet externally wrong (the trace-level sum overstates cost, the `chat`-filter understates it, and the latency waterfall misattributes the long pole). The failure is silent: no span is malformed, no exporter error fires, and the divergence only surfaces when a human reconciles the dashboard against the provider invoice — at which point the trace is the only artifact and it lies. Enforcing the accounting-parentage rule at emission time converts a silent post-hoc reconciliation failure into a loud span-validation error at the moment the misparented usage attribute is set, which is the only point where the emitting agent still holds the context needed to fix it. ```python class GenAiUsageAttributionError(RuntimeError): """Raised when a tool-call span carries token usage that duplicates an ancestor generation span.""" pass TOKEN_BEARING_OPS = {"chat", "invoke_agent", "embeddings"} def validate_usage_attribution(span, ancestors): """ span: dict with keys 'span_id', 'operation_name', 'usage_input', 'usage_output' ancestors: list of dicts (same shape) forming the parent chain, nearest first """ op = span["operation_name"] has_usage = span.get("usage_input", 0) > 0 or span.get("usage_output", 0) > 0 if op == "execute_tool" and has_usage: for anc in ancestors: if anc["operation_name"] in TOKEN_BEARING_OPS and ( anc.get("usage_input", 0) > 0 or anc.get("usage_output", 0) > 0 ): raise GenAiUsageAttributionError( f"tool span {span['span_id']} (op={op}) records usage " f"({span['usage_input']} in / {span['usage_output']} out) but ancestor " f"{anc['span_id']} (op={anc['operation_name']}) already owns a token-bearing " f"generation on the same accounting path; emit a child 'chat' span instead." ) # A nested chat span under a tool span is legal, but must not share the tool's usage. if op in TOKEN_BEARING_OPS and ancestors: nearest = ancestors[0] if nearest["operation_name"] == "execute_tool" and ( nearest.get("usage_input", 0) > 0 or nearest.get("usage_output", 0) > 0 ): raise GenAiUsageAttributionError( f"chat span {span['span_id']} nested under usage-bearing tool span " f"{nearest['span_id']}; token attribution is ambiguous and will double-count." ) return True def aggregate_cost(spans): """Sum usage only over token-bearing ops, deduplicated by span identity.""" seen = set() total_in = total_out = 0 for s in spans: if s["operation_name"] not in TOKEN_BEARING_OPS: continue key = (s["span_id"], s["operation_name"]) if key in seen: continue seen.add(key) total_in += s.get("usage_input", 0) total_out += s.get("usage_output", 0) return total_in, total_out ```