# What structural signals indicate span attribute mapping drift between OpenTelemetry GenAI semantic conventions and an agent's internal trace schema **Date of Record:** 2026 December 4 **Scope:** Applies to agents that emit OTel GenAI spans (`gen_ai.*` attributes) from an internal trace model via a translation layer, where the internal model and the semantic conventions version independently. Does not apply to agents that instrument directly against the OTel SDK with no intermediate schema. ## Core Distinction The failure is not "missing attributes." Missing attributes are loud—they fail validation, trip dashboards, and get caught by schema checks. The failure that matters is *silent semantic re-labeling*: the translation layer continues to populate every required key, but the meaning of the value has shifted because the internal trace model evolved (or the pinned semantic-conventions version advanced) without the mapping being re-derived. The span remains structurally well-formed and passes every conformance check while carrying a value that downstream verification, cost accounting, or replay logic will misread. The distinction to hold: **attribute presence is a structural property; attribute semantics are a versioned contract between two independently-moving schemas.** Conformance testing validates the former and is blind to the latter. ## What Was Observed An agent runtime maintains an internal trace record per LLM call with fields like `turn_index`, `accumulated_tokens`, `model_id`, and `tool_chain`. A translation layer maps these to `gen_ai.*` keys. When the internal model changed `accumulated_tokens` from "tokens for this call" to "cumulative tokens for the session" (a reasonable refactor for billing), the mapping `gen_ai.usage.input_tokens <- accumulated_tokens` kept type-checking: both are integers, both non-negative, both present. The span was valid. But every span after the first in a session now reported a monotonically increasing `input_tokens` that double-counted prior turns. Observable downstream signals, in order of appearance: 1. **Cost-per-span variance collapse.** Per-span token counts stopped being independent samples and became a running sum. Any aggregation that assumed per-span i.i.d. usage (mean, p50) silently inflated; only the *first* span of each session remained correct. 2. **Replay divergence without error.** A verification harness that replayed spans against a recorded transcript and recomputed expected token counts flagged a mismatch—but the mismatch surfaced as "usage anomaly," not "mapping error," because the span schema was valid. 3. **Cross-version skew in fleet telemetry.** Agents on the old internal model and the new one emitted identically-shaped spans with different semantics. Dashboards mixing them produced bimodal distributions that looked like workload heterogeneity, not schema drift. The critical observation: **no single span was wrong in isolation.** The error only exists in the relationship between spans (cumulative vs. per-call) and between schema versions. It is a relational invariant violation, not a field-level one. ## Condition of Validity The mapping contract holds only while three conditions are simultaneously true: - The internal field's **unit and scope** (per-call vs. per-session, input vs. total) match the semantic convention's definition, not merely its type. - The **semantic-conventions version** the mapping was derived against is the version the collector and downstream consumers assume. GenAI conventions are explicitly marked experimental and have moved attribute names and semantics across minor versions (`gen_ai.usage.prompt_tokens` → `gen_ai.usage.input_tokens`, and scope clarifications on several usage fields). - The **mapping is derived, not hand-maintained.** A hand-maintained dict of string keys has no mechanism to detect that the source field's meaning changed underneath it. When any condition lapses, the span remains valid but the mapping is no longer a contract—it is a coincidence of matching types. ## Decision Boundary The boundary between "acceptable drift" and "must halt emission" is crossed when **the same attribute key can carry two different semantics within a single trace or across the retention window of the telemetry backend.** Concretely: - If the refactor changes scope (per-call ↔ cumulative) and any consumer aggregates across spans, halt and re-derive the mapping before emitting. This is the hard boundary. - If the semantic-conventions version changes a definition (not just a name) for a key you emit, pin the version or dual-emit with a version tag; do not silently adopt. - If only the *name* changes with identical semantics, migration can proceed without halting—rename is a mechanical transform with no relational invariant at stake. The boundary is *semantic*: does the value's meaning depend on position or version in a way the consumer cannot recover? If yes, the mapping must fail closed. ## Operational Effect A mapping layer that fails closed converts a silent telemetry corruption into a startup error with a precise cause. The guardrail is: **the translation layer must assert the scope/unit contract of each mapped field against a declared schema, not just its type, and must carry the semantic-conventions version it was derived against as a first-class field.** When the declared source contract and the convention version disagree, emission halts rather than producing well-formed lies. Downstream consumers then see either correct spans or an explicit gap—never a valid-looking span whose meaning has quietly rotated. ```python from dataclasses import dataclass from enum import Enum class UsageScope(Enum): PER_CALL = "per_call" CUMULATIVE = "cumulative" @dataclass(frozen=True) class FieldContract: """The meaning a mapped attribute is required to carry.""" source_field: str required_scope: UsageScope conventions_version: str @dataclass(frozen=True) class InternalField: name: str scope: UsageScope schema_version: str class AttributeMappingDrift(RuntimeError): """Raised when a mapped field's semantics no longer match the semantic-conventions contract, even though its type still fits.""" # gen_ai.usage.input_tokens is defined per-call by the convention. CONTRACTS = { "gen_ai.usage.input_tokens": FieldContract( source_field="accumulated_tokens", required_scope=UsageScope.PER_CALL, conventions_version="1.28.0", ), } def translate_span(internal: dict[str, InternalField], conventions_version: str) -> dict[str, int]: out: dict[str, int] = {} for attr_key, contract in CONTRACTS.items(): field = internal.get(contract.source_field) if field is None: # Missing is loud and acceptable to surface as a gap. continue if field.scope != contract.required_scope: raise AttributeMappingDrift( f"mapping {field.name!r} -> {attr_key!r}: source scope " f"{field.scope.value!r} violates required " f"{contract.required_scope.value!r}; span would be " f"well-formed but semantically wrong" ) if conventions_version != contract.conventions_version: raise AttributeMappingDrift( f"conventions version {conventions_version!r} != " f"derived-against {contract.conventions_version!r} for " f"{attr_key!r}; re-derive before emitting" ) out[attr_key] = field_placeholder(field) return out def field_placeholder(field: InternalField) -> int: # Stand-in for the actual value read; scope already validated. return 0 if __name__ == "__main__": drifted = { "accumulated_tokens": InternalField( name="accumulated_tokens", scope=UsageScope.CUMULATIVE, # refactored for billing schema_version="2.0.0", ) } translate_span(drifted, conventions_version="1.28.0") # raises ```