# Why does an instance lose tool-call attribution when OTel GenAI spans cross an agent handoff boundary? **Date of Record:** 2026 November 21 **Scope:** Applies to multi-agent systems emitting OpenTelemetry GenAI spans (`gen_ai.*` semantic conventions) where a parent agent delegates a tool invocation or subtask to a child agent instance that runs in a separate process, model context, or execution runtime. Does not apply to single-process, single-agent traces where the tool call is executed inline within the same span context, nor to purely synchronous function calls that never cross an instance boundary. ## Core Distinction The failure is not "missing spans" and it is not "wrong parent_id." It is **attribution collapse at the GenAI attribute layer**: the child instance emits a well-formed `execute_tool` span with correct `trace_id`, correct `span_id`, and correct `parent_span_id` pointing at the delegating agent span — yet the `gen_ai.tool.name`, `gen_ai.usage.input_tokens`, and `gen_ai.usage.output_tokens` attributes on that child span are *semantically orphaned* from the parent's accounting because the parent's `gen_ai.agent.name` / conversation identity was never propagated as span attributes, only as trace structure. Trace structure survives the handoff. **Attribute identity does not.** A trace backend that aggregates token spend by `gen_ai.agent.name` or by conversation will silently double-count, under-count, or drop the child's tokens, even though every span is present and every parent link is intact. The invariant that breaks is: *token totals must be reconstructible from span attributes alone, without relying on trace topology.* GenAI conventions violate this the moment a handoff occurs, because topology carries the relationship while attributes carry the quantities, and only the topology crosses the boundary. ## What Was Observed A two-tier agent: a **Planner** (agent A) that decomposes a task and delegates a retrieval subtask to a **Retriever** (agent B) running in a separate worker. Both instrumented with the OTel GenAI conventions. Observed span sequence: - Agent A emits `invoke_agent` span `S_A` with attributes `gen_ai.agent.name=planner`, `gen_ai.operation.name=invoke_agent`, `gen_ai.usage.input_tokens=1840`, `gen_ai.usage.output_tokens=210`. - Agent A emits `execute_tool` span `S_T` (a `delegate_to_retriever` tool) with `gen_ai.tool.name=delegate_to_retriever`, `parent_span_id=S_A.span_id`. **No `gen_ai.agent.name` on `S_T`.** The tool span belongs to the *caller's* agent identity, which is correct per convention. - The delegation payload sent to B contains `traceparent` (trace_id, S_T.span_id) but **not** `gen_ai.agent.name`, **not** the conversation/thread identifier, and **not** the parent's token counters. - Agent B starts fresh, creates `S_B` = `invoke_agent` with `gen_ai.agent.name=retriever`, `parent_span_id=S_T.span_id`, `gen_ai.usage.input_tokens=920`, `gen_ai.usage.output_tokens=88`. - Agent B emits a nested `execute_tool` span `S_B1` with `gen_ai.tool.name=vector_search`, `gen_ai.usage.*` = 0 (tool itself consumes no model tokens), `parent_span_id=S_B.span_id`. Now the accounting query `sum(gen_ai.usage.input_tokens) group by gen_ai.agent.name` returns `planner=1840, retriever=920` — and the total conversation cost is reported as 2760 input tokens. But the *true* cost of the delegated subtask includes the 1840 tokens Agent A already spent reasoning about the delegation, which the retriever's span does not and cannot attribute to itself. Worse: if the orchestrator retries the delegation (a common agent-loop behavior), `S_T` is re-emitted under a *new* `S_A` invocation, and the retriever's 920 tokens attach to the *retry's* parent — so the *first* attempt's retriever cost, if it was emitted before the retry, now hangs off an orphaned `S_T` whose parent `S_A` was superseded. Token sums per agent remain individually correct; token sums *per task* become non-reconstructible. The second observation: `gen_ai.tool.call.id` (the convention's tool-call correlation attribute) is generated independently by A and by B. A's `S_T` carries `tool.call.id=A-7`, B's `S_B1` carries `tool.call.id=B-3`. There is no field linking them. A downstream verifier that wants to check "did the retriever actually execute the tool the planner requested" has no attribute-level join key — only the fragile topology path `S_A → S_T → S_B → S_B1`. If any span in that chain is sampled out (tail-sampling at the collector, which is standard), the join is destroyed and the tool call becomes unverifiable. ## Condition of Validity The invariant "token totals and tool-call attribution are reconstructible from attributes alone" holds **only if** every cross-instance handoff propagates a stable set of identity attributes as span attributes on the child's root span, not merely as trace context. Specifically, the child's `invoke_agent` span must carry: - the parent's `gen_ai.agent.name` (or a `gen_ai.conversation.id` / task id) as an explicit attribute, so token aggregation can be grouped by *task* rather than by *emitting agent*; - the originating `gen_ai.tool.call.id` (or a propagated `gen_ai.tool.call.parent_id`) so the delegated execution can be joined to the delegating tool span without relying on topology; - a propagated budget/counter snapshot (`gen_ai.usage.input_tokens` already consumed upstream) so that the child's spend can be reported as *incremental* against a known baseline rather than as an absolute that the parent cannot see. The convention as written provides `gen_ai.agent.name` and `gen_ai.tool.call.id` as span attributes but does **not** mandate their propagation across `traceparent`-only handoffs. The moment the transport carries only W3C trace context (the default for most agent-to-agent transports, including bare MCP and A2A `traceparent` headers), the attributes are dropped at the boundary and the invariant fails silently. It is valid only when the handoff envelope is *attribute-bearing*, not context-only. ## Decision Boundary The boundary is between **topology-trusting** and **attribute-trusting** accounting, and it must be chosen per-query, not per-system: - If the backend guarantees full-trace retention (no sampling, no span drops) **and** the query engine can traverse arbitrary parent chains, topology-based attribution is *correct* but *fragile* — a single dropped span severs it. - If the backend samples, or if spans can be emitted out of order across instances, or if any agent can be retried/superseded, attribute-based attribution is **required** and topology-based is **unsound**. - The crossover: any system where `P(span dropped) × (chain length) > tolerance` must switch to attribute-bearing handoffs. For a 4-hop agent chain at 10% tail-sampling, expected chain survival is ~0.66 — meaning a third of delegated tool calls are unverifiable by topology alone. The decision rule: **never let a cross-instance handoff carry only `traceparent`.** Either propagate the identity attribute set in the handoff envelope and re-emit it as span attributes on the child root, or accept that per-task token accounting and tool-call verification are best-effort and must be marked as such in any downstream audit. ## Operational Effect When the boundary is crossed without attribute propagation, three concrete operational failures follow, in order of severity: 1. **Token-accounting drift under retry.** Retried delegations re-parent the child's token spend under a new parent invocation. Per-agent sums stay stable; per-task sums diverge from ground truth by the cost of every superseded attempt. Budget enforcement keyed on per-task totals under-counts by exactly the retry overhead — the failure mode that most often triggers a runaway agent loop before the budget guard fires. 2. **Tool-call verification gaps.** Without a propagated `tool.call.id`, a verifier cannot assert "the tool the planner requested is the tool the retriever ran" from attributes. Under sampling, it cannot assert it from topology either. The verification replay silently degrades from *proof* to *plausibility*. 3. **Silent, not loud.** No span is malformed, no parent link is broken, no error is emitted. The trace looks healthy. The corruption is entirely in the *aggregation semantics*, which is why it survives code review and only surfaces in cost reconciliation or a post-incident audit weeks later. The guardrail is a handoff-envelope contract enforced at the transport layer: any agent-to-agent message must carry `gen_ai.agent.name`, the originating `gen_ai.tool.call.id`, and the upstream token baseline, and the receiving instance must emit them as span attributes on its root `invoke_agent` span before any child span is created. Enforce it by rejecting handoffs whose envelope lacks these fields — fail the delegation at the sender, loudly, rather than emit a well-formed but semantically orphaned trace. ```python from dataclasses import dataclass, field from typing import Optional class HandoffAttributionError(RuntimeError): """Raised when a cross-instance agent handoff would emit spans whose GenAI attributes cannot be joined to the delegating agent's accounting.""" pass @dataclass class HandoffEnvelope: traceparent: str # W3C trace context: trace_id-span_id-flags agent_name: Optional[str] = None # gen_ai.agent.name of the DELEGATING agent tool_call_id: Optional[str] = None # gen_ai.tool.call.id of the delegating tool span upstream_input_tokens: int = 0 # gen_ai.usage.input_tokens already spent upstream upstream_output_tokens: int = 0 # gen_ai.usage.output_tokens already spent upstream @dataclass class ChildRootSpan: agent_name: str parent_span_id: str attributes: dict = field(default_factory=dict) REQUIRED_ATTRS = ("gen_ai.agent.name", "gen_ai.tool.call.id") def build_child_root_span(env: HandoffEnvelope, child_agent_name: str) -> ChildRootSpan: """Construct the child instance's root invoke_agent span. Invariant: the child root span MUST carry the delegating agent's identity and tool-call correlation id as ATTRIBUTES, not merely as a parent link, so that per-task token accounting and tool-call verification survive span sampling and delegation retries. """ if not env.traceparent: raise HandoffAttributionError( "handoff envelope missing traceparent; cannot establish even topology" ) missing = [ name for name, val in ( ("gen_ai.agent.name", env.agent_name), ("gen_ai.tool.call.id", env.tool_call_id), ) if not val ] if missing: raise HandoffAttributionError( f"handoff from parent agent to '{child_agent_name}' is context-only; " f"missing attribute-bearing identity {missing}. Token totals and tool-call " f"attribution will be non-reconstructible from attributes alone." ) # traceparent format: 00--- parts = env.traceparent.split("-") if len(parts) != 4: raise HandoffAttributionError( f"malformed traceparent '{env.traceparent}': expected 4 dash-delimited fields" ) parent_span_id = parts[2] child = ChildRootSpan(agent_name=child_agent_name, parent_span_id=parent_span_id) child.attributes = { "gen_ai.operation.name": "invoke_agent", "gen_ai.agent.name": child_agent_name, # propagated identity — the point of the whole guardrail "gen_ai.agent.parent.name": env.agent_name, "gen_ai.tool.call.id": env.tool_call_id, "gen_ai.tool.call.parent_id": env.tool_call_id, # upstream baseline so child spend is reported as incremental "gen_ai.usage.input_tokens.upstream": env.upstream_input_tokens, "gen_ai.usage.output_tokens.upstream": env.upstream_output_tokens, } for required in REQUIRED_ATTRS: if required not in child.attributes: raise HandoffAttributionError( f"child root span for '{child_agent_name}' missing required attribute {required}" ) return child def verify_task_token_reconstructible(spans: list[ChildRootSpan]) -> int: """Sum incremental token spend across a delegated task tree. Raises if any span lacks the propagated baseline needed to attribute its spend to the originating task rather than only to its emitting agent. """ total = 0 for span in spans: if "gen_ai.tool.call.parent_id" not in span.attributes: raise HandoffAttributionError( f"agent '{span.agent_name}' emitted a root span without a propagated " f"tool-call parent id; per-task token sum is unsound" ) total += span.attributes.get("gen_ai.usage.input_tokens.upstream", 0) return total ```