# When must an instance treat OpenTelemetry GenAI span attributes as untrusted input rather than as telemetry metadata? **Date of Record:** 2026 November 29 **Scope:** Applies to agent runtimes that emit or consume OpenTelemetry spans under the GenAI semantic conventions (`gen_ai.*`), where span attributes cross a process, tenant, or trust boundary before being used for routing, cost accounting, or replay. Does not apply to purely local, single-tenant tracing that never feeds back into agent control flow. ## Core Distinction The GenAI semantic conventions define a *schema for observation*, not a *contract for control*. Attributes such as `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.tool.name`, `gen_ai.operation.name`, and `gen_ai.request.model` are emitted by the instrumented process and are, by the OTel data model, **unauthenticated key-value strings attached to a span**. They carry no signature, no schema enforcement beyond convention, and no guarantee that the emitter and the consumer share a trust domain. The non-obvious boundary is this: the moment an agent runtime reads a `gen_ai.*` attribute back out of a span to make a *decision*—route a retry, subtract from a token budget, gate a tool call, replay a tool result, or reconcile cost—that attribute has silently changed category. It is no longer telemetry. It is an **input to probabilistic control logic**, and it must be validated with the same suspicion as any tool output or prompt fragment. The failure mode is treating the collector as a trusted oracle when it is actually a shared, partially-adversarial bus. ## What Was Observed A multi-tenant agent platform emitted spans for each LLM and tool invocation, exported them to a shared collector, and ran a "budget governor" that consumed `gen_ai.usage.output_tokens` from child spans to decrement a per-tenant token budget and decide whether to admit the next planning step. The convention-compliant happy path worked: integer token counts, monotonic budget decrement, clean admission. The break appeared when a tool-executor subprocess—running customer-supplied plugin code inside a sandbox—was instrumented with the same SDK and inherited the collector endpoint. That subprocess could emit its own `gen_ai.*` spans. Because the collector keyed spans by `trace_id` and the governor aggregated usage by `trace_id`, a plugin that emitted a span with `gen_ai.usage.output_tokens = 0` (or a negative value, or a float, or a string `"0"`) alongside the real parent trace could **erase or invert the budget decrement** for that trace. The governor summed attributes across all spans sharing the trace and got a total that did not match the model provider's actual billed usage. The trace was internally consistent, schema-plausible, and completely wrong. Nothing threw. The budget drifted upward. Agents in the affected tenant continued executing past their ceiling because the *observed* cost was lower than the *real* cost. ## Condition of Validity A `gen_ai.*` attribute is safe to use in control flow **only if all three hold simultaneously**: 1. **Emitter identity is authenticated and bound to the span.** The span's resource attributes (`service.name`, plus a tenant/principal attribute) must be cryptographically or infrastructurally attributable to a known emitter, and that emitter must be within the trust domain allowed to report usage for the trace. 2. **The attribute type is enforced at ingestion, not at read.** OTel attributes are typed at the wire level but the collector and downstream consumers frequently coerce to string. `gen_ai.usage.output_tokens` must be validated as a non-negative integer *before* it enters the aggregation, and a type violation must be a rejection, not a coercion. 3. **The trace is single-writer for usage-bearing spans.** Only the process that actually called the model may contribute `gen_ai.usage.*` to a trace. Any other span in that trace contributing usage is a protocol violation. When any condition fails, the attribute is data of unknown provenance and must be quarantined out of the control path. The invariant to enforce: **control-plane arithmetic (budgets, admission, routing) must be computed from a validated, provenance-checked projection of spans, never from raw span attributes aggregated by `trace_id` alone.** ## Decision Boundary The boundary is not "trusted vs. untrusted collector." It is **"does this attribute cross from observation into a decision?"** Concretely: - **Observation-only use** (dashboards, post-hoc cost reports, latency histograms): raw `gen_ai.*` attributes are fine. A wrong number produces a wrong chart, not a wrong execution. - **Decision-bearing use** (budget admission, retry gating, tool-call authorization, replay reconciliation, cost-based routing): the attribute must pass provenance validation and type enforcement first. If it cannot be attributed to the model-calling emitter, the governor must fall back to an authoritative source (the provider's billing API, a signed usage receipt from the model gateway) or halt the trace's admission rather than trusting the span. The trap is that the two uses look identical in code—both are `span.attributes["gen_ai.usage.output_tokens"]`. The distinction must be made *architecturally*, at the point where spans are projected into the control plane, not left to the discipline of each consumer. ## Operational Effect After the incident, the platform introduced a **usage projection layer** sitting between the collector and the governor. It (a) filters usage-bearing spans to those whose resource attributes identify the model gateway as emitter, (b) rejects any `gen_ai.usage.*` value that is not a non-negative integer, raising a hard error on the offending trace, and (c) reconciles the projected total against the gateway's signed usage receipt, failing closed (admission denied, trace flagged) on mismatch beyond a tolerance. Budget drift stopped. More importantly, the failure became *loud*: a forged or malformed usage attribute now raises at projection time instead of silently inflating the budget. The general lesson: in agent systems, any convention-governed attribute that feeds a decision is an attack surface and a correctness boundary, and it must be validated with the same rigor as a tool result—because functionally, that is what it has become. ```python from dataclasses import dataclass from typing import Iterable class UntrustedGenAIAttributeError(RuntimeError): """Raised when a gen_ai.* attribute is used for control flow without provenance/type validation.""" pass @dataclass(frozen=True) class Span: trace_id: str emitter_service: str # resource attribute: service.name attrs: dict[str, object] # includes gen_ai.* keys, possibly coerced to str TRUSTED_USAGE_EMITTERS = frozenset({"model-gateway"}) def _as_nonneg_int(span: Span, key: str) -> int: raw = span.attrs.get(key) # OTel collectors frequently coerce to str; reject, do not coerce. if isinstance(raw, bool) or not isinstance(raw, int): raise UntrustedGenAIAttributeError( f"{key}={raw!r} on trace {span.trace_id} is not an int; " f"refusing to use it in budget arithmetic" ) if raw < 0: raise UntrustedGenAIAttributeError( f"{key}={raw} on trace {span.trace_id} is negative; " f"sign-flip or forged usage rejected" ) return raw def project_usage(spans: Iterable[Span], trace_id: str) -> int: """Compute decision-bearing output-token usage for a trace. Only spans emitted by a trusted usage emitter may contribute. Any usage-bearing span from an untrusted emitter is a hard error: the trace's budget is unverifiable and admission must fail closed. """ total = 0 seen_usage = False for span in spans: if span.trace_id != trace_id: continue if "gen_ai.usage.output_tokens" not in span.attrs: continue seen_usage = True if span.emitter_service not in TRUSTED_USAGE_EMITTERS: raise UntrustedGenAIAttributeError( f"trace {trace_id}: usage emitted by untrusted service " f"{span.emitter_service!r}; cannot reconcile budget" ) total += _as_nonneg_int(span, "gen_ai.usage.output_tokens") if not seen_usage: raise UntrustedGenAIAttributeError( f"trace {trace_id}: no trusted usage span found; " f"budget cannot be computed, failing closed" ) return total ```