# In what scenarios must an instance reject a GenAI agent span whose tool-call attributes were re-parented across a model invocation boundary **Date of Record:** 2026 November 21 **Scope:** Applies to OTel-instrumented LLM agent runtimes that emit `gen_ai.*` spans where a single logical agent turn contains one or more `gen_ai.tool.call` child spans nested under a `gen_ai.chat`/model-invocation span. Does not apply to single-shot completion calls with no tool loop, nor to purely trace-only deployments where span parentage is not consumed by a downstream verifier. ## Core Distinction The failure here is not "a span was dropped" or "an attribute was missing." It is a **parentage/attribute coherence violation**: the OpenTelemetry GenAI semantic conventions bind certain attributes to a span *by virtue of what that span represents*. Specifically, `gen_ai.operation.name`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, and the tool-call attributes (`gen_ai.tool.name`, `gen_ai.tool.call.id`) carry different cardinality and different ownership depending on whether they sit on a model-invocation span or a tool-execution span. The non-obvious boundary: an agent runtime that executes its tool loop *inside* a single model-invocation span (the common "one `chat` span wraps the whole ReAct loop" pattern) produces a trace that is **structurally valid OTel** but **semantically invalid GenAI**. The tool-call spans get re-parented under the model span, and the model span's `gen_ai.usage.*` token counters get mutated by every tool round-trip. Downstream metric normalization — which assumes `gen_ai.usage.*` is a per-invocation, monotonic, single-writer quantity — then double-counts or under-counts tokens, and any auditor replaying the trace to reconstruct "how many model invocations happened" gets the wrong answer. ## What Was Observed A ReAct-style agent was instrumented with a single `gen_ai.chat` span opened at the start of the turn and closed after the final answer. Inside it, the loop: 1. calls the model (`gen_ai.operation.name = "chat"`), 2. reads `tool_calls` from the response, 3. opens a `gen_ai.tool.call` span per tool, executes it, closes it, 4. appends the tool result to messages and calls the model again, 5. repeats until `finish_reason = "stop"`. The instrumentation added `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` to the *outer* span, **accumulating** across iterations (`span.set_attribute("gen_ai.usage.input_tokens", total)` on each round). Observed trace shape (abbreviated): ``` gen_ai.chat [op=chat, usage.input_tokens=18400, usage.output_tokens=2100] ├── gen_ai.tool.call [tool.name=search, tool.call.id=call_a1] ├── gen_ai.tool.call [tool.name=read_file, tool.call.id=call_b2] ├── gen_ai.tool.call [tool.name=search, tool.call.id=call_c3] └── gen_ai.tool.call [tool.name=calc, tool.call.id=call_d4] ``` Three distinct breakages surfaced, only one of which was visible in the OTel collector's own validation: - **Token double-counting.** A metrics pipeline that sums `gen_ai.usage.input_tokens` per `gen_ai.chat` span reported 18,400 for a turn that actually consumed ~4,600 input tokens across four invocations. The accumulator was single-writer (the outer span), so it *looked* correct per the semantic convention's "attributes on the span" rule, but it violated the convention's implicit invariant that usage is per-invocation. The collector accepted it; the dashboard was wrong by 4×. - **Tool-call cardinality collapse.** Because all four tool spans shared one parent, a verifier that counted `gen_ai.tool.call` children of a `gen_ai.chat` span to infer "how many model invocations did this turn take" inferred 1 instead of 4. `gen_ai.tool.call.id` values were unique and correct; the *nesting* lied about the loop structure. - **Re-parenting on retry.** When the third model invocation was retried after a 429, the retry logic closed and reopened the outer span. The already-emitted tool spans for `call_c3` were now orphans whose `parent_span_id` pointed at a span that no longer existed in the exporter's batch. The collector dropped them silently under its default `parent_not_found` handling. ## Condition of Validity The GenAI semantic conventions are coherent **only when the span that owns `gen_ai.usage.*` and `gen_ai.operation.name = "chat"` corresponds to exactly one model invocation**. The invariant is: > A `gen_ai.chat` span is a 1:1 record of a single request/response exchange with a model endpoint. Its `gen_ai.usage.*` attributes are the usage reported by that exchange, written once, never accumulated. Tool calls made *as a consequence* of that exchange are siblings at the same level as the *next* `gen_ai.chat` span, not children of the one that requested them. Under this invariant the correct shape for the same turn is: ``` agent.turn (or gen_ai.agent span) ├── gen_ai.chat [usage.input_tokens=1200, output_tokens=310] │ └── gen_ai.tool.call [tool.name=search, tool.call.id=call_a1] ├── gen_ai.chat [usage.input_tokens=1180, output_tokens=290] │ └── gen_ai.tool.call [tool.name=read_file, tool.call.id=call_b2] ├── gen_ai.chat [usage.input_tokens=1120, output_tokens=540] │ ├── gen_ai.tool.call [tool.name=search, tool.call.id=call_c3] │ └── gen_ai.tool.call [tool.name=calc, tool.call.id=call_d4] └── gen_ai.chat [usage.input_tokens=1100, output_tokens=960] ``` Each `gen_ai.chat` owns its own usage; the tool call *requested by* an invocation is its child (the tool belongs to the exchange that emitted the `tool_call`), and the *next* invocation is a sibling. This is the boundary the convention encodes but does not enforce — OTel has no schema rule preventing accumulation, and no rule preventing a tool span from being nested under an unrelated model span. Validity therefore depends on the instrumenter, not the collector. A span is conditionally valid GenAI only if: (a) `gen_ai.usage.*` was written by exactly one invocation and never re-set, (b) every `gen_ai.tool.call` span's parent is the `gen_ai.chat` span whose response contained its `tool_call.id`, and (c) the span lifetime does not span a retry of the underlying invocation. ## Decision Boundary An instance must **reject** (not merely flag) a GenAI span when the following is detectable at export or ingest time: - `gen_ai.usage.input_tokens` or `output_tokens` was set more than once on the same span (monotonicity violation — accumulation), **or** - a `gen_ai.tool.call` span's `parent_span_id` resolves to a `gen_ai.chat` span whose `gen_ai.usage.output_tokens` is inconsistent with having produced that tool call (e.g., the tool call id is absent from any recorded `tool_calls` payload for that span), **or** - a `gen_ai.chat` span has more than one `gen_ai.tool.call` child *and* its `finish_reason` is not `tool_calls` (the loop was collapsed). The instance must **accept** (with a warning, not rejection) when a `gen_ai.chat` span has zero tool children but `finish_reason = "tool_calls"` — this is a legitimate case where the tool span was emitted by a different process/instance and parentage is reconstructed later; the convention permits late attachment as long as `tool.call.id` matches. The boundary is *not* "any span with unusual nesting." A `gen_ai.agent` span legitimately parents many `gen_ai.chat` spans. The rejection trigger is specifically the violation of the 1:1 model-invocation invariant, because that is the only invariant whose violation silently corrupts the normalized metric. ## Operational Effect When the invariant is enforced, three things become true that were not before: 1. **Token metrics normalize correctly.** Summing `gen_ai.usage.input_tokens` over `gen_ai.chat` spans equals actual billed input tokens, because each span is single-writer. The 4× overcount disappears without any downstream dedup logic. 2. **Loop depth is recoverable from trace structure.** Counting `gen_ai.chat` siblings under an agent span yields the true number of model invocations, which is the quantity auditors replay to verify that a turn's cost matches its reasoning depth. 3. **Retry orphaning is contained.** Because the model span closes at the end of each invocation, a retry opens a *new* span; previously-emitted tool spans keep a live parent. The `parent_not_found` silent-drop class is eliminated for this shape. The cost is instrumentation complexity: the agent runtime must open and close a span per loop iteration rather than per turn, and must not use a single accumulator. Runtimes that cannot restructure their loop must instead emit an explicit `gen_ai.agent` wrapper span to absorb the turn-level usage, and leave `gen_ai.chat` spans strictly per-invocation — a workaround that preserves the metric invariant at the cost of an extra span per turn. ```python from dataclasses import dataclass, field class GenAISpanInvariantError(RuntimeError): """Raised when a GenAI span violates the 1:1 model-invocation invariant.""" @dataclass class GenAISpan: span_id: str parent_span_id: str | None operation_name: str # "chat" | "tool.call" | "agent" usage_input_tokens: int | None = None usage_output_tokens: int | None = None usage_write_count: int = 0 # increments each set_attribute tool_name: str | None = None tool_call_id: str | None = None finish_reason: str | None = None children: list["GenAISpan"] = field(default_factory=list) def validate_genai_span(span: GenAISpan) -> None: """Enforce the per-invocation usage invariant and tool parentage coherence.""" # Invariant 1: usage attributes are single-writer (no accumulation). if span.operation_name == "chat" and span.usage_write_count > 1: raise GenAISpanInvariantError( f"span {span.span_id}: gen_ai.usage.* written " f"{span.usage_write_count} times; usage must be set once per " f"model invocation, not accumulated across a tool loop" ) # Invariant 2: a chat span's tool children must be the calls it emitted. if span.operation_name == "chat": tool_children = [c for c in span.children if c.operation_name == "tool.call"] if len(tool_children) > 1 and span.finish_reason != "tool_calls": raise GenAISpanInvariantError( f"span {span.span_id}: {len(tool_children)} tool.call children " f"but finish_reason={span.finish_reason!r}; loop was collapsed " f"into a single model-invocation span" ) for child in tool_children: if not child.tool_call_id: raise GenAISpanInvariantError( f"tool span {child.span_id} under {span.span_id} missing " f"gen_ai.tool.call.id; cannot verify parentage" ) # Invariant 3: no tool span may outlive a re-opened parent (retry orphan). for child in span.children: if child.parent_span_id != span.span_id: raise GenAISpanInvariantError( f"tool span {child.span_id} claims parent " f"{child.parent_span_id!r} but is nested under {span.span_id}; " f"re-parented across a retry boundary" ) def normalize_turn_usage(turn_spans: list[GenAISpan]) -> int: """Sum input tokens over per-invocation chat spans; reject accumulated spans.""" total = 0 for span in turn_spans: validate_genai_span(span) if span.operation_name == "chat": total += span.usage_input_tokens or 0 return total ```