{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-emit-orphaned-tool-call-spans-when-genai-token-accounting-crosses-a-retry-boundary",
  "title": "Why does an instance emit orphaned tool-call spans when GenAI token accounting crosses a retry boundary",
  "date_of_record": "2026 November 28",
  "scope": "Applies to agents instrumented with OpenTelemetry GenAI semantic conventions (`gen_ai.*` attributes) where tool invocations are wrapped in `execute_tool` spans and token usage is recorded on `chat` spans, and where the model SDK performs internal retries or streaming reconnects. Does not apply to batch/offline tracing where spans are flushed only at process exit and never cross a retry boundary.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-emit-orphaned-tool-call-spans-when-genai-token-accounting-crosses-a-retry-boundary.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-emit-orphaned-tool-call-spans-when-genai-token-accounting-crosses-a-retry-boundary.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-emit-orphaned-tool-call-spans-when-genai-token-accounting-crosses-a-retry-boundary.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1765,
    "python_slice_tokens": 459,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions treat a tool call as a **child span of the model invocation that requested it**, with `gen_ai.operation.name = \"execute_tool\"` and a `gen_ai.tool.call.id` that must match the `tool_calls[].id` emitted in the assistant message. Token accounting is recorded on the **parent** `chat` span via `gen_ai.usage.input_tokens` and `gen_ai...",
    "what_was_observed": "A retrieval agent running against a provider that returns `503` on roughly 2% of streaming completions. On a retried turn, the trace showed: - `chat` span A (attempt 1): `status = ERROR`, `gen_ai.usage.input_tokens = 0`, `gen_ai.usage.output_tokens = 0`, **not exported** (the SDK aborts the span before the exporter sees the usage attributes, which are set only on stream completion). - `chat` span B (attempt 2): `stat...",
    "condition_of_validity": "This failure reproduces only when all of the following hold: 1. The SDK retries at the **inference layer**, not the application layer — i.e., the retry is invisible to the agent loop, so the loop never re-parses the assistant message and never regenerates tool-call IDs. 2. Usage attributes are set **on stream completion** (the common pattern), so an aborted ...",
    "decision_boundary": "The guardrail is a **post-export reconciliation invariant** enforced at span-processor shutdown, not at emission time (emission-time checks cannot see the retry that has not happened yet): - For every exported `chat` span with `gen_ai.response.finish_reasons` containing `tool_calls`, assert there exists ≥1 descendant `execute_tool` span under that span's con...",
    "operational_effect": "Once reconciliation is in place, the retry boundary stops being a silent correctness hole and becomes a measurable rate: `orphaned_tool_call_rate` per provider. In the observed system it was 1.9%, matching the provider's 503 rate almost exactly — confirming the mechanism. Two downstream fixes followed: (a) the tool executor was changed to read the *currently..."
  },
  "matched_failure_symptoms": [
    "DOMAIN:EPISTEMIC_PROVENANCE_AND_FEDERATION_ROUTING"
  ],
  "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": "bb77d9078f0aa3e2f35d2d9dd23dee52f497b8de2225e6332173f72d5221745f",
    "verified_classes": [
      "OrphanedToolCallError",
      "Span"
    ],
    "verified_functions": [
      "reconcile_tool_lineage",
      "emit_reconciled_trace"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 5,
    "raised_exceptions": [
      "OrphanedToolCallError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
