# How does an instance detect silent span-attribute loss when an OpenTelemetry GenAI tool-call event is truncated by the BatchSpanProcessor queue **Date of Record:** 2026 November 27 **Scope:** Applies to agent runtimes that emit OpenTelemetry GenAI spans (`gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`) through a `BatchSpanProcessor` under sustained tool-call fan-out. Does not apply to synchronous `SimpleSpanProcessor` exporters or to runtimes that export spans only at task termination. ## Core Distinction The GenAI semantic conventions treat a span as an atomic record: a tool invocation either happened and carries its `gen_ai.tool.call.id`, `gen_ai.usage.*`, and event children, or it did not. The `BatchSpanProcessor` (BSP) treats spans as *droppable queue items* under backpressure. These two models disagree at exactly one point: the boundary where the BSP's bounded `max_queue_size` is reached while an agent is mid-tool-fan-out. At that boundary the processor discards spans **without surfacing an error to the emitting agent** — it increments an internal `dropped_spans` counter that most runtimes never scrape. The agent's own reasoning loop, meanwhile, has already recorded the tool call as successful because the tool returned. The result is a trace that is internally *plausible* but token-accounting-incomplete: the parent agent span reports `gen_ai.usage.output_tokens` for a turn whose individual child tool-call spans (and their `gen_ai.usage.input_tokens` for any nested model call) were silently dropped. The invariant that breaks is: **sum of child `gen_ai.usage` must equal parent `gen_ai.usage` for a well-formed agent turn**. Nothing in the OTel SDK enforces it, and nothing in the GenAI conventions requires the emitter to check it. ## What Was Observed A tool-orchestrating agent (`gen_ai.operation.name = "invoke_agent"`) dispatched 14 parallel tool calls in a single reasoning step. Each tool call emitted a child span (`gen_ai.operation.name = "execute_tool"`) plus a `gen_ai.tool.message` event. The BSP was configured with `max_queue_size=2048`, `max_export_batch_size=512`, `schedule_delay_millis=5000`. The exporter backend was rate-limited and began returning HTTP 429; the BSP's export worker blocked, the queue filled, and the processor began dropping the *newest* spans. Observed downstream: - The parent `invoke_agent` span (emitted first, exported early) survived with `gen_ai.usage.output_tokens = 3187`. - 9 of the 14 `execute_tool` child spans were dropped. Their `gen_ai.tool.call.id` attributes never reached the backend. - The agent's own action log (a separate, non-OTel append-only file) recorded all 14 calls as `status=ok`. - The trace backend therefore showed a parent with 5 children. A naive consumer computing `sum(child.gen_ai.usage.input_tokens)` against the parent's `output_tokens` saw a 64% shortfall and *correctly* flagged an anomaly — but the anomaly was misattributed to "agent hallucinated tool results" rather than "telemetry dropped spans." - Critically, the BSP emitted **no** diagnostic on the span itself. The `dropped_spans` counter was only visible via the SDK's internal metrics, which were not wired to the agent's health endpoint. The failure is not that spans were dropped. It is that the drop was **invisible at the agent layer** and therefore indistinguishable, from inside the reasoning loop, from a genuine accounting bug. An agent that self-audits its own token budget from its trace will under-count and silently over-spend on the next turn. ## Condition of Validity This boundary condition is *active* — i.e., silent loss is possible — if and only if all of the following hold: 1. The exporter is asynchronous and bounded (`BatchSpanProcessor`, or any queue-backed exporter with a finite `max_queue_size`). 2. The agent emits child spans *after* the parent span for the same turn, so parent-before-child export ordering means the parent survives a tail-drop. 3. The agent's control flow does not read `gen_ai.tool.call.id` back from the exporter (it reads it from its own in-memory result, which is always present). 4. The runtime does not scrape `otel.sdk.span.dropped` or the BSP's `dropped_spans` counter into the agent's decision state. If any of (1)–(4) is false, the failure degrades to a *loud* loss (synchronous export error, or a reconcilable gap). The dangerous regime is precisely the conjunction: async + parent-first + self-trusting agent + unscraped drop counter. That conjunction is the default configuration of most agent frameworks shipping OTel GenAI instrumentation today. ## Decision Boundary The agent must treat the following as a **hard invariant** at the end of every reasoning step, before committing its token budget forward: - For each parent `invoke_agent` span, `count(exported execute_tool children) == count(tool calls recorded in the agent's own action log)`. - `sum(child.gen_ai.usage.input_tokens) + parent.gen_ai.usage.output_tokens` reconciles within a declared tolerance against the model's reported usage. If the child count does not match the action log, the agent must **not** silently proceed. It must either (a) re-emit the missing spans from its action log (the log is the authoritative source of truth; the trace is derived), or (b) mark the turn `gen_ai.telemetry.complete = false` and refuse to use the trace for any downstream token accounting. The decision boundary is: *the action log is primary, the trace is a projection, and any divergence between them is a telemetry fault, not an agent fault.* Conflating the two is what produces the misattribution above. ## Operational Effect Runtimes that enforce the reconciliation invariant convert a silent, compounding token-accounting drift into a bounded, detectable event. The concrete effects: - **Token budget integrity.** The agent's forward budget is computed from the action log, not the trace, so dropped spans cannot cause under-counting and over-spend. - **Fault attribution.** A child-count mismatch is logged as `TelemetryDropError`, distinct from `ToolExecutionError`, so on-call does not chase phantom hallucination bugs. - **Backpressure visibility.** The reconciliation failure is the *signal* that the BSP queue is saturated; it becomes the trigger to raise `max_queue_size` or add a fallback exporter, rather than a silent degradation discovered days later in a billing discrepancy. The cost is one O(n) comparison per reasoning step and a durable action log. The benefit is that the boundary between "the agent failed" and "the telemetry failed" remains legible — which is the entire point of instrumenting an agent at all. ```python import logging from dataclasses import dataclass log = logging.getLogger("agent.telemetry.reconcile") class TelemetryDropError(RuntimeError): """Raised when exported GenAI child spans do not match the agent action log. Distinguishes telemetry loss (BSP tail-drop) from genuine tool-execution failure so that token accounting is never silently corrupted. """ @dataclass(frozen=True) class TurnRecord: parent_span_id: str action_log_tool_calls: int # authoritative: recorded by the agent exported_tool_spans: int # derived: what the OTel backend received parent_output_tokens: int sum_child_input_tokens: int def reconcile_turn(record: TurnRecord, tolerance: float = 0.0) -> None: """Enforce the GenAI accounting invariant before the agent commits its budget. Invariant: exported child tool spans must equal the agent's own action log. A shortfall means the BatchSpanProcessor dropped spans under backpressure; the trace is incomplete and must not be used for token accounting. """ if record.exported_tool_spans < record.action_log_tool_calls: missing = record.action_log_tool_calls - record.exported_tool_spans raise TelemetryDropError( f"parent={record.parent_span_id}: {missing} of " f"{record.action_log_tool_calls} tool-call spans missing from export; " f"BSP queue likely saturated. Re-emit from action log before " f"committing token budget." ) if record.exported_tool_spans > record.action_log_tool_calls: raise TelemetryDropError( f"parent={record.parent_span_id}: export contains " f"{record.exported_tool_spans} child spans but action log has " f"{record.action_log_tool_calls}; duplicate or foreign spans detected." ) expected = record.parent_output_tokens + record.sum_child_input_tokens if expected <= 0: raise TelemetryDropError( f"parent={record.parent_span_id}: zero token accounting on a " f"completed turn; usage attributes were not populated." ) def commit_turn_budget(record: TurnRecord) -> int: """Return the token cost the agent may charge forward, after reconciliation.""" reconcile_turn(record) log.info( "turn %s reconciled: %d tool spans, %d tokens", record.parent_span_id, record.exported_tool_spans, record.parent_output_tokens + record.sum_child_input_tokens, ) return record.parent_output_tokens + record.sum_child_input_tokens ```