{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-lose-token-accounting-when-a-tool-call-span-closes-before-its-child-generation-span",
  "title": "Why does an instance lose token accounting when a tool-call span closes before its child generation span?",
  "date_of_record": "2026 November 27",
  "scope": "Applies to agents emitting OpenTelemetry GenAI spans (`gen_ai.*`) where a `gen_ai.tool.call` (or `execute_tool`) span is emitted as a child of an `invoke_agent` span and the model's generation span is emitted by a separate, concurrent execution context (async tool dispatch, streaming callback, or out-of-process MCP server). Does not apply to strictly synchronous single-threaded loops where generation span start/end are lexically nested inside the tool-call span.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-token-accounting-when-a-tool-call-span-closes-before-its-child-generation-span.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-token-accounting-when-a-tool-call-span-closes-before-its-child-generation-span.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-token-accounting-when-a-tool-call-span-closes-before-its-child-generation-span.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1597,
    "python_slice_tokens": 264,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The failure is not \"metrics are missing.\" The failure is that **token accounting is attributed to the wrong span's parent chain because span *end* time, not span *start* time, determines whether an attribute survives aggregation across a trace.** A generation span that starts inside a tool-call's lifetime but *ends after* the tool-call span closes will still...",
    "what_was_observed": "A tool-augmented agent (`invoke_agent` → `execute_tool` → out-of-process MCP call → model re-invocation) produced traces where the `invoke_agent` span reported `gen_ai.usage.input_tokens = 1840`, but the sum of all `gen_ai.usage.input_tokens` on spans in the same trace was 4120. The discrepancy was exactly the token count of the *second* generation (the one produced after the tool returned), which had been emitted wi...",
    "condition_of_validity": "The invariant that must hold: **a span's `parent_span_id` must reference a span that has not yet been exported when the child ends, OR the aggregator must reconstruct parentage from the full trace, not from streaming arrival order.** This breaks precisely when all three hold: - (a) The parent (`execute_tool`) span is ended and exported *before* the child gen...",
    "decision_boundary": "The boundary is: **does the instrumentation end the tool-call span at dispatch or at result-return?** If `execute_tool` ends at dispatch (the common pattern when the tool is a fire-and-forget MCP request), you MUST either: - Re-scope the generation span to the `invoke_agent` span explicitly (do not rely on ambient context after an `await`), or - Emit a synth...",
    "operational_effect": "Without the guardrail, the cost ledger under-reports by exactly the tokens of every post-tool generation — typically 40–60% of agent cost in tool-heavy loops, and *systematically* under-reported (never over), so the error is invisible to anomaly detection. The agent appears cheaper than it is, which suppresses budget alarms and lets runaway loops run longer ..."
  },
  "matched_failure_symptoms": [
    "SUBPROCESS_PIPE_HANG_OR_FD_LEAK"
  ],
  "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": "6c42417d5674e70d239faf3df382b09c49024eea4af79575944adf6475eff912",
    "verified_classes": [
      "TokenAccountingDivergence",
      "Span"
    ],
    "verified_functions": [
      "reconcile_trace_tokens",
      "__init__"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "TokenAccountingDivergence"
    ],
    "sample_fail_closed_invariants": [
      "no invoke_agent span in trace"
    ],
    "stdlib_dependencies": [
      "otel_types"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
