{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-lose-token-accounting-fidelity-when-genai-semantic-convention-spans-are-closed-out-of-order-across-nested-tool-calls",
  "title": "Why does an instance lose token accounting fidelity when GenAI semantic-convention spans are closed out of order across nested tool calls",
  "date_of_record": "2026 December 1",
  "scope": "Applies to LLM agent runtimes that emit OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.*` attributes) for a parent agent span with nested tool-call and sub-inference spans, where token usage is accumulated via span attributes and exported to a collector. Does not apply to single-shot inference with no nested tool calls, nor to systems that buffer usage in a separate metrics pipeline independent of span lifecycle.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-token-accounting-fidelity-when-genai-semantic-convention-spans-are-closed-out-of-order-across-nested-tool-calls.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-token-accounting-fidelity-when-genai-semantic-convention-spans-are-closed-out-of-order-across-nested-tool-calls.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-token-accounting-fidelity-when-genai-semantic-convention-spans-are-closed-out-of-order-across-nested-tool-calls.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1915,
    "python_slice_tokens": 425,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions treat token accounting as a *span-local attribute* (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, and their cached/reasoning variants) attached to the span that *consumed* the tokens. They do not define a parent-aggregation rule, a monotonicity guarantee, or an ordering constraint on span closure. This is fine when...",
    "what_was_observed": "A multi-tool agent (planner → retriever tool → summarizer sub-inference → writer tool) produced traces where the agent-level span reported `gen_ai.usage.input_tokens = 8,412` while the sum of all descendant `gen_ai.usage.input_tokens` was `11,907`. The delta of 3,495 tokens was exactly the input-token count of the summarizer sub-inference span. That span had been opened inside the retriever tool's execution context, ...",
    "condition_of_validity": "The invariant that must hold for correct token accounting is: > **For every span `S` carrying `gen_ai.usage.*` attributes, all spans in `subtree(S)` must have ended before `S.end()` is invoked, OR the aggregation must be deferred to a post-export tree walk that is the *sole* source of truth for rollups.** Two valid regimes exist, and mixing them is the failu...",
    "decision_boundary": "The boundary is crossed at the moment a tool span's `end()` is called while a descendant inference span is still open. Concretely, the decision is: **does the tool-execution wrapper end the tool span at tool-return, or at tool-output-consumed?** - If at tool-return: the wrapper is in live-tree mode and *must not* propagate child usage; rollup must be export-...",
    "operational_effect": "The effect is a silent, direction-inconsistent token under/over-count that is invisible to span-count and trace-completeness checks (the trace has the right number of spans; only the sum is wrong). It corrupts cost attribution per agent run, breaks budget-enforcement guardrails that read `gen_ai.usage.*` from the agent span to decide whether to halt a run, a..."
  },
  "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": "2bf390c92afde99148a94c5f1db193f999ef254c56fcf23b4dee0944f5b8190a",
    "verified_classes": [
      "GenAiUsageAggregationError",
      "GenAiSpan",
      "UsageAggregator"
    ],
    "verified_functions": [
      "reproduce_orphan_case",
      "__init__",
      "open",
      "end",
      "rollup"
    ],
    "verified_functions_count": 5,
    "defensive_guards_count": 6,
    "raised_exceptions": [
      "GenAiUsageAggregationError"
    ],
    "sample_fail_closed_invariants": [
      "live_tree regime must not call export-time rollup; totals are snapshotted at end()"
    ],
    "stdlib_dependencies": [
      "__future__",
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
