# In what scenarios must an instance reject an OpenTelemetry GenAI span tree when token/cost attributes disagree with the tool-call event ledger **Date of Record:** 2026 November 21 **Scope:** Applies to any agent runtime that emits OpenTelemetry GenAI spans (`gen_ai.*` attributes) per the semantic conventions and reconciles them against an out-of-band tool-call event ledger for billing, replay, or audit. Does not apply to runtimes that treat spans as purely observational telemetry with no downstream cost or replay authority. ## Core Distinction The GenAI semantic conventions define two *independent* accounting surfaces that are never required to agree: the **span attribute surface** (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.operation.name`, `gen_ai.tool.name`, nested under a parent `invoke_agent` span) and the **event surface** (span events / log records of type `gen_ai.tool.call` carrying `gen_ai.tool.call.id`, arguments, and results). The conventions specify *how* to serialize each surface. They do **not** specify that the sum of tool-call events must reconcile with the token totals on the enclosing agent span, nor that a tool-call event's `call.id` must appear exactly once across the span tree. This is the non-obvious boundary. A span tree can be **fully spec-conformant** — every attribute name correct, every required field present, every event well-formed — while being **internally inconsistent**: the agent span reports 4,200 output tokens, but the tool-call events beneath it reference nine calls, three of which were retried and re-emitted, and the completion that produced the final answer was truncated at a token budget that the span does not record. The conventions validate each surface in isolation; the invariant that binds them is an *operator* invariant, not a spec invariant. An instance that assumes spec-conformance implies cost-consistency will silently double-bill or under-bill. ## What Was Observed A research agent running a `invoke_agent` span with `gen_ai.operation.name="invoke_agent"` and `gen_ai.agent.name="researcher-v3"` executed a plan of tool calls against a search API and a code interpreter. The span tree, as exported: - Parent `invoke_agent` span: `gen_ai.usage.input_tokens=18,940`, `gen_ai.usage.output_tokens=4,201`. - Child `execute_tool` spans: 11, of which 3 were retries of the same `gen_ai.tool.name="web_search"` call after a 429, each carrying a distinct `gen_ai.tool.call.id` (the SDK generated a fresh UUID per attempt rather than propagating the logical call identity). - Two `gen_ai.tool.call` events emitted on the parent span itself (the runtime emitted a summary event in addition to the child span), duplicating two of the child spans' calls. - No `gen_ai.usage.*` on any child `execute_tool` span — correct per convention when the tool is not an LLM, but the runtime's cost aggregator summed *only* spans bearing usage attributes, so the retried calls' latency and any associated model-side overhead were invisible. The downstream cost ledger, built by summing `gen_ai.usage.*` across the tree, reported 18,940 + 4,201 tokens once — correct for the model. But the **tool-call audit ledger**, built by counting `gen_ai.tool.call.id` values, reported 13 calls (11 child spans + 2 parent events), of which 3 were retries and 2 were duplicates. The reconciliation job flagged a mismatch and, because it had no rule for "expected duplication," quarantined the entire trace. The agent's task had already completed; the trace was the only record of what it did. The trigger condition is precise: **the same logical tool invocation acquires more than one `gen_ai.tool.call.id` across retries, and the runtime also emits a summary event on the parent span.** Neither is a convention violation. Together they make the event surface non-injective onto logical calls, and the span surface's token totals cannot disambiguate because tool spans carry no usage attributes. ## Condition of Validity The reconciliation invariant holds only under all of the following: 1. **Call-identity stability across retries.** The runtime must propagate a single logical `gen_ai.tool.call.id` through every retry attempt (typically by minting the id before the first attempt and reusing it, recording attempt count in a separate `retry.attempt` attribute). If the id is regenerated per attempt, the event surface is not injective and no count-based reconciliation is valid. 2. **Event/span non-duplication.** A tool call must be represented *either* as a child span *or* as a parent-span event, not both. The conventions permit both; a reconciler must pick one as authoritative and treat the other as a projection, with a dedup key. 3. **Usage-attribute placement.** Token totals on the `invoke_agent` span must be understood as *aggregate model usage for the whole agent turn*, including tokens spent on intermediate reasoning that produced tool-call arguments. A reconciler that compares tool-call *count* to output tokens is comparing incomparable quantities; the only valid comparison is aggregate usage vs. the sum of per-inference usage if the runtime emits per-inference child spans. 4. **Truncation is recorded.** If the final completion hit a token budget, the span must carry an explicit marker (e.g., `gen_ai.response.finish_reasons=["length"]`). Absent this, a truncated 4,201-token output is indistinguishable from a naturally-terminated one, and the cost ledger under-counts the true model spend by the budget ceiling. When any of these fails, the span tree is *spec-valid but reconciliation-invalid*, and an instance must not treat it as authoritative for cost or replay. ## Decision Boundary The instance must **reject** the trace for cost/replay authority — raising rather than quarantining-and-proceeding — when: - `count(distinct gen_ai.tool.call.id)` exceeds the number of distinct `(gen_ai.tool.name, normalized_arguments_hash)` pairs by more than the recorded retry count, indicating either id instability or event/span duplication that the dedup key cannot resolve. - The parent `invoke_agent` span lacks `gen_ai.response.finish_reasons` while `gen_ai.usage.output_tokens` equals a configured budget ceiling exactly (the truncation signature). - Any child `execute_tool` span that *did* invoke an LLM (nested `gen_ai.operation.name="chat"`) lacks `gen_ai.usage.*`, meaning aggregate parent usage double-counts or the child usage is lost. The instance must **accept with annotation** when retries are present but ids are stable and retry count is recorded, or when a parent summary event exists but carries a `dedup_of` attribute pointing at the child span's call id. ## Operational Effect Rejecting on these boundaries converts a silent billing error into a loud, attributable failure at trace-ingest time, before the ledger is written. The practical consequence is that the agent runtime is forced to mint call ids *before* the first attempt and to emit exactly one authoritative representation per call — a small change to the SDK's span-emission path that makes the event surface injective and the reconciliation total. Without it, retries and summary events accumulate as phantom calls, and the only defense is a human reading the trace. With it, the invariant "one logical call, one id, one authoritative record" is enforceable by a machine, and the GenAI span tree becomes usable as a cost and replay ledger rather than merely as observability decoration. ```python from dataclasses import dataclass from typing import Sequence class GenAiSpanReconciliationError(RuntimeError): """Raised when a GenAI span tree is spec-valid but reconciliation-invalid.""" @dataclass(frozen=True) class ToolCallRecord: call_id: str tool_name: str args_hash: str is_retry: bool represented_as: str # "span" | "event" @dataclass(frozen=True) class AgentSpanSummary: input_tokens: int output_tokens: int finish_reasons: Sequence[str] budget_ceiling: int tool_calls: Sequence[ToolCallRecord] def assert_reconcilable(span: AgentSpanSummary) -> None: logical = {(c.tool_name, c.args_hash) for c in span.tool_calls} distinct_ids = {c.call_id for c in span.tool_calls} retries = sum(1 for c in span.tool_calls if c.is_retry) if len(distinct_ids) - len(logical) > retries: raise GenAiSpanReconciliationError( f"call-id surface non-injective: {len(distinct_ids)} ids for " f"{len(logical)} logical calls with only {retries} retries; " "id instability or event/span duplication is unresolvable" ) if not span.finish_reasons and span.output_tokens >= span.budget_ceiling: raise GenAiSpanReconciliationError( f"truncation signature: output_tokens={span.output_tokens} equals " f"budget_ceiling={span.budget_ceiling} with no finish_reasons; " "trace is spec-valid but cost-under-counting" ) authoritative = [c for c in span.tool_calls if c.represented_as == "span"] projected = [c for c in span.tool_calls if c.represented_as == "event"] span_ids = {c.call_id for c in authoritative} for ev in projected: if ev.call_id not in span_ids: raise GenAiSpanReconciliationError( f"event-only call {ev.call_id} on {ev.tool_name} has no " "authoritative span record; dedup key cannot bind it" ) ```