# Under what conditions must an instance refuse to emit a `gen_ai.*` tool-call span when its parent context is a foreign-trace continuation **Date of Record:** 2026 November 22 **Scope:** Applies to any agent runtime that emits OpenTelemetry GenAI semantic-convention spans (`gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.tool.call.id`) for tool invocations whose `traceparent` was inherited from an upstream agent, gateway, or peer over MCP/A2A. Does not apply to single-process agents that own their entire trace root. ## Core Distinction There are two incompatible definitions of "the token count for a span," and the GenAI semantic conventions only formalize one of them. The **cumulative-usage** reading treats `gen_ai.usage.input_tokens` as the model's total prompt consumption for that completion — including the system prompt, the accumulated tool-result history, and the retrieved context — regardless of which upstream span caused those tokens to be present. The **attributable-usage** reading treats the same attribute as the cost *incurred by this span's operation* — i.e., the delta that would not have been spent had this tool call not fired. These diverge the moment an agent inherits a `traceparent` from a foreign trace root. The inherited context carries a `tracestate` and a span ancestry that the local SDK did not author, and the local runtime typically replays the full conversation history into the model to produce the completion that decides the tool call. Under cumulative accounting, the child span's `input_tokens` now reports the entire replayed history — which was *already* counted by the parent agent that assembled it. The trace looks complete; the sum over the trace is inflated by exactly the replayed prefix at every hop. This is the boundary: **an instance must not emit a GenAI tool-call span with cumulative token attributes onto a trace whose root it does not own**, because the trace's accounting invariant (sum of leaf usage = root cost) is silently violated, and no downstream consumer can detect it from the span data alone. ## What Was Observed A two-hop pipeline: Agent A (orchestrator, trace root owner) calls Agent B over A2A with a propagated `traceparent`. B invokes a tool through MCP, gets a result, and replays the full accumulated context (system prompt ~1.8k tokens, prior turns ~4.2k tokens, tool result ~0.9k tokens) into a completion that emits the next tool call. B's SDK auto-instruments the `gen_ai.chat` span and the child `gen_ai.execute_tool` span. Observed span attributes on B's tool span: - `gen_ai.usage.input_tokens = 6900` (the full replayed prompt) - `gen_ai.usage.output_tokens = 210` - `gen_ai.tool.call.id = "call_8f2..."` (locally generated, not the upstream call id) - `gen_ai.operation.name = "execute_tool"` The parent span in A recorded `gen_ai.usage.input_tokens = 6900` for the *same* prompt it shipped to B. Summing leaf `input_tokens` across the trace yields 13,800 for a conversation whose true peak prompt was 6,900. The billing reconciler, which sums leaf usage by `trace_id`, over-reports by 100%. Worse, the `gen_ai.tool.call.id` on B's span does not match the call id A expects in its own tool-result correlation, so the replay-based verifier that checks "every tool call has a matching result" flags a phantom orphan. The failure is silent: every span is individually well-formed and passes schema validation. The invariant break is at the trace level, and it is invisible to per-span validators. ## Condition of Validity The cumulative-usage convention is valid **only** when the emitting instance is the trace root owner — i.e., the span's `trace_id` was minted by this process and the `parent_span_id` chain is entirely locally authored. In that case the replayed prefix is genuinely this trace's cost, and cumulative attribution is correct. The convention is **invalid** when any of the following hold on the incoming context: 1. The `traceparent` was received over the wire (A2A, MCP `_meta.traceparent`, HTTP header) and the local process did not generate the `trace_id`. 2. The incoming `tracestate` contains vendor keys the local SDK does not recognize, indicating a foreign instrumentation layer owns part of the chain. 3. The local runtime replays conversation history that was assembled upstream — detectable because the prompt prefix hash matches a `gen_ai.prompt` or message payload already present in an ancestor span. In any of these cases the instance must switch to **attributable-usage** emission: report only the delta tokens this span's operation introduced (the tool result + the decision completion), and mark the span with an explicit attribution boundary so aggregators do not double-count. ## Decision Boundary The runtime must branch on trace ownership *before* the span is exported, not after. The check is cheap and deterministic: - Read the active `SpanContext`. If `IsRemote == true` (the OpenTelemetry SDK sets this when the context was extracted from a carrier rather than started locally), the span is a continuation. - Additionally compare `trace_id` against the local root registry. A `trace_id` not in the registry is foreign by definition. If foreign, the emitter must do one of two things, and the choice is a policy decision that must be recorded on the span: - **Delta mode:** emit `gen_ai.usage.input_tokens` as the attributable delta only, and set a non-standard-but-explicit attribute `gen_ai.usage.attribution = "delta"` plus `gen_ai.usage.replayed_prefix_tokens` so aggregators can reconstruct cumulative cost if they want it. - **Suppress mode:** do not emit usage attributes at all on the continuation span; emit only `gen_ai.tool.name`, `gen_ai.tool.call.id`, and operation name, and let the root owner account for cost from its own side. What the instance must *never* do is emit cumulative `input_tokens` on a remote-parented span. That is the boundary violation. The decision boundary is therefore: `IsRemote(context) → delta-or-suppress`, never `cumulative`. The `gen_ai.tool.call.id` correlation has a parallel boundary. A locally generated call id on a continuation span will not match the upstream orchestrator's expected id, so the instance must propagate the upstream call id (from A2A `_meta` or the MCP request envelope) rather than minting a new one, or it must emit a `gen_ai.tool.call.parent_id` linking the two. Minting a fresh id on a foreign trace is a lineage break. ## Operational Effect When the boundary is respected, trace-level aggregation becomes sound: sum of attributable deltas across a trace equals the root owner's cumulative total, and the two are reconcilable by summing `replayed_prefix_tokens` against the root's `input_tokens`. Verifiers that check tool-call/result pairing see a consistent id graph because call ids are propagated, not regenerated. When the boundary is violated, the failure modes are: (a) billing reconciliation over-reports by the replayed-prefix factor at every hop, compounding multiplicatively in deep pipelines; (b) orphan-tool-call verifiers raise false positives because call ids diverge; (c) cost-based autoscalers and budget guards trip early because aggregate usage crosses thresholds that no single agent actually reached. All three are silent under per-span schema validation and only surface at trace-level aggregation — which is precisely why the invariant must be enforced at emission time by the instance itself, not delegated to a downstream collector that lacks the ownership context. The guardrail is a single predicate plus a policy branch, and it must live in the instrumentation layer, because by the time the span reaches a collector the `IsRemote` bit is gone and the `trace_id` provenance is unrecoverable. ```python from dataclasses import dataclass from enum import Enum class ForeignTraceAccountingError(RuntimeError): """Raised when a GenAI span would emit cumulative usage on a foreign-parented trace.""" class AttributionMode(str, Enum): CUMULATIVE = "cumulative" DELTA = "delta" SUPPRESS = "suppress" @dataclass(frozen=True) class SpanContext: trace_id: str span_id: str is_remote: bool @dataclass(frozen=True) class GenAIUsage: input_tokens: int output_tokens: int replayed_prefix_tokens: int = 0 def resolve_attribution_mode( ctx: SpanContext, local_root_registry: frozenset[str], policy: AttributionMode = AttributionMode.DELTA, ) -> AttributionMode: owns_trace = (not ctx.is_remote) and (ctx.trace_id in local_root_registry) if owns_trace: return AttributionMode.CUMULATIVE if policy is AttributionMode.CUMULATIVE: # Caller asked for cumulative on a continuation span: hard refusal. raise ForeignTraceAccountingError( f"refusing cumulative gen_ai.usage on remote-parented trace " f"{ctx.trace_id}: would double-count replayed prefix" ) return policy def build_usage_attributes( ctx: SpanContext, usage: GenAIUsage, local_root_registry: frozenset[str], policy: AttributionMode = AttributionMode.DELTA, ) -> dict[str, int | str]: mode = resolve_attribution_mode(ctx, local_root_registry, policy) if mode is AttributionMode.CUMULATIVE: return { "gen_ai.usage.input_tokens": usage.input_tokens, "gen_ai.usage.output_tokens": usage.output_tokens, "gen_ai.usage.attribution": "cumulative", } if mode is AttributionMode.DELTA: attributable = usage.input_tokens - usage.replayed_prefix_tokens if attributable < 0: raise ForeignTraceAccountingError( f"replayed_prefix_tokens ({usage.replayed_prefix_tokens}) exceeds " f"input_tokens ({usage.input_tokens}) on trace {ctx.trace_id}; " f"delta attribution would be negative" ) return { "gen_ai.usage.input_tokens": attributable, "gen_ai.usage.output_tokens": usage.output_tokens, "gen_ai.usage.replayed_prefix_tokens": usage.replayed_prefix_tokens, "gen_ai.usage.attribution": "delta", } # SUPPRESS: emit no usage attributes at all on a continuation span. return {"gen_ai.usage.attribution": "suppressed"} ```