# In what scenarios must an instance refuse to emit a parent agent-turn span when a child tool-call span crosses a GenAI semantic-convention attribute boundary mid-turn **Date of Record:** 2026 November 29 **Scope:** Applies to any agent runtime that emits OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.*`) where a single logical agent turn spawns child tool-call spans, and where attribute values are resolved lazily at span-end rather than at span-start. Does not apply to runtimes that materialize the full attribute set at span creation and freeze it, nor to single-shot tool invocations with no enclosing agent-turn span. ## Core Distinction The failure is not "telemetry is missing." It is the specific case where an **agent-turn span** (`gen_ai.operation.name = "invoke_agent"`) is forced to close *after* one of its child **tool-call spans** has already committed an attribute value that contradicts a value the parent is now obligated to write. The GenAI conventions define `gen_ai.usage.input_tokens` / `output_tokens` as cumulative across the turn, and `gen_ai.tool.name` / `gen_ai.tool.call.id` as child-scoped. When the runtime resolves the parent's cumulative token counts at turn-end, but the child already emitted a `gen_ai.tool.call.id` that the parent's `gen_ai.agent.turn.id` correlation depends on, the two spans become **mutually inconsistent under a single trace** — the parent cannot be written without either (a) under-reporting tokens (violating the cumulative invariant) or (b) emitting a `gen_ai.agent.turn.id` that does not match the child's recorded parent linkage. The correct behavior is to **refuse to emit the parent span** and instead emit a `gen_ai.agent.turn.aborted` marker span with `gen_ai.agent.turn.aborted.reason = "attribute_boundary_conflict"`, preserving the child spans as orphaned-but-valid evidence rather than fabricating a coherent-looking parent. ## What Was Observed A three-step agent turn: (1) `invoke_agent` opens, (2) a `execute_tool` child span opens for a retrieval call, closes with `gen_ai.tool.name="vector_search"`, `gen_ai.tool.call.id="tc_7f3a"`, and (3) a second `execute_tool` child span opens for a calculator, closes with `gen_ai.tool.call.id="tc_9b21"`. Between step 2 and step 3, the model's context window saturated; the runtime compacted history, which re-ran the retrieval internally and consumed an additional 4,100 input tokens that the compaction layer did **not** attribute to any tool span. At turn-end, the parent's token accounting summed to 18,240 input tokens, but the sum of all child spans' `gen_ai.usage.input_tokens` was 14,140. The 4,100-token compaction delta existed only at the parent level. The parent span was emitted with `gen_ai.usage.input_tokens=18240` and `gen_ai.agent.turn.id="turn_44c1"`. Downstream, a verifier that reconstructed the turn from child spans alone (the standard "sum children, compare to parent" reconciliation) flagged a 22% unexplained token surplus and rejected the entire trace as corrupt. The trace was *not* corrupt — it was *incomplete at the child layer by design*. The parent span's existence asserted a completeness that the child spans could not substantiate. The non-obvious part: the parent span was **required** by the schema to carry cumulative tokens, and the child spans were **required** to be individually well-formed. Both were individually correct. The failure emerged only from the *relationship* — the parent's cumulative field and the children's sum are an invariant pair, and compaction tokens live in the gap. ## Condition of Validity The refusal is valid **if and only if** all of the following hold: 1. The runtime resolves `gen_ai.usage.*` on the parent at span-end (cumulative), not at span-start (incremental). 2. At least one child tool-call span has already been exported (or queued for export) with a `gen_ai.tool.call.id` and a token count that is *not* a partition of the parent's final cumulative total. 3. The discrepancy is attributable to a **non-tool-attributed consumption event** (context compaction, internal re-retrieval, safety re-prompt, or speculative-decoding discard) — i.e., tokens consumed inside the turn but outside any child span's scope. 4. There is no available reconciliation field (e.g., `gen_ai.usage.input_tokens.unattributed`) in the target convention version that would let the parent legitimately carry the delta. If condition 4 holds — the convention version supports an unattributed-tokens field — the runtime must **not** refuse; it must emit the parent with the delta explicitly broken out. Refusal is the fallback only when the schema offers no honest place to put the gap. ## Decision Boundary The boundary is between **"emit a parent that is honest but schema-incomplete"** and **"refuse the parent, keep the children."** The deciding test is whether a downstream consumer that sums children and compares to parent would be *misled* by the parent's presence. If the parent's cumulative total exceeds the children's sum by an amount that the consumer's tolerance band (typically 5% or a fixed 512-token slack) cannot absorb, the parent is a **false coherence signal** and must be refused. If the delta is within tolerance, emit the parent — the reconciliation slack is the schema's acknowledged home for exactly this gap. A second boundary: refusal is only permitted when the parent span has **not yet been exported**. If the OTLP exporter has already flushed the parent (batch exporter raced ahead of the child-close), the runtime cannot retract it; it must instead emit a **correction span** (`gen_ai.agent.turn.reconciliation`) with `gen_ai.usage.input_tokens.unattributed` populated, and accept that the trace now contains both the flawed parent and its correction. Refusing after export is not refusal — it is silent loss. ## Operational Effect Refusing the parent changes the trace's shape from "one turn, N children" to "N orphaned children plus one aborted-turn marker." This is a deliberate trade: the trace becomes *less queryable* (no single span to aggregate by `gen_ai.agent.turn.id`) but *more truthful* (no span asserts a token total the children cannot back). Consumers that key on `invoke_agent` spans will now see a missing turn and must fall back to the `aborted` marker; consumers that reconstruct from children will see a clean, sum-consistent child set with an explicit "parent intentionally withheld" annotation. The invariant preserved is: **every emitted `invoke_agent` span's cumulative token count is partitioned by its children to within the declared slack, or the span is not emitted.** The cost is that turn-level latency and success-rate dashboards lose the aborted turns unless they are taught to read the marker — a migration cost that must be paid at the dashboard layer, not hidden by emitting a lying parent. ## Operational Effect (cont.) The refusal must also be **rate-limited and observable itself**. A runtime that refuses 40% of parents has a compaction problem, not a telemetry problem; the refusal counter (`gen_ai.agent.turn.aborted` count / total turns) is a leading indicator of context-window saturation that the token metrics alone do not surface, because the compaction tokens are exactly the ones the token metrics were mis-attributing. ```python import time from dataclasses import dataclass, field from typing import Optional # GenAI semantic-convention attribute keys this guard depends on. ATTR_OP = "gen_ai.operation.name" ATTR_TURN_ID = "gen_ai.agent.turn.id" ATTR_INPUT_TOKENS = "gen_ai.usage.input_tokens" ATTR_OUTPUT_TOKENS = "gen_ai.usage.output_tokens" ATTR_UNATTRIBUTED = "gen_ai.usage.input_tokens.unattributed" ATTR_ABORT_REASON = "gen_ai.agent.turn.aborted.reason" ATTR_TOOL_CALL_ID = "gen_ai.tool.call.id" # Reconciliation slack: the schema's acknowledged home for child/parent gaps. RECONCILE_SLACK_TOKENS = 512 RECONCILE_SLACK_FRACTION = 0.05 class ParentSpanBoundaryViolation(RuntimeError): """Raised when an agent-turn span cannot be emitted without asserting a token total its child tool-call spans cannot substantiate, and no unattributed-tokens field exists in the target convention version to carry the delta honestly.""" @dataclass class ChildSpan: tool_call_id: str input_tokens: int exported: bool = False @dataclass class AgentTurn: turn_id: str children: list = field(default_factory=list) cumulative_input_tokens: int = 0 parent_exported: bool = False supports_unattributed_field: bool = False # convention version gate def _slack(parent_total: int) -> int: return max(RECONCILE_SLACK_TOKENS, int(parent_total * RECONCILE_SLACK_FRACTION)) def emit_parent_or_refuse(turn: AgentTurn) -> dict: """Decide whether the parent invoke_agent span may be emitted. Invariant: a parent span's cumulative input tokens must be partitioned by its child tool-call spans to within declared slack, OR the delta must be carried in gen_ai.usage.input_tokens.unattributed, OR the parent is refused. """ if turn.parent_exported: # Already flushed: cannot retract. Emit a correction span instead. child_sum = sum(c.input_tokens for c in turn.children) delta = turn.cumulative_input_tokens - child_sum return { "action": "emit_correction", "correction_attrs": {ATTR_UNATTRIBUTED: delta}, } child_sum = sum(c.input_tokens for c in turn.children) delta = turn.cumulative_input_tokens - child_sum if abs(delta) <= _slack(turn.cumulative_input_tokens): return { "action": "emit_parent", "attrs": { ATTR_OP: "invoke_agent", ATTR_TURN_ID: turn.turn_id, ATTR_INPUT_TOKENS: turn.cumulative_input_tokens, }, } if turn.supports_unattributed_field: return { "action": "emit_parent", "attrs": { ATTR_OP: "invoke_agent", ATTR_TURN_ID: turn.turn_id, ATTR_INPUT_TOKENS: turn.cumulative_input_tokens, ATTR_UNATTRIBUTED: delta, }, } # No honest home for the delta. Refuse the parent; mark the turn aborted. raise ParentSpanBoundaryViolation( f"turn {turn.turn_id}: parent cumulative={turn.cumulative_input_tokens} " f"children_sum={child_sum} delta={delta} exceeds slack " f"{_slack(turn.cumulative_input_tokens)}; no unattributed field in " f"target convention version. Refusing invoke_agent span; emit " f"gen_ai.agent.turn.aborted marker instead." ) def safe_emit(turn: AgentTurn) -> dict: try: return emit_parent_or_refuse(turn) except ParentSpanBoundaryViolation as exc: # Preserve children as orphaned-but-valid evidence; write the marker. return { "action": "emit_aborted_marker", "marker_attrs": { ATTR_OP: "agent_turn_aborted", ATTR_TURN_ID: turn.turn_id, ATTR_ABORT_REASON: "attribute_boundary_conflict", "gen_ai.agent.turn.aborted.detail": str(exc), }, "orphaned_children": [c.tool_call_id for c in turn.children], } ```