# When must an instance abort a trace span when OpenTelemetry GenAI semantic-convention attributes disagree with the tool-call result the agent actually observed **Date of Record:** 2026 November 27 **Scope:** Applies to any agent runtime that emits OpenTelemetry spans using the GenAI semantic conventions (`gen_ai.*`) to record tool-call and agent-turn activity, where the span is closed before downstream execution confirms the recorded outcome. Does not apply to runtimes that emit spans only after tool execution completes and results are durably committed. ## Core Distinction The GenAI semantic conventions model a tool call as a span with attributes describing *intent* (`gen_ai.tool.name`, `gen_ai.tool.call.id`, `gen_ai.operation.name`) and *outcome* (`gen_ai.tool.call.result`, error status, `gen_ai.usage.*`). The convention assumes the span is a faithful post-hoc record. In practice, agent runtimes emit the span at the moment the model *emits* the tool call — before the tool executes — and then patch attributes afterward. The distinction that matters: a span attribute is not evidence of execution; it is a claim. When the claim and the observed tool result diverge, the span becomes a *poisoned provenance record*: downstream auditors, replay engines, and cost reconcilers read `gen_ai.tool.call.result` as ground truth and reconstruct a false execution history. The boundary failure is not the divergence itself — tools fail, that is normal — it is the runtime continuing the agent turn while the span asserts success. The span must be aborted, not patched. ## What Was Observed A multi-step agent executing a `search_corpus` tool call emitted a span with `gen_ai.operation.name="execute_tool"`, `gen_ai.tool.name="search_corpus"`, and initially `gen_ai.tool.call.result` unset. The tool returned a structured error (`{"status": "partial", "rows": 3, "truncated": true}`) because the backend hit a row cap. The runtime's span-patching logic had a bug: it treated any dict response without an `"error"` key as success and set `gen_ai.tool.call.result` to the serialized dict, status `OK`. The agent's reasoning loop, reading the same dict, correctly saw `truncated: true` and retried with pagination — producing three more spans, each also `OK`. The failure trace: 1. Span S1 closes with `status=OK`, `gen_ai.tool.call.result` = truncated payload. 2. Agent retries; spans S2–S4 close `OK`. 3. Replay engine, reconstructing the turn from spans alone, sees four successful `search_corpus` calls and no indication of truncation semantics — it cannot distinguish "four independent searches" from "one search that silently degraded." 4. Cost reconciler sums `gen_ai.usage.input_tokens` across S1–S4 and attributes them to four distinct retrieval events, inflating the retrieval-cost line by 4× against the ground-truth one logical query. 5. An auditor agent, asked to verify the turn, reads the spans and concludes the retrieval was complete. It is wrong. The non-obvious part: the runtime was *correct* at the reasoning layer (it retried) and *wrong* at the telemetry layer (it recorded success). The two layers disagreed, and the telemetry layer won because it is the durable record. ## Condition of Validity The invariant holds only when all three conditions are met: - **C1 (pre-execution emission):** the span is opened before tool execution, so `gen_ai.tool.call.result` and status are written by a patcher that runs after the result is known. - **C2 (semantic gap):** the tool's result schema admits "successful but degraded" outcomes (truncation, partial, rate-limited-but-nonempty) that the convention's binary `status=OK|ERROR` cannot represent. - **C3 (downstream consumption):** at least one consumer (replay, audit, cost, or a peer agent) reads spans as ground truth rather than as hints. When any condition is absent, the failure does not occur: if the span is emitted post-execution (¬C1), the patcher cannot race the result; if the tool has a strict success/error schema (¬C2), the binary status is faithful; if no consumer trusts spans (¬C3), divergence is harmless. The failure is a *conjunction*, which is why it hides — each condition alone looks like normal practice. ## Decision Boundary The runtime must decide, at span-close time, between three actions: - **Patch and continue** — valid only if the patched attributes are *provably* faithful to the observed result. Requires a total mapping from tool result schema to `gen_ai.*` attributes, including degraded outcomes. - **Abort the span** — set `status=ERROR`, record `gen_ai.tool.call.result` as the raw observed payload, and attach an exception event naming the semantic gap. The agent turn may continue; the *span* does not claim success. - **Suppress the span** — discard it and emit a replacement post-execution. Only valid if the runtime can guarantee no partial span escaped to the exporter (i.e., the span was never flushed). The boundary is crossed the moment the patcher would set `status=OK` on a result whose schema the convention cannot faithfully encode. At that instant, patching is a lie and abort is mandatory. The rule: *a span may only claim `OK` if every field in the tool result maps to a convention attribute that preserves its meaning.* Truncation has no such attribute, so `OK` is forbidden. ## Operational Effect Enforcing the abort boundary changes three downstream behaviors: 1. **Replay engines** see `ERROR` spans with raw payloads and can reconstruct the retry chain correctly — the truncated call is visibly distinct from the paginated retries. 2. **Cost reconcilers** must sum only spans whose status matches the logical operation; aborted spans are flagged as "degraded, retried" and excluded from per-query attribution, restoring the 1× retrieval count. 3. **Auditor agents** reading spans now see a signal (the exception event's `semantic_gap` field) that instructs them to consult the raw payload rather than trust the status. The audit conclusion flips from "retrieval complete" to "retrieval required pagination; verify row coverage." The cost is span volume: degraded calls produce two spans (the aborted original, the successful retry) instead of one patched span. This is the correct trade — provenance fidelity at the telemetry layer is worth the extra span, because the alternative is a durable record that lies to every consumer downstream. ```python class SpanAttributeDivergenceError(RuntimeError): """Raised when a span's gen_ai.* attributes would misrepresent the observed tool result.""" pass def close_tool_span(span, tool_result: dict, convention_attrs: dict) -> None: """ Close a gen_ai tool-call span. Abort if the result cannot be faithfully encoded by the GenAI semantic conventions. """ degraded_markers = {"truncated", "partial", "rate_limited"} observed_degraded = any(tool_result.get(k) for k in degraded_markers) claims_ok = convention_attrs.get("status") == "OK" if observed_degraded and claims_ok: # The convention has no attribute for "successful but truncated". # Patching status=OK would produce a poisoned provenance record. span.set_attribute("status", "ERROR") span.set_attribute("gen_ai.tool.call.result", repr(tool_result)) span.add_event( "semantic_gap", {"gap": "degraded_result_unrepresentable", "markers": str(observed_degraded)}, ) raise SpanAttributeDivergenceError( f"tool result {tool_result!r} is degraded but span would claim OK; " "span aborted to preserve replay fidelity" ) for key, value in convention_attrs.items(): span.set_attribute(key, value) span.set_attribute("status", "OK") ```