# Under what conditions must an instance treat a GenAI tool-call span as non-authoritative for token accounting during agent handoffs **Date of Record:** 2026 November 22 **Scope:** Applies to agents that emit OpenTelemetry GenAI spans (`gen_ai.*` attributes) for tool calls and token usage, and that hand off control to another agent instance whose spans are merged into the same trace. Does not apply to single-instance, single-span tool invocations where the emitting process is also the terminating process for the span. ## Core Distinction The GenAI semantic conventions define `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as attributes of the *model invocation span* (`gen_ai.operation.name = chat`). They define tool invocations as *child spans* (`gen_ai.operation.name = execute_tool`) that carry `gen_ai.tool.name`, `gen_ai.tool.call.id`, and — critically — no usage attributes of their own. The convention is silent on what a tool-call span means for token accounting when the tool call is *delegated*: the calling agent emits the `execute_tool` span, but the actual model invocation that consumes tokens happens inside a different agent instance, which emits its own `chat` span as a sibling or descendant. The boundary failure is this: **a tool-call span is a syntactic marker of delegation, not an accounting record.** When an agent hands off to a peer, the caller's `execute_tool` span and the callee's `chat` span are both present in the trace, but only the callee's `chat` span carries the `gen_ai.usage.*` attributes. Any aggregator that sums token usage by walking spans and attributing usage to the nearest enclosing tool-call span will double-count on retry, under-count on silent delegation, and silently mis-attribute on fan-out. The invariant that must hold is: *token usage is only authoritative on spans where `gen_ai.operation.name` is a model-invocation operation AND the span's `gen_ai.tool.call.id` is absent or matches an unbroken call chain.* Tool-call spans are structural, not fiscal. ## What Was Observed A two-agent pipeline: an orchestrator (Agent A) calls a retrieval tool implemented as a delegated sub-agent (Agent B), which itself performs a `chat` invocation against a model to synthesize a retrieval query, then calls a vector store. Both agents emit GenAI spans into one OTel trace via a shared collector. The trace contained, in order: - `chat` span (A, planning) — `gen_ai.usage.input_tokens=1840`, `output_tokens=212` - `execute_tool` span (A) — `gen_ai.tool.name="retrieval"`, `gen_ai.tool.call.id="call_01HX..."` - `chat` span (B, query synthesis) — `gen_ai.usage.input_tokens=610`, `output_tokens=88`, parented to A's `execute_tool` span - `execute_tool` span (B) — `gen_ai.tool.name="vector_search"` - `chat` span (A, synthesis of final answer) — `gen_ai.usage.input_tokens=2410`, `output_tokens=340` The token-accounting job walked the trace, and for each `execute_tool` span, summed the usage of all descendant `chat` spans, then added the top-level `chat` spans. Agent A's `execute_tool` span received credit for B's 610/88 tokens, *and* those tokens were also counted in the top-level walk because the aggregator's traversal was depth-first with a "visited" set keyed on span ID — but B's `chat` span had been re-emitted with a new span ID on a retry of the tool call (B's model call timed out at 30s, was retried, and the retry succeeded). The first attempt's span was orphaned — parented to the same `execute_tool` span but with `otel.status_code=ERROR` — and its 610/88 tokens were counted alongside the retry's 610/88. Total attributed to the retrieval tool call: 1220/176. Actual billed tokens for B: 610/88 (the first attempt was billed but discarded; the retry was billed and used). The deeper observation: the aggregator had no way to distinguish "first attempt, discarded" from "retry, used" because the GenAI conventions do not require a retry-attempt attribute. `gen_ai.tool.call.id` was identical across both attempts (it identifies the *logical* call, not the attempt). The only signal was `otel.status_code`, which is a span-level attribute from the OTel core conventions, not the GenAI ones — so a GenAI-only consumer would not know to look for it. ## Condition of Validity The invariant holds — tool-call spans are non-authoritative for token accounting — under these conditions, all of which must be checked: 1. **Delegation depth > 0.** The tool call is implemented by a process other than the span emitter, evidenced by a descendant `chat` span whose `service.name` (resource attribute) differs from the `execute_tool` span's `service.name`. If they match, the tool call is in-process and the child `chat` span's usage is safely attributable to the caller. 2. **Call ID is logical, not physical.** `gen_ai.tool.call.id` is stable across retries by design (it maps to the model's tool-call token). Therefore any accounting that keys on `gen_ai.tool.call.id` must deduplicate by `(tool.call.id, attempt)` where `attempt` is derived from `otel.status_code` + span start time ordering, not from the call ID alone. 3. **Usage attributes are present only on model-invocation spans.** If a `execute_tool` span carries `gen_ai.usage.*` attributes (some SDKs emit them as a convenience aggregate), those attributes are *derived* and must be treated as non-authoritative; the authoritative record is the descendant `chat` spans. A tool-call span with usage attributes and no descendant `chat` span is a red flag: either the tool is a non-LLM tool that mis-emitted usage, or the descendant span was dropped by sampling. 4. **The trace is complete (no sampling gap).** If head-based sampling dropped B's `chat` span but kept A's `execute_tool` span, the tool-call span will appear to have zero usage. Any aggregator that treats zero-usage tool-call spans as "free" will under-count. The condition of validity requires either tail-based sampling that keeps all spans of a trace, or an explicit `gen_ai.usage.*` fallback on the tool-call span that is marked as derived. When any of these conditions fails, the tool-call span is not merely non-authoritative — it is actively misleading, and the aggregator must refuse to attribute usage to it. ## Decision Boundary The boundary is between *structural attribution* (which span is the parent of which) and *fiscal attribution* (which span's usage counts toward which logical operation). The GenAI conventions give you structural attribution for free via parent-child span relationships. They do not give you fiscal attribution, because fiscal attribution requires knowing: - whether a retry's first attempt was billed (it usually was, by the provider, even if discarded by the agent), - whether a delegated sub-agent's usage should be charged to the caller's tool call or to the sub-agent's own budget line, - whether a fan-out tool call (one `execute_tool` span, N parallel `chat` spans from N sub-agents) should split usage evenly or attribute by sub-agent identity. The decision boundary is crossed the moment an aggregator assumes `execute_tool` span → sum of descendant `chat` usage. That assumption is valid only for in-process tools (condition 1 fails) and only for single-attempt calls (condition 2 fails on retry). For any delegated, retried, or fanned-out tool call, the aggregator must instead: - key fiscal attribution on `(gen_ai.tool.call.id, attempt_index)` where attempt_index is computed from span ordering within the parent, - treat `otel.status_code=ERROR` spans as *billed but discarded* and record them in a separate `discarded_usage` counter rather than summing them into the tool call's usage, - for fan-out, attribute by `service.name` of the descendant `chat` span, not by the parent tool-call span. The guardrail is a *fiscal attribution pass* that runs after span collection and before any token report, and that raises on any `execute_tool` span whose descendant `chat` spans have inconsistent `gen_ai.tool.call.id` values or whose usage cannot be reconciled with the provider's billing record for the same time window. ## Operational Effect Downstream, a billing reconciliation job compared the trace-derived token total against the provider's invoice for the same hour. The trace total was 14% higher than the invoice — exactly the retry double-count plus a fan-out mis-attribution where a 3-way parallel retrieval had its 3 sub-agent `chat` spans summed into a single tool-call span that was then also counted at the top level. The reconciliation job's tolerance was 2%, so it failed the batch and paged the on-call. The on-call spent 40 minutes tracing the discrepancy to the `execute_tool` span's usage sum before finding that the GenAI conventions never promised that sum was meaningful. The fix was not to change the spans but to change the aggregator: it now refuses to attribute usage to any `execute_tool` span whose resource `service.name` differs from a descendant `chat` span's resource `service.name`, and it emits a `fiscal_attribution_skipped` counter for those spans. The counter's rate is now a leading indicator of delegation depth in the pipeline — when it spikes, a new sub-agent has been introduced without updating the attribution rules. The operational lesson: in GenAI tracing, the span tree tells you *what called what*, and the `gen_ai.usage.*` attributes tell you *what a model invocation cost*, but nothing in the conventions tells you *which logical operation should be billed for a delegated call*. That mapping is your responsibility, and it must be encoded as an explicit guardrail — because the default traversal (sum descendants, attribute to parent) is wrong for every delegated, retried, or fanned-out tool call, which is to say, for most real agent systems. ```python class FiscalAttributionError(RuntimeError): """Raised when a tool-call span cannot be safely attributed token usage.""" pass def attribute_usage(tool_span, descendant_chat_spans): """ Attribute token usage to a GenAI execute_tool span. Returns (authoritative_usage, discarded_usage) where authoritative_usage is the usage of the successful attempt(s) and discarded_usage is the usage of failed attempts that were still billed by the provider. """ if not descendant_chat_spans: # Tool span with no descendant chat spans: either a non-LLM tool # (fine, zero usage) or a sampling gap (not fine). if tool_span.attributes.get("gen_ai.usage.input_tokens", 0) > 0: raise FiscalAttributionError( f"tool span {tool_span.span_id} carries usage attributes " f"but has no descendant chat span; sampling gap or mis-emission" ) return (0, 0) tool_service = tool_span.resource.get("service.name") call_ids = {s.attributes.get("gen_ai.tool.call.id") for s in descendant_chat_spans} if len(call_ids) > 1: raise FiscalAttributionError( f"tool span {tool_span.span_id} has descendant chat spans with " f"inconsistent gen_ai.tool.call.id values: {call_ids}; " f"fan-out must be attributed per sub-agent, not per tool call" ) services = {s.resource.get("service.name") for s in descendant_chat_spans} if services != {tool_service}: # Delegated call: usage belongs to the sub-agent's budget line, # not the caller's tool call. Refuse to aggregate. raise FiscalAttributionError( f"tool span {tool_span.span_id} (service={tool_service}) has " f"descendant chat spans from services {services}; " f"delegated usage must not be summed into the caller's tool call" ) authoritative = 0 discarded = 0 for span in descendant_chat_spans: usage = span.attributes.get("gen_ai.usage.input_tokens", 0) + \ span.attributes.get("gen_ai.usage.output_tokens", 0) if span.attributes.get("otel.status_code") == "ERROR": discarded += usage else: authoritative += usage return (authoritative, discarded) ```