{
  "schema_version": "1.3.0",
  "slug": "what-happens-when-an-instance-emits-genai-tool-call-spans-before-the-model-span-closes-orphaned-token-accounting-and-the-parent-span-ordering-invarian",
  "title": "What happens when an instance emits GenAI tool-call spans before the model span closes: orphaned token accounting and the parent-span ordering invariant",
  "date_of_record": "2026 November 23",
  "scope": "Applies to LLM agent runtimes that instrument model invocations and tool calls using OpenTelemetry GenAI semantic conventions (`gen_ai.*` attributes) and export spans to a collector with parent/child linkage. Does not apply to flat, single-span tracing where tool calls are recorded as span events rather than child spans, nor to runtimes that buffer the full trace and emit it atomically after the agent turn completes.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-emits-genai-tool-call-spans-before-the-model-span-closes-orphaned-token-accounting-and-the-parent-span-ordering-invarian.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-emits-genai-tool-call-spans-before-the-model-span-closes-orphaned-token-accounting-and-the-parent-span-ordering-invarian.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-emits-genai-tool-call-spans-before-the-model-span-closes-orphaned-token-accounting-and-the-parent-span-ordering-invarian.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1895,
    "python_slice_tokens": 207,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions treat an LLM invocation span (`gen_ai.operation.name = \"chat\"`) as the accounting unit for token usage: `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` are attributes on that span, and downstream cost aggregation, budget enforcement, and per-turn attribution all key off the model span's `trace_id`/`span_id`. A tool...",
    "what_was_observed": "A multi-tool agent (planner → 3 parallel tool executors → synthesis) instrumented with the GenAI conventions showed a reproducible ~4–7% deficit in aggregated `gen_ai.usage.output_tokens` relative to the provider's billing dashboard, but only on turns where tool calls executed concurrently with the model stream. Serial tool-call turns showed no deficit. The failure trace, in order: 1. Model span `S_model` opens. `gen...",
    "condition_of_validity": "This failure is bounded by three conditions, and the guardrail only needs to hold when all three are true: - **Concurrency:** At least one tool call executes while its originating model span is still open, or the runtime closes the model span before the tool executor emits. If tool calls are strictly serialized after `S_model.end()`, the edge still resolves ...",
    "decision_boundary": "The architectural invariant that bounds this failure: **a span that contributes to token accounting must be self-describing with respect to the turn it belongs to, independent of the parent edge.** Concretely, every `gen_ai.operation.name=\"execute_tool\"` span (and any nested model span inside a tool) must carry a turn-correlation attribute—e.g. `gen_ai.conve...",
    "operational_effect": "The practical consequence is that token accounting becomes *eventually-inconsistent and order-dependent* rather than wrong-in-a-fixed-way. Under light load, the collector's span buffer (default ~5s eviction) absorbs the inter-batch gap and the join resolves; the deficit is invisible in staging and appears in production only when batch flush intervals stretch..."
  },
  "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": "cc085fb95f33bb80c5a285dd4d45462da2163a6e2d624079c56f650a08fce63a",
    "verified_classes": [
      "OrphanedGenAISpanError",
      "SpanRecord"
    ],
    "verified_functions": [
      "attribute_turn_tokens"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 1,
    "raised_exceptions": [
      "OrphanedGenAISpanError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
