{
  "schema_version": "1.3.0",
  "slug": "what-happens-when-an-instance-emits-genai-spans-before-its-token-accounting-closes-the-orphaned-tool-call-span-and-the-double-count-invariant-under-ge",
  "title": "What happens when an instance emits GenAI spans before its token accounting closes: the orphaned tool-call span and the double-count invariant under `genai.usage.*`",
  "date_of_record": "2026 November 22",
  "scope": "Applies to any LLM-serving process that emits OpenTelemetry GenAI spans (`genai.*` semantic conventions) where tool/function calls are represented as child spans and token usage is recorded on the parent chat span. Does not cover non-GenAI spans, pure HTTP instrumentation, or providers that emit usage on a separate metrics pipeline only.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-emits-genai-spans-before-its-token-accounting-closes-the-orphaned-tool-call-span-and-the-double-count-invariant-under-ge.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-emits-genai-spans-before-its-token-accounting-closes-the-orphaned-tool-call-span-and-the-double-count-invariant-under-ge.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-emits-genai-spans-before-its-token-accounting-closes-the-orphaned-tool-call-span-and-the-double-count-invariant-under-ge.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1679,
    "python_slice_tokens": 407,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions treat a chat completion as one span carrying `genai.usage.input_tokens`, `genai.usage.output_tokens`, and (where applicable) `genai.usage.total_tokens`, with tool invocations modeled as **child spans** of that chat span. The critical but under-specified distinction is between *span lifetime* and *accounting lifetime*. The chat ...",
    "what_was_observed": "A streaming agent loop (provider A) emitted the following sequence for a single turn: 1. `chat {model}` span opened; `genai.request.model`, `genai.operation.name=chat` set. 2. First token arrives; span remains open. 3. Model emits a tool-call delta; the runtime **closes the chat span** to satisfy a \"span ends when generation ends\" heuristic in the SDK wrapper, exporting it with `genai.usage.output_tokens` unset (the ...",
    "condition_of_validity": "The invariant *\"tool-call child spans are token-attributable to their parent chat span\"* holds **only if** the chat span's `End` is deferred until the usage-bearing terminal frame is processed. It is violated whenever any of the following hold: - The span is ended on first-byte or on stream-close-before-usage-frame. - Usage is written via a post-`End` attrib...",
    "decision_boundary": "The boundary is: **does the runtime own the terminal usage frame before it owns the span's end?** If yes, attribute-then-end preserves the invariant. If no — streaming, async usage, or provider-side deferred accounting — the runtime must either (a) hold the chat span open across the tool-call boundary and close it only after the tool child has ended *and* us...",
    "operational_effect": "The observable effect is a **silent, load-shaped undercount**: dashboards show healthy p50 latency and stable request rates, while token cost per tool-invoking turn is systematically understated by the fraction of output tokens consumed by tool-call argument generation. Because that fraction scales with tool schema complexity (larger JSON schemas → longer ar..."
  },
  "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": "949f546bb5c362f59a8944cc83b782f56d0839f0dabff5bda3146a5ca9455717",
    "verified_classes": [
      "GenAIAccountingInvariantError",
      "GenAISpan"
    ],
    "verified_functions": [
      "reconcile_turn",
      "example_failure",
      "set_attribute",
      "end"
    ],
    "verified_functions_count": 4,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "GenAIAccountingInvariantError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "__future__",
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
