# Under what conditions must an instance reject an OpenTelemetry GenAI span whose `gen_ai.usage.input_tokens` was inherited from a parent agent span rather than measured at the invocation boundary **Date of Record:** 2026 November 28 **Scope:** Applies to any agent runtime that emits OpenTelemetry GenAI semantic-convention spans (`gen_ai.*`) where nested agent/tool invocations may share a parent span or propagate usage attributes downward. Does not apply to single-shot, single-span model calls with no parent-child span nesting. ## Core Distinction There are two structurally different things that can carry a token count on a GenAI span, and they are routinely conflated by agent frameworks: 1. **Measured usage** — a count obtained from the model provider's response envelope (`usage.input_tokens`, `usage.prompt_tokens`, `usage.cache_read_input_tokens`, etc.) at the exact invocation boundary, attributable to exactly one request/response pair. 2. **Inherited usage** — a count copied from an ancestor span (or from a shared context carrier) into a descendant span because the framework's tracing wrapper defaults to "propagate parent attributes on child creation." The GenAI semantic conventions define `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as *per-invocation* counters. They are additive across sibling spans under a parent only if every span's value is measured at its own boundary. The moment a descendant span inherits a parent's usage value, the span tree's token totals become double-counted, and — more subtly — the descendant span's *cost attribution* becomes a lie about which model call consumed the budget. The non-obvious failure is not the double-count. The double-count is loud and usually caught by a dashboards sanity check. The non-obvious failure is that an **inherited usage value makes a descendant span pass its own budget guard**, because the guard checks `gen_ai.usage.input_tokens > threshold` and the inherited value is always large (it is the parent's full budget), so the guard either always fires or never fires depending on comparison direction — and either way it is measuring the wrong request. ## What Was Observed A planner agent (span `agent.plan`, model `gpt-class-A`) dispatches three sequential sub-agent invocations (`agent.subtask[0..2]`, model `gpt-class-B`) under a shared parent trace. The tracing wrapper used was configured with `propagate_parent_attributes=True` to ensure `gen_ai.conversation.id` and `gen_ai.system` flowed down. The attribute filter was a substring match on `gen_ai.` — so `gen_ai.usage.input_tokens` flowed down too. Trace of subtask[1], the second sub-agent call, which is where the boundary broke: ``` span agent.plan gen_ai.usage.input_tokens=18420 (measured, planner call) span agent.subtask[0] gen_ai.usage.input_tokens=18420 (INHERITED — actual was 3102) span agent.subtask[1] gen_ai.usage.input_tokens=18420 (INHERITED — actual was 2877) span tool.search gen_ai.usage.input_tokens=18420 (INHERITED — actual was 0, not a model call) span agent.subtask[2] gen_ai.usage.input_tokens=18420 (INHERITED — actual was 3390) ``` Three distinct downstream effects were observed, in increasing order of subtlety: **Effect 1 — Aggregate overcount.** The trace's summed `gen_ai.usage.input_tokens` was 5×18420 = 92,100 versus a true 27,789. The cost dashboard for this tenant showed a 3.3× overcount. This was detected within a day. **Effect 2 — Budget guard mis-fire on a non-model span.** The `tool.search` span carried `gen_ai.usage.input_tokens=18420` despite making zero model calls. A downstream cost-attribution job that grouped by `gen_ai.operation.name` and summed usage per operation reported `execute_tool` consuming 18,420 input tokens. This is a category error: tool spans cannot consume input tokens. The job's invariant `usage.input_tokens > 0 ⇒ operation.name ∈ {chat, text_completion, embeddings}` was violated, but no assertion existed to catch it, so the bad rows flowed into a monthly chargeback report. **Effect 3 — The subtask[1] budget guard passed when it should have failed.** Subtask[1]'s actual measured input was 2,877 tokens against a per-subtask soft limit of 3,000. It was *under* budget. But the runtime's guard read the span attribute at emit time, which was 18,420, and the guard's logic was `if attr > limit: downgrade_model()`. So subtask[1] was downgraded from `gpt-class-B` to `gpt-class-C` mid-execution — not because it was over budget, but because it *inherited the parent's over-budget value*. The downgrade changed the model mid-conversation, invalidating the prompt cache and producing a reasoning discontinuity that cost more than the original overage would have. This third effect is the one that matters. It is the case where an inherited token count does not just corrupt telemetry — it corrupts *execution*, because the token count was treated as a control signal rather than a measurement. ## Condition of Validity A `gen_ai.usage.*` attribute on a span is valid as a control signal if and only if all three hold: 1. **Boundary provenance.** The value was written by the code path that received the provider response, not by an attribute-inheritance mechanism. In OpenTelemetry terms: the attribute must be set via `span.set_attribute(...)` on the span that directly wraps the provider SDK call, and must not be present in the span's initial attribute map at creation time. 2. **Operation compatibility.** The span's `gen_ai.operation.name` is one that can consume tokens (`chat`, `text_completion`, `embeddings`, `generate_content`). A span with `gen_ai.operation.name=execute_tool` or `invoke_agent` (in the sense of the agent orchestration span, not the model call) must not carry a nonzero `gen_ai.usage.input_tokens` unless that span *itself* made a model call. 3. **Non-inheritance witness.** The span carries evidence that its usage was measured, not inherited. The conventions do not define such an attribute, which is precisely the gap. The workable convention is a companion attribute — `gen_ai.usage.source` with values `measured` | `inherited` | `estimated` — set by the instrumentation layer. Absent that, the only reliable witness is structural: the span is a direct child of a `gen_ai.operation.name=chat` span *and* the parent's usage differs from the child's. When any of these fails, the value is a measurement of some *other* invocation and must be excluded from budget guards, cost attribution, and aggregate totals. ## Decision Boundary The decision an instrumentation or guard layer must make is: **on span emission, do I trust `gen_ai.usage.input_tokens` as the local invocation's count?** The boundary is crossed when the attribute was present at span creation (inherited) rather than set after the provider response (measured). The detection is mechanical and cheap: - Capture the span's attribute snapshot at creation time. - On end, compare the final attribute map against the creation snapshot. - If `gen_ai.usage.input_tokens` appears in the creation snapshot with a nonzero value, it was inherited. Reject it for control purposes; keep it only as a trace-annotation for debugging, clearly marked. A subtler variant: some frameworks set usage at creation from a *pre-flight estimate* (a tokenizer count of the prompt) and then overwrite it with the measured value on response. If the response never arrives (timeout, cancellation), the span ends with the estimate still in place. The estimate is not inherited, but it is also not measured, and it will pass a budget guard as if it were. The guard must therefore also check that the span has a terminal status of `OK` and a response event before trusting the value. A cancelled span with `gen_ai.usage.input_tokens` set is an estimate and must not feed the guard. The invariant to enforce: **for any span S, `S.gen_ai.usage.input_tokens` is admissible as a control signal iff (a) the value was absent at S creation, (b) S.gen_ai.operation.name ∈ token-consuming operations, and (c) S.status.code == OK with a response event recorded.** Any span failing (a)–(c) has its usage attribute quarantined: retained on the span for observability, excluded from guards, aggregates, and chargeback. ## Operational Effect Enforcing the invariant changes three behaviors: 1. **Budget guards become per-invocation.** The subtask[1] downgrade in the observed trace would not have fired, because the inherited value fails condition (a) and the guard falls back to the measured 2,877, which is under the 3,000 limit. The model stays `gpt-class-B`, the cache survives, and the reasoning discontinuity does not occur. 2. **Aggregates become additive.** With inheritance excluded, sibling spans under a parent sum correctly: 18,420 + 3,102 + 2,877 + 0 + 3,390 = 27,789, matching ground truth. The 3.3× overcount disappears without any change to the provider integration. 3. **Tool spans stop lying.** The `execute_tool` span's usage attribute is quarantined because `execute_tool` is not a token-consuming operation (condition b). The chargeback job's invariant `usage.input_tokens > 0 ⇒ operation.name ∈ {chat, text_completion, embeddings}` now holds, and the assertion, if added, would have caught the original leak. The residual cost is that the instrumentation layer must snapshot attributes at span creation and compare at end — a small per-span allocation. This is the price of distinguishing measured from inherited, and it is cheaper than the alternative, which is discovering the corruption downstream in a chargeback dispute or, worse, in a model downgrade that silently degrades an agent's reasoning mid-task. ```python from dataclasses import dataclass, field from enum import Enum from typing import Any class UsageAdmissibilityError(RuntimeError): """Raised when a gen_ai.usage.* attribute cannot be trusted as a control signal.""" class UsageSource(Enum): MEASURED = "measured" INHERITED = "inherited" ESTIMATED = "estimated" UNKNOWN = "unknown" TOKEN_CONSUMING_OPS = {"chat", "text_completion", "embeddings", "generate_content"} @dataclass class SpanSnapshot: name: str attributes_at_creation: dict[str, Any] operation_name: str status_code: str = "UNSET" has_response_event: bool = False final_attributes: dict[str, Any] = field(default_factory=dict) def classify_usage_source(snap: SpanSnapshot) -> UsageSource: """Determine whether gen_ai.usage.input_tokens on this span is measured or inherited.""" key = "gen_ai.usage.input_tokens" creation_value = snap.attributes_at_creation.get(key) final_value = snap.final_attributes.get(key) if final_value is None: return UsageSource.UNKNOWN if creation_value is not None and creation_value == final_value: # Present at creation with the same value at end: never measured at boundary. return UsageSource.INHERITED if creation_value is not None and creation_value != final_value: # Pre-flight estimate overwritten by a measured value — measured if OK. if snap.status_code == "OK" and snap.has_response_event: return UsageSource.MEASURED return UsageSource.ESTIMATED # Absent at creation, present at end: written by the provider-response path. if snap.status_code == "OK" and snap.has_response_event: return UsageSource.MEASURED return UsageSource.ESTIMATED def admissible_input_tokens(snap: SpanSnapshot) -> int: """Return the token count admissible for budget guards, or raise if not admissible.""" source = classify_usage_source(snap) if source is UsageSource.INHERITED: raise UsageAdmissibilityError( f"span {snap.name!r}: gen_ai.usage.input_tokens was inherited from an " f"ancestor span and must not drive control decisions" ) if source is UsageSource.ESTIMATED: raise UsageAdmissibilityError( f"span {snap.name!r}: gen_ai.usage.input_tokens is an estimate " f"(status={snap.status_code}, response_event={snap.has_response_event}); " f"cannot be trusted for budget enforcement" ) if source is UsageSource.UNKNOWN: raise UsageAdmissibilityError( f"span {snap.name!r}: no gen_ai.usage.input_tokens present" ) if snap.operation_name not in TOKEN_CONSUMING_OPS: raise UsageAdmissibilityError( f"span {snap.name!r}: operation {snap.operation_name!r} cannot consume " f"input tokens, yet carries a measured usage value" ) return int(snap.final_attributes["gen_ai.usage.input_tokens"]) def enforce_budget(snap: SpanSnapshot, limit: int) -> None: """Budget guard that refuses to act on inherited or estimated usage.""" try: used = admissible_input_tokens(snap) except UsageAdmissibilityError: # Quarantine: do not downgrade, do not aggregate. Let the call proceed # under the measured value once the response arrives. return if used > limit: raise UsageAdmissibilityError( f"span {snap.name!r}: measured input_tokens={used} exceeds limit={limit}" ) ```