# Which invariants prevent semantic-convention conformance drift from silently corrupting agent observability pipelines **Date of Record:** 2026 November 28 **Scope:** Applies to agent runtimes that emit GenAI semantic conventions (span attributes such as `gen_ai.operation.name`, `gen_ai.request.model`, `gen_ai.usage.input_tokens`, `gen_ai.tool.name`, plus parent/child span linkage) into an observability pipeline that validates conformance against a pinned schema version. Does not cover generic OpenTelemetry resource attribute validation, log-body schema checks, or metric-only pipelines without spans. ## Core Distinction The failure that matters is not "a span is missing an attribute." It is **conformance drift under partial emission**: an agent emits a span that is *structurally valid* against the collector's schema validator but *semantically wrong* against the convention because a required attribute was defaulted, coerced, or inherited from a parent span rather than measured at the emitting operation. The validator passes it. The pipeline accepts it. Downstream consumers (cost attribution, token-budget enforcement, tool-call replay) read a plausible number that never existed. The distinction is between **schema conformance** (type, presence, enum membership) and **semantic conformance** (the value corresponds to the operation the span claims to describe). A conformance test suite that only checks the former gives you a green dashboard over a corrupted trace graph. The invariant that prevents this is not "every required attribute is present" but "every attribute value is *provenance-bound* to the span's own operation, not inherited or defaulted across a handoff boundary." ## What Was Observed A multi-agent runtime emitted spans for a planner agent that delegated tool execution to a worker agent over an A2A-style transport. The worker emitted `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` on its own `gen_ai.chat` span. The planner, on receiving the worker's result, emitted a *parent* span for the same logical turn. The planner's span-emission helper was written to "fill in" usage attributes from the nearest context if absent — a defensive default added during an earlier incident where some providers omitted usage. When the worker returned, the planner's context still held the **planner's own** last-known token counts (from the prompt it sent, not the worker's response). The helper saw `gen_ai.usage.input_tokens` absent on the planner span, pulled from context, and stamped the planner's *request* token count onto a span that represented the *aggregate* turn. Observed trace after the fact: - Worker `gen_ai.chat` span: `input_tokens=1840`, `output_tokens=412` (correct, measured). - Planner parent span: `input_tokens=1840`, `output_tokens=412` (inherited from context — but the planner's actual aggregate was `input_tokens=1840+412=2252` across two model calls, and its own prompt was 1840 *before* the worker's output was appended). The conformance validator checked: attribute present, integer type, non-negative, within `gen_ai.usage.*` namespace. All passed. The cost-attribution job summed parent + child and **double-counted 1840 input tokens**, silently inflating per-turn cost by ~62% on turns that delegated. The token-budget enforcer, reading the same spans, throttled the agent early on delegated turns only — a behavior that looked like "the agent is expensive when it uses tools" rather than "our telemetry is lying." The drift was invisible for eleven days because the validator's conformance report was keyed on *attribute presence rate*, which stayed at 100%. ## Condition of Validity The provenance-binding invariant holds only when all of the following are true at emission time: 1. **The emitting span owns its measurement.** A usage attribute is written only by the code path that observed the model call or tool call it describes, never by a context-inheritance helper. If a value is unavailable, the span omits the attribute and sets an explicit `gen_ai.usage.unavailable_reason` rather than defaulting. 2. **Parent spans aggregate, they do not inherit.** A span representing a multi-call turn must either (a) carry no usage attributes and rely on child-span summation by the consumer, or (b) carry a usage attribute computed as an explicit sum over named children, recorded with `gen_ai.usage.aggregation=sum_of_children` and a child-span-count. There is no third mode. 3. **Conformance tests assert cross-span arithmetic, not per-span presence.** The test harness must, for every parent span carrying usage, verify `parent.usage == Σ(child.usage)` within tolerance, and fail the build when the identity breaks — not merely check that the attribute exists. 4. **Handoff boundaries are marked.** When a span crosses an agent-to-agent transport, it carries `gen_ai.agent.handoff_from` and `gen_ai.agent.handoff_to`; the validator rejects any usage attribute on a span that has a `handoff_from` but no matching child span, because that is the exact shape of an inherited value. Outside these conditions — e.g., single-agent runtimes with one model call per turn, or pipelines that only consume leaf spans — the invariant is trivially satisfied and the drift does not occur. The invariant is load-bearing specifically at **delegation and aggregation boundaries**. ## Decision Boundary When a conformance run reports a violation, the operator must decide: is this a *schema* failure (reject the span, drop it) or a *semantic* failure (the span is well-formed but the value is untrustworthy, and downstream consumers must be warned)? The boundary condition: **a span that passes schema conformance but fails the cross-span arithmetic identity is never safe to pass downstream unmarked.** The correct action is not to drop it (you lose the trace) and not to pass it (you corrupt cost and budget). The correct action is to emit it with a quarantine flag — `gen_ai.conformance.status=arithmetic_mismatch` — and route it to a side channel that downstream consumers are configured to exclude from cost and budget aggregation but include in trace reconstruction. The decision flips to "reject and alert" when the mismatch rate exceeds a threshold (observed: >2% of delegated spans over a 15-minute window), because at that point the emission helper itself is broken, not an isolated race, and quarantine is masking a systemic defect. The non-obvious part: **presence-rate dashboards will show 100% green during a full arithmetic-mismatch outage.** The decision must be driven by the arithmetic identity check, which must be a first-class conformance test, not a downstream analytics query someone remembers to run. ## Operational Effect After the fix — removing context-inheritance from the emission helper, adding `gen_ai.usage.aggregation` semantics, and promoting the parent-equals-sum-of-children identity to a blocking conformance test — three things changed: - Cost attribution stabilized on delegated turns (the 62% inflation disappeared; per-turn cost variance dropped to expected provider-pricing variance). - The token-budget enforcer stopped throttling tool-using turns preferentially, because it was reading real aggregate counts. - The conformance suite's runtime increased ~14% (it now walks the full span tree per turn to check the identity), which was accepted as the cost of not shipping a green dashboard over a corrupted graph. The general lesson for agent observability pipelines: **schema conformance is necessary but not sufficient, and the gap between them is exactly where inherited-and-defaulted values live.** Any conformance test that does not assert an arithmetic or provenance identity across span relationships will pass a pipeline that is silently misreporting the thing it exists to measure. The invariant to enforce is provenance-binding: every attribute value is measured by the span that claims it, or the span explicitly says it cannot. ```python class ConformanceArithmeticMismatch(RuntimeError): """Raised when a parent span's usage attributes do not equal the sum of its children's, indicating inherited or defaulted values rather than measured aggregation.""" pass def validate_usage_provenance(span, children, tolerance=0): """ Enforce the provenance-binding invariant for GenAI usage attributes. A parent span carrying gen_ai.usage.* must either: (a) carry no usage attributes (consumer sums children), or (b) declare gen_ai.usage.aggregation == 'sum_of_children' AND equal the sum of its children's usage within tolerance. Context-inherited values (the failure mode) violate (b) and are rejected. """ parent_input = span.attributes.get("gen_ai.usage.input_tokens") parent_output = span.attributes.get("gen_ai.usage.output_tokens") if parent_input is None and parent_output is None: return # mode (a): no usage on parent, safe # If any usage is present, aggregation mode must be declared explicitly. mode = span.attributes.get("gen_ai.usage.aggregation") if mode != "sum_of_children": raise ConformanceArithmeticMismatch( f"span {span.span_id} carries usage {parent_input}/{parent_output} " f"but declares aggregation={mode!r}; inherited values are not permitted" ) child_input = sum( c.attributes.get("gen_ai.usage.input_tokens", 0) for c in children ) child_output = sum( c.attributes.get("gen_ai.usage.output_tokens", 0) for c in children ) if abs((parent_input or 0) - child_input) > tolerance or abs( (parent_output or 0) - child_output ) > tolerance: raise ConformanceArithmeticMismatch( f"span {span.span_id} aggregation mismatch: " f"parent=({parent_input},{parent_output}) " f"sum_of_children=({child_input},{child_output}); " f"likely inherited from context rather than measured" ) ```