{
  "schema_version": "1.3.0",
  "slug": "when-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-orphaned-tool-call-attribution-and-the-parent-token-double-count-invariant",
  "title": "When must an instance refuse to close an OpenTelemetry GenAI agent span: orphaned tool-call attribution and the parent-token double-count invariant",
  "date_of_record": "2026 November 24",
  "scope": "Applies when an agent emits OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.*`) for a parent agent invocation that fans out to child tool-call spans, and where token usage is attributed across that tree. Does not cover pure LLM inference spans with no tool fan-out, nor non-OTel telemetry pipelines.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/when-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-orphaned-tool-call-attribution-and-the-parent-token-double-count-invariant.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/when-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-orphaned-tool-call-attribution-and-the-parent-token-double-count-invariant.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/when-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-orphaned-tool-call-attribution-and-the-parent-token-double-count-invariant.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1818,
    "python_slice_tokens": 363,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The failure is not \"spans are missing.\" It is the opposite: spans are present, well-formed individually, and pass schema validation, yet the *aggregate* token attribution is wrong because a tool-call span closed **after** its parent agent span closed. The GenAI conventions treat `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens` as attributes on span...",
    "what_was_observed": "A multi-step agent run produced the following trace fragment (timings in ms relative to run start, single OTLP batch exporter with a 5000ms scheduled delay): - `agent.invoke` (root, span A) opened at t=0. - `execute_tool search` (span B, child of A) opened at t=120, dispatched a network call. - `execute_tool fetch` (span C, child of A) opened at t=140, dispatched a network call. - Span A's reasoning loop hit its step...",
    "condition_of_validity": "The invariant \"parent usage = sum of subtree usage\" is only valid under the precondition: > For every agent span A, all descendant tool-call spans must reach `Span.end()` before A reaches `Span.end()`, OR A must not record usage until subtree closure is confirmed. The GenAI conventions do not encode this precondition. They specify *what* attributes mean, not...",
    "decision_boundary": "The boundary is a single predicate evaluated at parent `Span.end()`: The trap is that agent frameworks commonly violate `all_children_ended` in exactly one scenario: **the step budget fires while a tool call is retrying.** The reasoning loop sees \"budget exhausted, produce final answer, end span,\" but a child tool span is mid-retry with exponential backoff. ...",
    "operational_effect": "Downstream consumers must treat `gen_ai.usage.*` as **eventually consistent per subtree**, not as final-at-parent-close. Concretely: - Billing ledgers that snapshot at parent-close will under-report by the token volume of every tool call that outlived its parent—empirically concentrated in retry-heavy runs and runs that hit step budgets. - Trace UIs that com..."
  },
  "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": "2766f155ce62c47ea5dc4fa133894b308981540578d28c896164b4578bd09e10",
    "verified_classes": [
      "UnclosedChildSpanError",
      "AgentSpan"
    ],
    "verified_functions": [
      "reconcile",
      "seal_agent_run",
      "register_child",
      "mark_child_ended",
      "_children_all_ended",
      "end",
      "_emit_final",
      "_emit_incomplete"
    ],
    "verified_functions_count": 8,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "UnclosedChildSpanError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "time"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
