# In what scenarios must an instance refuse to emit OpenTelemetry GenAI agent spans when the tool-call attribute set is only partially resolvable at span-close time **Date of Record:** 2026 November 26 **Scope:** Applies to any agent runtime that instruments tool-invoking LLM loops with OpenTelemetry GenAI semantic conventions (`gen_ai.*` attributes) and closes spans before the full tool-call attribute set is known. Does not apply to pure inference spans with no tool dispatch, nor to batch offline trace export where span mutation before flush is permitted. ## Core Distinction The GenAI semantic conventions treat a span as a *record of a completed operation*, not a mutable event log. The `gen_ai.tool.name`, `gen_ai.tool.call.id`, `gen_ai.operation.name`, and token-usage attributes (`gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens`) are defined at span-close. The failure mode is not "missing attributes" — it is **emitting a span whose attribute set is internally inconsistent because the tool call resolved asynchronously after the model turn span was closed**. A span that says `gen_ai.operation.name=chat` with `gen_ai.usage.output_tokens` populated but no corresponding `gen_ai.tool.call.id`, when a tool call *did* occur, is worse than no span: it is a false negative that downstream cost attribution and replay auditors will trust. The non-obvious boundary: **partial resolvability at close is not a reason to emit a degraded span; it is a reason to defer close or to emit an explicit `incomplete` marker that downstream consumers must honor.** Most SDKs default to the former-violating behavior because span builders are synchronous and tool dispatch is not. ## What Was Observed An agent loop issued a `chat` request that returned a `tool_calls` array with two entries. The runtime dispatched tool A synchronously (resolved in 40ms) and tool B to an MCP server over a transport that timed out at the 5s span deadline. The span builder had already captured `gen_ai.tool.name` for A, set `gen_ai.usage.output_tokens=812`, and — because the span context manager exited at the deadline — closed the span with A's tool attributes only. Tool B's eventual late resolution (at 7.3s) found no open span and was dropped. The resulting trace showed a single tool call, token counts consistent with *one* tool-call round trip, and a `finish_reason=stop`. A cost-attribution replay that summed `output_tokens` across the session undercounted by the tool-B round trip, and a verification auditor replaying the trace concluded the agent had completed with one tool result when it had actually consumed two. The trace was not corrupt — every attribute was individually valid — but the **cardinality invariant between `tool_calls` in the response and `gen_ai.tool.call.id` spans emitted was violated silently**. This is the boundary: the semantic conventions have no field expressing "this span is known-incomplete." Absence of a tool span is indistinguishable from absence of a tool call. ## Condition of Validity A GenAI agent span is valid only if, at close, one of the following holds: 1. **Complete resolution:** every tool call in the model response has a corresponding child span with a matching `gen_ai.tool.call.id`, and token usage reflects the full round trip. 2. **Explicit incompleteness:** the span carries a sentinel (e.g., `gen_ai.agent.incomplete=true` plus a reason attribute) and *no* token-usage attributes are emitted, because usage is only meaningful for a fully accounted round trip. 3. **Deferred close:** the span remains open until all dispatched tool calls resolve or a hard deadline fires, at which point condition (2) applies. Emitting a span that satisfies none of these is the invalid state. The invariant is: *token usage attributes and tool-call span cardinality must be jointly consistent or jointly withheld.* ## Decision Boundary The runtime must decide, at the moment the model turn completes but before tool dispatch resolves, whether the span can be closed synchronously. The decision rule: - If all tool calls are dispatched to resolvers with a bounded, synchronous-or-awaitable contract → await, then close with full attributes. - If any tool call is dispatched to an asynchronous transport (MCP over network, A2A handoff, queue) → the span **must not** be closed at the model turn boundary; it must be held open under a deadline, and the deadline expiry path must route to the explicit-incompleteness branch, stripping token-usage attributes. - Never close a tool-bearing span on a wall-clock timer that is shorter than the tool transport's own timeout, because the timer will systematically win the race and produce silent undercounts. ## Operational Effect Runtimes that violate this boundary produce traces that are individually well-formed but collectively lie about tool-call cardinality and token cost. The failure is invisible to schema validation and visible only to replay auditors that cross-check `tool_calls` length against emitted tool spans. The guardrail is a span-close gate that refuses to emit token-usage attributes on a tool-bearing span unless tool-span cardinality matches the response's `tool_calls` length, and otherwise emits the explicit-incompleteness sentinel. ```python class IncompleteGenAISpanError(RuntimeError): """Raised when a tool-bearing GenAI span would close with inconsistent token-usage and tool-call cardinality attributes.""" pass def close_genai_span(span, response_tool_calls, emitted_tool_spans, usage): expected = len(response_tool_calls) emitted = len(emitted_tool_spans) if expected == 0: span.set_attribute("gen_ai.usage.input_tokens", usage.input_tokens) span.set_attribute("gen_ai.usage.output_tokens", usage.output_tokens) span.end() return if emitted == expected: span.set_attribute("gen_ai.usage.input_tokens", usage.input_tokens) span.set_attribute("gen_ai.usage.output_tokens", usage.output_tokens) span.end() return # Partial resolution: refuse to emit usage, mark incomplete instead. if usage is not None and hasattr(usage, "output_tokens"): raise IncompleteGenAISpanError( f"tool-bearing span has {emitted}/{expected} tool spans resolved; " f"refusing to emit gen_ai.usage.* on an incomplete round trip" ) span.set_attribute("gen_ai.agent.incomplete", True) span.set_attribute("gen_ai.agent.incomplete.reason", "tool_dispatch_unresolved") span.end() ```