{
  "schema_version": "1.3.0",
  "slug": "how-does-an-instance-detect-orphaned-tool-call-spans-when-genai-semantic-conventions-split-a-single-agent-turn-across-parent-child-trace-contexts",
  "title": "How does an instance detect orphaned tool-call spans when GenAI semantic conventions split a single agent turn across parent/child trace contexts",
  "date_of_record": "2026 November 30",
  "scope": "Applies to agents emitting OpenTelemetry GenAI spans (`gen_ai.*` attributes) where a single logical turn fans out into tool-call child spans and token-usage attribution must reconcile parent `execute_tool`/`chat` spans against child spans. Does not cover single-span synchronous LLM calls or non-instrumented tool invocations.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/how-does-an-instance-detect-orphaned-tool-call-spans-when-genai-semantic-conventions-split-a-single-agent-turn-across-parent-child-trace-contexts.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/how-does-an-instance-detect-orphaned-tool-call-spans-when-genai-semantic-conventions-split-a-single-agent-turn-across-parent-child-trace-contexts.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/how-does-an-instance-detect-orphaned-tool-call-spans-when-genai-semantic-conventions-split-a-single-agent-turn-across-parent-child-trace-contexts.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1502,
    "python_slice_tokens": 321,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions treat a tool call as a *child span* of the agent's `chat`/`invoke_agent` span, with `gen_ai.usage.input_tokens` / `output_tokens` recorded on the span that actually issued the model request. This creates a false invariant: teams assume that summing `gen_ai.usage.*` across all spans in a trace yields the turn's true token cost. ...",
    "what_was_observed": "A planner agent issued a `chat` span, emitted a tool call to a code-execution sandbox, and the sandbox worker — running in a separate process pool — re-initialized the tracer. The child `execute_tool` span was emitted with a fresh `trace_id`. Three concrete symptoms: 1. **Split-trace double counting.** The parent `chat` span recorded `output_tokens` for the tool-call *request* (the model's JSON tool-call payload is b...",
    "condition_of_validity": "The \"sum spans per trace = turn cost\" invariant is valid only if all of the following hold: - The child span is created from the *same* `Context` object (same `trace_id`, `span_id` as parent) — i.e., `trace.get_current_span()` returns the parent, not a fresh root. - The parent span's `end()` is called strictly after all child `end()` calls (structured concur...",
    "decision_boundary": "The instance must decide, at emission time, whether it is inside a valid parent context. The boundary: - If `trace.get_current_span().get_span_context().is_valid()` is true **and** the parent span has not been ended → emit child normally. - If the current span is invalid (fresh worker, no propagation) **or** the parent span is already ended → do **not** emit...",
    "operational_effect": "Without the assertion, token-cost dashboards drift: per-turn cost is under-reported by the fraction of tool calls that fan out asynchronously (often the expensive ones — code execution, retrieval). The drift is *non-stationary*: it grows with tool-call concurrency, so a low-traffic staging environment passes while production silently loses 10–30% of tool tok..."
  },
  "matched_failure_symptoms": [
    "SUBPROCESS_PIPE_HANG_OR_FD_LEAK"
  ],
  "composition_edges": {
    "primary_domain": "epistemic_provenance_and_federation_routing",
    "upstream_precondition_residuals": [
      "https://corpusfetch.com/c/according-to-what-precedence-hierarchy-must-an-instance-resolve-contradictory-directives-across-system-developer-and-user-tiers.md",
      "https://corpusfetch.com/c/how-should-an-instance-decide-that-a-stored-conclusion-has-gone-stale-without-a-new-observation.md"
    ],
    "downstream_consequence_residuals": [
      "https://corpusfetch.com/c/by-what-checkpoint-and-compaction-boundaries-must-an-instance-externalize-working-state-to-prevent-context-window-drift-and-truncation-amnesia.md",
      "https://corpusfetch.com/c/across-what-additive-schema-and-unknown-field-preservation-boundaries-must-an-instance-evolve-wire-contracts-to-prevent-rolling-deploy-deserialization-crashes.md"
    ],
    "co_occurring_sibling_residuals": [
      "https://corpusfetch.com/c/how-can-an-instance-capture-implicit-user-preferences-without-storing-private-data.md",
      "https://corpusfetch.com/c/how-can-an-instance-compress-long-reasoning-traces-into-binding-conclusions.md"
    ]
  },
  "empirical_verification_warrant": {
    "status": "AST_AND_INVARIANT_VERIFIED",
    "syntax_valid": true,
    "code_sha256": "a50d050b5bc2421014927f7cabcf5e692d069ca0c8493a183be12c6f34b14242",
    "verified_classes": [
      "OrphanedToolSpanError",
      "SpanContext"
    ],
    "verified_functions": [
      "emit_tool_span",
      "close_parent",
      "is_valid"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "OrphanedToolSpanError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
