{
  "schema_version": "1.3.0",
  "slug": "how-should-an-instance-reconcile-parent-child-token-attribution-when-an-agent-span-ends-before-its-async-tool-call-spans-report-usage",
  "title": "How should an instance reconcile parent-child token attribution when an agent span ends before its async tool-call spans report usage",
  "date_of_record": "2026 November 18",
  "scope": "Applies to OpenTelemetry-instrumented agent runtimes where an `invoke_agent` span can terminate (success, error, or context cancellation) while one or more `execute_tool` child spans are still open and have not yet emitted `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens`. Does not apply to synchronous tool calls that complete before the agent span closes, nor to providers that report aggregate usage only on the root span.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-parent-child-token-attribution-when-an-agent-span-ends-before-its-async-tool-call-spans-report-usage.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-parent-child-token-attribution-when-an-agent-span-ends-before-its-async-tool-call-spans-report-usage.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-parent-child-token-attribution-when-an-agent-span-ends-before-its-async-tool-call-spans-report-usage.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1561,
    "python_slice_tokens": 197,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions treat token usage as a span-local attribute: `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` belong to the span that *consumed* the tokens, and aggregators are expected to sum leaf spans rather than trust a parent's total. This works when the span tree is a tree in the strict sense — every child closes before its p...",
    "what_was_observed": "In a runtime where the agent span used `SpanKind.INTERNAL` and tool spans were dispatched with context propagation into a `ThreadPoolExecutor`, three distinct symptoms appeared in production traces over a two-week window: 1. **Orphaned usage.** Approximately 4% of traces contained `execute_tool` spans whose `parent_span_id` referenced a span not present in the exported trace. The collector had flushed the root and it...",
    "condition_of_validity": "The \"sum leaf spans for cost\" strategy is valid **only if** all three hold: - Every span that sets `gen_ai.usage.*` is a descendant of exactly one root, and that root is still recording when the descendant is created. - No span reports usage that was *already* reported by an ancestor (no context re-sends, no fan-out of the same prompt). - The collector's ass...",
    "decision_boundary": "Do not attempt to reconcile usage *within* the trace. The trace is not the unit of accounting when the span tree is not closed. Instead: - **Emit an explicit accounting span.** Have the runtime create a dedicated `gen_ai.agent.usage` span (or a span attribute on a synthetic root) that is written by the accounting layer, not the instrumentation layer, and tha...",
    "operational_effect": "After moving to an accounting span and setting `gen_ai.usage.reported` explicitly, the discrepancy between exported token totals and provider billing dropped from a 4–18% band to under 0.5% (residual attributable to provider-side retries not visible to the client). The cancelled-context no-op case was eliminated by checking `span.IsRecording()` before dispat..."
  },
  "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": "90be0ea6c459854bda00a2b063d4a2d56b50181b457caa091092ee5943ea092e",
    "verified_classes": [
      "UnreconciledUsageError"
    ],
    "verified_functions": [
      "reconcile_usage"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "UnreconciledUsageError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
