# When must an instance refuse to emit a GenAI span: parent-span attribute inheritance across a mid-trace model swap **Date of Record:** 2026 November 19 **Scope:** Applies to any agent runtime that emits OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.*`) and that can change the model, provider, or tool schema *within* a single trace or a single logical agent turn. Does not apply to single-model, single-turn request/response instrumentation where one span maps to one immutable completion. ## Core Distinction The GenAI semantic conventions treat a span as a *record of a single inference or tool invocation*, but agent runtimes treat a trace as a *continuity object*: a unit of work that may span a model swap, a fallback, a retry against a different provider, or a tool-call whose schema is renegotiated mid-flight. The failure lives in the gap between these two models of the span. The non-obvious boundary: `gen_ai.request.model`, `gen_ai.response.model`, `gen_ai.provider.name`, and `gen_ai.usage.*` are defined as *per-span* attributes, and the conventions assume the request model and the responding model are the same entity for the life of the span. When an agent runtime implements provider fallback by reusing a parent span (or by letting a child span inherit the parent's `gen_ai.request.model`), the emitted span asserts a model identity that never produced the tokens it reports usage for. The span is well-formed, passes schema validation, and is silently wrong. Downstream cost attribution, latency SLOs keyed on model, and eval replay all inherit the corruption. This is not a schema violation. It is a *semantic* violation that no validator catches, because every individual attribute is legal and the attribute set is complete. The invariant that breaks is: **the model named in `gen_ai.response.model` for a span must be the model that generated every token counted in that span's `gen_ai.usage.output_tokens`.** The conventions do not state this as a hard invariant, but every consumer of the data assumes it. ## What Was Observed An agent orchestration layer ran a single logical turn as one parent span (`agent.turn`) with child spans for each inference call. The parent span was created once and stamped with `gen_ai.request.model = "model-a"` and `gen_ai.provider.name = "provider-a"` at turn start. The runtime supported mid-turn fallback: if the primary provider returned a 429 or a schema-invalid tool call, the next inference call was routed to `model-b` on `provider-b`. The child inference spans were emitted correctly — each carried its own `gen_ai.request.model` and `gen_ai.response.model`. But the *parent* span, which carried the aggregate `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` for the whole turn (summed across children), was never updated. Its `gen_ai.request.model` remained `model-a` for the entire turn, including the two child calls that actually executed on `model-b`. The observable trace, in the backend: - Parent span `agent.turn`: `gen_ai.request.model=model-a`, `gen_ai.usage.output_tokens=4821`, `gen_ai.provider.name=provider-a`. - Child span 1: `gen_ai.response.model=model-a`, 1,900 output tokens. - Child span 2: `gen_ai.response.model=model-b`, 1,410 output tokens (fallback). - Child span 3: `gen_ai.response.model=model-b`, 1,511 output tokens (fallback persisted). The parent's 4,821 tokens were attributed in full to `model-a` by every cost dashboard that read the parent span. The per-child spans told the truth, but the aggregation layer had been written to read the parent — because that is what the conventions imply a parent turn span is for. Cost attribution was off by roughly 60% for the affected turns; latency SLOs keyed on `model-a` p99 were polluted by fallback-path latencies that belonged to `model-b`. A second, subtler observation: a tool-call child span emitted after the fallback inherited `gen_ai.request.model=model-a` from the parent via the runtime's context-propagated attribute bag, even though the tool schema in force was the one `model-b` had negotiated. The tool span's `gen_ai.tool.name` was correct, but the model context it implicitly claimed was not. Replay tooling that reconstructed the turn from parent attributes re-issued the tool call against `model-a`'s schema and failed validation on resume. ## Condition of Validity The parent-span-as-continuity-object pattern is only valid under a strict condition: **the model identity is invariant across the entire span's lifetime.** Concretely, a span may carry `gen_ai.request.model`, `gen_ai.provider.name`, and aggregate `gen_ai.usage.*` together only if: 1. Every token counted in `gen_ai.usage.*` on that span was produced by the model named in `gen_ai.request.model` (and, where present, `gen_ai.response.model`), and 2. No descendant span in the subtree executed against a different model or provider, or 3. If a descendant did, the parent span does not carry aggregate usage or model-identity attributes at all — it becomes a structural span only. Condition 3 is the escape hatch the conventions leave open and the one most runtimes miss. A turn span that can contain heterogeneous model calls must either drop `gen_ai.request.model` and `gen_ai.usage.*` from the parent and let children own them, or it must fragment into one parent span per model segment. There is no valid middle: a parent span that names one model and aggregates usage across many is a lie that passes every schema check. The boundary is sharp because the attributes are individually legal in every configuration. `gen_ai.request.model` is a valid attribute on any span. `gen_ai.usage.output_tokens` is a valid attribute on any span. The invalidity is relational — it exists only in the joint assertion that these tokens came from this model — and relational invalidity is exactly what attribute-level validation cannot see. ## Decision Boundary The runtime must make a decision at the moment of fallback, before the second inference call is emitted: - **If the fallback model equals the original model** (same provider, same model string, retry only): the parent span may retain model identity and aggregate usage. No fragmentation needed. - **If the fallback model differs** in `gen_ai.request.model`, `gen_ai.response.model`, or `gen_ai.provider.name`: the parent span must either (a) drop model-identity and usage attributes and become structural, with children owning the truth, or (b) be closed and a new sibling parent span opened for the new model segment. The decision must be made *before* the fallback inference span is created, because the parent's attribute set is what the child inherits through context propagation. A runtime that decides after the fact — that patches the parent at turn end — has already emitted child spans whose inherited attributes were captured at creation time and cannot be retroactively corrected in most OTel SDKs without span re-creation. The hard rule: **model identity is not a span-tree property; it is a span-leaf property.** Any span that aggregates across leaves of differing model identity must not assert a single model identity. This is the invariant. It is violated the instant a parent span carries a model attribute while its subtree contains a child with a different `gen_ai.response.model`. ## Operational Effect When the invariant is enforced, three downstream properties hold that otherwise fail silently: - **Cost attribution is exact.** Reading the parent span for aggregate cost is safe only if the parent names one model. When it does not, aggregation layers must be forced to sum children, and the runtime must not present a misleading parent aggregate. - **Replay is faithful.** A trace replay that reconstructs model context from span attributes will reconstruct the correct model per segment, so tool-schema renegotiation on resume does not desynchronize. - **SLOs are clean.** Latency distributions keyed on `gen_ai.request.model` do not mix fallback-path latencies into primary-model percentiles. When it is violated, the failure is invisible at emit time, invisible at validation time, and only surfaces as a downstream discrepancy between two independent readings of the same trace — parent aggregate versus child sum. That discrepancy is the detection signal, and it should be a first-class health check: for every span carrying both `gen_ai.request.model` and `gen_ai.usage.output_tokens`, assert that the sum of descendant `gen_ai.usage.output_tokens` for children whose `gen_ai.response.model` differs from the parent's `gen_ai.request.model` is zero. A nonzero result is a continuity violation, not a rounding error. ```python from dataclasses import dataclass, field from typing import Optional class GenAISpanContinuityError(RuntimeError): """Raised when a span asserts a model identity that its subtree contradicts.""" pass @dataclass class SpanRecord: name: str attributes: dict children: list = field(default_factory=list) def model_identity(self) -> Optional[str]: return self.attributes.get("gen_ai.request.model") def response_model(self) -> Optional[str]: return self.attributes.get("gen_ai.response.model") def output_tokens(self) -> int: return int(self.attributes.get("gen_ai.usage.output_tokens", 0)) def descendants(self): for child in self.children: yield child yield from child.descendants() def assert_model_identity_invariant(span: SpanRecord) -> None: """ Enforce: a span carrying gen_ai.request.model and aggregate gen_ai.usage.* must not have any descendant whose gen_ai.response.model differs from it. """ parent_model = span.model_identity() has_usage = "gen_ai.usage.output_tokens" in span.attributes if parent_model is None or not has_usage: # Structural span or leaf; invariant does not bind here. return offending = [] for desc in span.descendants(): desc_model = desc.response_model() or desc.model_identity() if desc_model is not None and desc_model != parent_model: offending.append((desc.name, desc_model, desc.output_tokens())) if offending: total_foreign = sum(t for _, _, t in offending) raise GenAISpanContinuityError( f"Span '{span.name}' asserts gen_ai.request.model={parent_model!r} " f"with aggregate gen_ai.usage.output_tokens={span.output_tokens()}, " f"but {len(offending)} descendant(s) executed on a different model " f"({total_foreign} tokens misattributed): {offending}. " f"Drop model/usage attributes from the parent or fragment the span " f"per model segment before emitting." ) def emit_turn_span(span: SpanRecord) -> SpanRecord: # Called immediately before the span is handed to the exporter. assert_model_identity_invariant(span) return span ```