Why does an instance's tool-call span silently lose its parent when OTel GenAI `gen_ai.operation.name` is re-emitted across a mid-task resume?
**Date of Record:** 2026 December 1 **Scope:** Applies to agents that emit OpenTelemetry GenAI spans (`gen_ai.*` semantic conventions) and resume execution from a checkpoint by re-instantiating a span context, rather than continuing an in-process span. Does not apply to single-shot agents whose entire task lifecycle fits one process without checkpoint/resume.
Core Distinction
The GenAI semantic conventions treat a span as a *record of an operation that occurred*, not as a *durable handle that survives process death*. Two attributes carry this distinction: `gen_ai.operation.name` (e.g., `chat`, `execute_tool`, `invoke_agent`) classifies the operation, while trace/span parentage (`trace_id`, `parent_span_id`, `span_id`) defines continuity. These are orthogonal in the spec but conflated by naive resume logic.
The failure: on checkpoint/resume, an agent re-emits its tool-call span with the *same* `gen_ai.operation.name` and the *same* logical call identity (same tool, same arguments hash), but with a *freshly generated* `span_id` and a parent derived from the *resumed* context rather than the *original* one. The result is a duplicate, orphaned tool-call span whose token/cost attributes are counted twice by cost aggregators, and whose parent linkage points at a resume-root instead of the original reasoning loop. The span is structurally valid, semantically wrong, and — critically — invisible to the agent that emitted it, because OTel has no back-reference from a child to a prior sibling.
What Was Observed
A planner-executor agent ran a `chat` span (reasoning) that produced a tool call. The executor emitted `gen_ai.operation.name=execute_tool` with `gen_ai.tool.name`, a `gen_ai.usage.input_tokens`/`output_tokens` pair, and a `gen_ai.response.id`. Mid-tool-call the process was checkpointed (context window pressure triggered a serialize-resume). On resume:
- The resumed agent rebuilt its span context from the checkpoint's `trace_id` but generated a new `span_id` for the re-entered tool call, because the original span was never `end()`-ed — it was abandoned when the process died, so its `span_id` was never persisted.
- The parent was resolved to the *resume root* (a synthetic `invoke_agent` span opened at resume), not the original `chat` span, because the parent `span_id` was also not checkpointed.
- The re-executed tool call emitted a second `execute_tool` span with identical `gen_ai.tool.name` and a near-identical argument fingerprint, but `gen_ai.response.id` differed (the tool is non-deterministic), so downstream dedup by response id failed.
- Cost aggregation summed both spans: token totals for the task were inflated by the first (abandoned) attempt plus the resumed attempt.
The orphan was detectable only by a downstream auditor that joined on `trace_id` and noticed two `execute_tool` spans with the same tool name and no shared parent. The agent itself, on resume, had no attribute telling it "a prior attempt at this exact call exists."
Condition of Validity
The invariant that must hold: **for any logical tool call identified by `(trace_id, gen_ai.tool.name, argument_fingerprint)`, at most one *completed* `execute_tool` span may carry billable `gen_ai.usage.*` attributes, and every such span must have a `parent_span_id` that resolves to a span in the same trace that was open at the time of the call.**
This holds only when the resume path persists and restores the *span identity*, not just the `trace_id`. Concretely, the checkpoint must carry:
- the `span_id` of the in-flight tool-call span (so resume can either continue it or mark it `abandoned`),
- the `parent_span_id` of the originating reasoning span,
- a stable `argument_fingerprint` attribute the agent can query on resume to detect prior attempts.
If any of these is absent, the invariant is unverifiable at emission time and the failure is silent. The condition of validity is therefore *checkpoint completeness*, not span correctness — a span can be perfectly well-formed and still violate the invariant.
Decision Boundary
The boundary is between **re-emit** and **adopt**:
- *Re-emit* (the default, and the failure): generate a new span, let the original dangle. Valid only if the original span was explicitly `end()`-ed with an `error`/`abandoned` status before checkpoint, so cost aggregators can exclude it by status.
- *Adopt*: on resume, query the checkpoint for an in-flight `execute_tool` span matching the fingerprint; if found, continue it (restore `span_id`, reuse `parent_span_id`) rather than opening a new one. Requires the fingerprint and span identity to be checkpointed.
The decision must be made *before* the tool is re-invoked, because once the non-deterministic tool runs a second time, the response ids diverge and the two attempts can no longer be reconciled by content. The boundary is thus: **if the checkpoint lacks span identity, the agent must treat the prior attempt as billable-and-abandoned and emit a compensating `abandoned` status span, not a silent duplicate.**
Operational Effect
Without the guardrail: cost telemetry double-counts every resumed tool call; trace graphs show orphaned `execute_tool` spans parented to a resume root, breaking causal reconstruction of the reasoning loop; and `gen_ai.usage.*` token totals become non-reconcilable against provider invoices because the abandoned attempt's tokens were still billed by the model provider even though the span was never closed. The agent's own memory of "what I already tried" is absent from its span history, so on a second resume it may re-attempt a third time, compounding the inflation linearly with resume count.
With the guardrail: resume either continues the original span (single billable record, correct parentage) or emits an explicit `abandoned` span that cost aggregators exclude, keeping token totals reconcilable and trace graphs causally intact.
class SpanContinuityViolation(RuntimeError):
"""Raised when a resumed agent would emit a duplicate billable tool-call span."""
pass
def resolve_tool_span(checkpoint: dict, fingerprint: str, tool_name: str) -> dict:
"""
Decide whether to adopt an in-flight tool-call span or open a new one.
Enforces: at most one completed billable span per
(trace_id, tool_name, fingerprint), with resolvable parentage.
"""
trace_id = checkpoint.get("trace_id")
in_flight = checkpoint.get("in_flight_tool_spans", [])
matches = [
s for s in in_flight
if s.get("gen_ai.tool.name") == tool_name
and s.get("argument_fingerprint") == fingerprint
]
if len(matches) > 1:
# Two prior attempts already exist: invariant already broken upstream.
raise SpanContinuityViolation(
f"trace {trace_id}: {len(matches)} in-flight spans for "
f"tool={tool_name} fp={fingerprint}; checkpoint is inconsistent"
)
if matches:
prior = matches[0]
if not prior.get("span_id") or not prior.get("parent_span_id"):
# Cannot adopt: span identity was not checkpointed.
# Must mark abandoned, not silently re-emit.
raise SpanContinuityViolation(
f"trace {trace_id}: prior span for tool={tool_name} lacks "
f"span_id/parent_span_id; emit 'abandoned' status span "
f"before re-invoking, do not open a duplicate"
)
return {"action": "adopt", "span": prior}
return {"action": "open_new", "span": None}