Under what conditions must an instance treat GenAI semantic-convention token counts as non-attributable to the emitting tool call
**Date of Record:** 2026 December 1 **Scope:** Applies to single- and multi-tool agent runtimes that emit OpenTelemetry `gen_ai.*` spans and attribute token cost to the tool call whose span they nest inside. Does not apply to single-shot completion endpoints where one span equals one billing unit with no nested tool spans.
Core Distinction
The GenAI semantic convention defines `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as properties of a *model invocation*, not of a *tool call*. When an agent framework nests a tool span under the model span that decided to call the tool, the tool span inherits the model span's `parent_span_id` but not its usage attributes. Cost attribution systems that walk the span tree and sum `gen_ai.usage.*` per leaf frequently double-count, under-count, or misattribute because the model invocation that *produced* the tool call and the model invocation that *consumed* the tool result are two distinct spans that share a parent.
The distinction that matters operationally: **token usage is a property of the span that carries the `gen_ai.operation.name` attribute, not of the span that carries the tool name.** Any aggregator that keys on tool name while summing usage attributes from descendants of the model span will attribute the *decision* tokens to the *executed* tool.
What Was Observed
In a runtime emitting spans per the GenAI convention (v1.27+), a single agent turn produced this span tree:
span: chat (gen_ai.operation.name=chat, gen_ai.usage.input_tokens=4820, output_tokens=310)
└─ span: execute_tool (gen_ai.tool.name=search_docs, no usage attrs)
└─ span: chat (gen_ai.operation.name=chat, gen_ai.usage.input_tokens=5310, output_tokens=204)
└─ span: execute_tool (gen_ai.tool.name=summarize, no usage attrs)
The observability pipeline attributed 4,820 + 310 tokens to `search_docs` (the first tool span, because it was the first child of the first chat span) and 5,310 + 204 tokens to `summarize`. Ground truth: `search_docs` consumed zero model tokens; it is a retrieval call. The 4,820/310 belongs to the *planning* invocation whose output happened to be a tool-call directive. The 5,310/204 belongs to the *synthesis* invocation that received `search_docs` output.
The misattribution is structural. The first chat span's usage describes the model deciding to call `search_docs`. That decision is not caused by `search_docs`; it precedes it. Attributing decision tokens to the tool inverts causality and inflates the apparent cost of cheap tools (retrieval, lookup) while deflating the cost of expensive ones (long-context synthesis) whose consuming invocation is a *sibling* of the tool span, not an ancestor.
A second observed failure: when a tool call fails and the agent retries, the retry emits a new `chat` span under the same parent. The failed tool span has no usage; the retry chat span carries the full re-planning cost. Naive aggregation attributes the retry cost to the tool that failed, producing a per-tool cost series that spikes on flaky tools even though the model's re-planning is the cost driver.
Condition of Validity
The attribution is valid **only if** all of the following hold:
- The aggregator keys token sums on spans bearing `gen_ai.operation.name ∈ {chat, text_completion, embeddings}` and never on spans bearing `gen_ai.tool.name`.
- The tool span and the model span that decided to invoke it are *siblings*, and the aggregator does not descend from a model span into its tool children when summing usage.
- `gen_ai.usage.*` attributes are present on the model span at span-end (they are set after the response is received, not at span-start; a span exported before completion carries zero and must be treated as *unknown*, not *zero*).
- Retries are represented as distinct model spans, each with its own usage, not as repeated attributes on a single span.
When any of these fail, the per-tool token figure is not an attribution — it is a coincidental sum of whatever usage attributes happened to be reachable by the tree walk.
Decision Boundary
The boundary is **span-export timing versus span-tree finalization**. A model span's usage attributes are written at span-end, after the streamed response completes. A tool span can be exported and its cost flushed to the billing pipeline *before* the sibling model span that will consume its output has even started. This creates a temporal inversion: the tool's cost row is written first, the consuming model span's usage arrives milliseconds to seconds later, and any streaming aggregator that commits per-span cannot retroactively bind them.
The correct decision at this boundary: **do not commit per-tool cost at tool-span end.** Buffer until the parent turn's model spans are all closed, then attribute the sum of model-span usage to the turn, and attribute to individual tools only the *tool-side* costs (external API calls, sandbox compute) that the tool span actually carries. Token cost is a turn-level quantity; tool cost is a tool-level quantity; conflating them at the span boundary produces the inversion above.
A second boundary: when `gen_ai.usage.input_tokens` is absent (provider does not emit it, or span truncated), the aggregator must raise rather than substitute a heuristic estimate. A heuristic estimate that silently fills the gap destroys the invariant that token sums are reconcilable to provider billing. The system must fail closed on missing usage.
Operational Effect
Runtimes that enforce the span-typing invariant see per-tool cost series that are stable under retry storms (retry cost lands on the turn, not the tool) and that reconcile to provider invoices within rounding. Runtimes that key on tool name see per-tool cost that moves with model behavior — a prompt change that makes the model plan more verbosely inflates every tool's apparent cost simultaneously, which is the signature of the misattribution. The diagnostic: if all tools' costs move together across a prompt-only change, the aggregator is attributing decision tokens, not tool tokens.
The guardrail is a span-typing assertion at aggregation time: a token-bearing span must carry `gen_ai.operation.name`; a tool span must not carry `gen_ai.usage.*`; and the aggregator must refuse to sum usage across a parent-child edge where the child is a tool span. This is enforced below.
class TokenAttributionError(RuntimeError):
"""Raised when a span tree cannot be attributed without violating the typing invariant."""
pass
TOOL_ATTR = "gen_ai.tool.name"
OP_ATTR = "gen_ai.operation.name"
USAGE_KEYS = ("gen_ai.usage.input_tokens", "gen_ai.usage.output_tokens")
def attribute_turn_tokens(spans: list[dict]) -> dict:
"""
spans: list of span dicts with keys: span_id, parent_span_id, attributes.
Returns {"turn_tokens": int, "per_tool_tool_cost": {tool: int}, "model_spans": int}.
Raises TokenAttributionError if a tool span carries usage attrs, or if a
model span is missing usage attrs entirely (must fail closed, not estimate).
"""
usage_by_model_span = {}
tool_spans = []
for s in spans:
attrs = s.get("attributes", {})
is_tool = TOOL_ATTR in attrs
is_model = OP_ATTR in attrs and attrs[OP_ATTR] in ("chat", "text_completion", "embeddings")
if is_tool and any(k in attrs for k in USAGE_KEYS):
raise TokenAttributionError(
f"tool span {s['span_id']} ({attrs[TOOL_ATTR]}) carries usage attrs; "
"usage belongs to the model span, not the tool span"
)
if is_model:
missing = [k for k in USAGE_KEYS if k not in attrs]
if missing:
raise TokenAttributionError(
f"model span {s['span_id']} missing {missing}; "
"refusing to estimate — span may have exported before completion"
)
usage_by_model_span[s["span_id"]] = sum(attrs[k] for k in USAGE_KEYS)
if is_tool:
tool_spans.append(s)
# Invariant: no tool span may be an ancestor of a model span whose usage
# we are about to sum. If it is, the tree is malformed for attribution.
model_ids = set(usage_by_model_span)
for s in spans:
if s["span_id"] in model_ids:
parent = s.get("parent_span_id")
for t in tool_spans:
if t["span_id"] == parent:
raise TokenAttributionError(
f"model span {s['span_id']} is nested under tool span {parent}; "
"usage cannot be attributed to the tool — it precedes the tool's effect"
)
return {
"turn_tokens": sum(usage_by_model_span.values()),
"per_tool_tool_cost": {t["attributes"][TOOL_ATTR]: 0 for t in tool_spans},
"model_spans": len(model_ids),
}