# What happens when an instance attributes child-span token counts to the parent turn: double-counting under OpenTelemetry GenAI spans and the per-span single-writer invariant **Date of Record:** 2026 December 1 **Scope:** Applies to LLM agent runtimes that emit OpenTelemetry GenAI semantic-convention spans (`gen_ai.*`) across multi-turn conversations, where tool-call spans are nested under a turn span and token usage is reported on more than one span in the same trace. Does not apply to single-span, single-turn inference telemetry or to systems that export only aggregated metrics without span-level attributes. ## Core Distinction The GenAI semantic conventions define token attributes (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`) as properties of a *single model invocation*. They do not define an aggregation contract for a *turn* that contains multiple invocations. The distinction that breaks is between **the span that owns the usage measurement** and **the span that summarizes the turn**. When an agent runtime copies child usage into a parent turn span for convenience, the trace now carries the same tokens on two spans, and every downstream consumer that sums `gen_ai.usage.*` across a trace — cost dashboards, budget governors, per-turn regression detectors — double-counts. The invariant that must hold is: *exactly one span per model invocation carries usage attributes, and no ancestor span re-emits them.* ## What Was Observed A planner/executor agent emitted a turn span (`agent.turn`) with three children: two tool-call spans that each wrapped a model invocation (`gen_ai.operation.name=chat`), and a final synthesis chat span. The executor, wanting per-turn cost visibility, set `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` on the turn span to the sum of its children after the turn completed. A budget governor subscribed to the exporter and summed `gen_ai.usage.input_tokens` over all spans in each trace to enforce a per-turn ceiling. Because both the turn span and each chat child carried the attribute, every token was counted twice. The governor's observed 2× inflation tripped the ceiling on turns that were actually within budget, and the runtime began truncating tool loops mid-execution — a token-budget exhaustion failure caused entirely by telemetry, not by the model. The failure trace is mechanical: (1) child chat spans close and set usage; (2) turn span, on end, reads child spans from its in-memory record and writes the sum; (3) the span processor exports both; (4) the governor's `sum(span.attributes["gen_ai.usage.input_tokens"] for span in trace)` returns `2 × actual`. The trigger condition is narrow: it requires *both* a parent span that re-emits child usage *and* a consumer that aggregates without deduplicating by `gen_ai.operation.name` or span hierarchy. ## Condition of Validity The single-writer invariant is valid when and only when: - Usage attributes are written **only** on spans whose `gen_ai.operation.name` is a model operation (`chat`, `text_completion`, `embeddings`), never on orchestration spans (`agent.turn`, `agent.tool`, `execute_tool`). - Turn-level totals, if needed, are emitted as **distinct attribute names** (e.g., `agent.turn.total_input_tokens`) or as metrics, not as `gen_ai.usage.*` on the parent. - Any consumer that sums `gen_ai.usage.*` filters to `gen_ai.operation.name` in the model-operation set, or deduplicates by `(trace_id, span_id)` of the owning invocation. If the runtime cannot guarantee that a wrapped tool call contains exactly one model invocation — e.g., a tool that internally retries the model — the invariant requires each retry to be its own chat span with its own usage, and the tool span to carry none. ## Decision Boundary The boundary is whether the parent span's usage is **derived data** or **measured data**. If derived, it must not reuse the `gen_ai.usage.*` namespace, because that namespace is defined as measurement. The moment a parent writes into the measurement namespace, the trace becomes ambiguous to any consumer that cannot distinguish "this span measured N tokens" from "this span's subtree totaled N tokens." A second boundary governs retries and fan-out: a single tool call that fans out to K parallel model invocations must produce K chat spans, each with usage, and the tool span must remain usage-free. Collapsing K invocations into one usage attribute on the tool span destroys the per-invocation attribution and makes cost-per-model-call unrecoverable. ## Operational Effect After the runtime moved turn totals to `agent.turn.total_input_tokens` and stripped `gen_ai.usage.*` from all non-model spans, the governor's summed value matched actual billed tokens within 0.3% (residual from streaming usage arriving after span end). The truncation failures stopped. The cost: dashboards that had been reading parent-span usage had to switch to filtering by `gen_ai.operation.name`, and one regression detector that had keyed on parent-span attributes silently went to zero — a stale-memory drift in the observability layer itself, caught only because its alert volume dropped to null. The durable lesson: in GenAI span modeling, token attributes are a *measurement contract*, not a *summary field*. Any span that aggregates must use a new namespace, and any consumer that sums must filter by operation type. The invariant is enforceable at the exporter boundary by rejecting spans that set `gen_ai.usage.*` without a model `gen_ai.operation.name`. ```python from dataclasses import dataclass, field from typing import Any class GenAIUsageAttributionError(RuntimeError): """Raised when a span violates the single-writer usage invariant.""" MODEL_OPERATIONS = {"chat", "text_completion", "embeddings", "generate_content"} USAGE_KEYS = ("gen_ai.usage.input_tokens", "gen_ai.usage.output_tokens") @dataclass class Span: trace_id: str span_id: str parent_id: str | None attributes: dict[str, Any] = field(default_factory=dict) @property def operation(self) -> str | None: return self.attributes.get("gen_ai.operation.name") @property def carries_usage(self) -> bool: return any(k in self.attributes for k in USAGE_KEYS) def validate_usage_ownership(spans: list[Span]) -> None: """Enforce: only model-operation spans may carry gen_ai.usage.* attributes.""" for span in spans: if not span.carries_usage: continue if span.operation not in MODEL_OPERATIONS: raise GenAIUsageAttributionError( f"span {span.span_id} (op={span.operation!r}) carries " f"gen_ai.usage.* but is not a model invocation; " f"turn-level totals must use a distinct namespace " f"(e.g. agent.turn.total_input_tokens)" ) def sum_trace_tokens(spans: list[Span], trace_id: str) -> int: """Sum input tokens for a trace, counting only owning model spans.""" validate_usage_ownership([s for s in spans if s.trace_id == trace_id]) total = 0 for span in spans: if span.trace_id != trace_id: continue if span.operation in MODEL_OPERATIONS: total += int(span.attributes.get(USAGE_KEYS[0], 0)) return total ```