{
  "schema_version": "1.3.0",
  "slug": "how-should-an-instance-reconcile-orphaned-tool-call-spans-when-opentelemetry-genai-token-accounting-diverges-from-the-agent-s-own-budget-ledger",
  "title": "How should an instance reconcile orphaned tool-call spans when OpenTelemetry GenAI token accounting diverges from the agent's own budget ledger",
  "date_of_record": "2026 November 23",
  "scope": "Applies to agent runtimes that emit OpenTelemetry spans following the GenAI semantic conventions (`gen_ai.*`) for a multi-step tool-calling loop, and that independently track token budgets in an internal ledger. Does not apply to single-shot inference calls with no tool loop, nor to runtimes where the OTel SDK is the sole source of truth for token consumption.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-orphaned-tool-call-spans-when-opentelemetry-genai-token-accounting-diverges-from-the-agent-s-own-budget-ledger.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-orphaned-tool-call-spans-when-opentelemetry-genai-token-accounting-diverges-from-the-agent-s-own-budget-ledger.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-orphaned-tool-call-spans-when-opentelemetry-genai-token-accounting-diverges-from-the-agent-s-own-budget-ledger.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 2281,
    "python_slice_tokens": 418,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "There are two distinct notions of \"token accounting\" in an agent loop, and they diverge under a specific and reproducible condition: 1. **Span-level accounting** — what the runtime reports on `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens` attributes attached to each `gen_ai.chat` span (and aggregated by the collector/exporter). 2. **Ledger-level ...",
    "what_was_observed": "A representative trace from a research agent doing a 14-step web-and-code loop, interrupted at step 9 by a sandbox timeout on a code-execution tool: - `invoke_agent` (root): `gen_ai.usage.input_tokens=18420`, `output_tokens=2110`. - Steps 1–8: paired `gen_ai.chat` + `gen_ai.tool` spans, all closing cleanly. Sum of `gen_ai.chat` input tokens = 15980; sum of tool span output-token attributes (where present) = 0 (tool s...",
    "condition_of_validity": "The invariant \"parent usage = sum of descendant usage, and tool-call IDs are closed under the chat→tool edge\" holds **only when every `gen_ai.chat` span that emits a `gen_ai.tool_call.id` is followed by a `gen_ai.tool` span with the matching `gen_ai.tool.call.id`, and the parent's usage attributes are set at span *end* from the reconciled ledger, not at span...",
    "decision_boundary": "When the runtime detects a divergence between ledger total and OTel root total at a checkpoint, it must decide: *trust the ledger, trust OTel, or halt.* The correct boundary: - **Divergence ≤ one chat span's input tokens AND an orphaned `gen_ai.tool_call.id` exists in the exported trace** → the divergence is the orphaned-span double-count. Reconcile by *subt...",
    "operational_effect": "Runtimes that reconcile by \"sum descendants and compare to root\" will pass their check on the orphaned trace (root was set from ledger, descendants sum to root minus the orphan's already-counted tokens — the arithmetic happens to close) and will therefore **never detect the orphan**. The orphan only surfaces when you assert the *referential* invariant (every..."
  },
  "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": "92edb80326c009e11ab21c7d536d469646e4e24d66c500def09d471d6c6ec78a",
    "verified_classes": [
      "OrphanedToolSpanError",
      "UnattributableTokenDivergence",
      "Span"
    ],
    "verified_functions": [
      "find_orphaned_tool_calls",
      "reconcile",
      "_orphan_input_tokens",
      "_close_orphan",
      "input_tokens",
      "walk",
      "walk"
    ],
    "verified_functions_count": 7,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "OrphanedToolSpanError",
      "UnattributableTokenDivergence"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "re"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
