{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-lose-token-accounting-when-a-tool-call-span-closes-before-its-parent-agent-span-and-how-do-you-bound-it",
  "title": "Why does an instance lose token accounting when a tool-call span closes before its parent agent span, and how do you bound it?",
  "date_of_record": "2026 November 23",
  "scope": "Applies to agents emitting OpenTelemetry GenAI spans (`gen_ai.*` attributes) where tool invocations are instrumented as child spans of an agent span and token usage is aggregated upward. Does not apply to flat, single-span agents that emit one terminal span per turn, nor to providers that emit server-side usage records independent of client instrumentation.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-token-accounting-when-a-tool-call-span-closes-before-its-parent-agent-span-and-how-do-you-bound-it.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-token-accounting-when-a-tool-call-span-closes-before-its-parent-agent-span-and-how-do-you-bound-it.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-token-accounting-when-a-tool-call-span-closes-before-its-parent-agent-span-and-how-do-you-bound-it.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1736,
    "python_slice_tokens": 344,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The failure is not \"missing attributes.\" It is a **temporal aggregation invariant violation**: the GenAI semantic conventions expect usage counters (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`) to be attributable to the span that *consumed* the model call, and for agent-level totals to be *derivable* from the span tree at export time. When a to...",
    "what_was_observed": "A planner agent (span `agent.plan`, `gen_ai.operation.name=chat`) dispatched three tool calls. The second tool, `summarize_context`, internally invoked a small model to compress retrieved passages. Instrumentation used the standard `BatchSpanProcessor`, so spans were queued and exported on flush, but the *parent's* usage attributes were computed eagerly in the agent's `finally` block by reading `span.attributes` on c...",
    "condition_of_validity": "The invariant \"parent `gen_ai.usage.*` ≥ sum over descendant spans of `gen_ai.usage.*`\" holds **if and only if** all of the following are true at the moment the parent's usage attributes are written: 1. Every descendant span that will ever carry `gen_ai.usage.*` has already closed, *or* the parent does not write usage attributes eagerly and instead defers to...",
    "decision_boundary": "The boundary is: **do you compute usage at span-close (eager) or at export (deferred)?** - **Eager (in the `finally` block, reading closed children):** correct only for strictly nested, single-threaded, non-retrying tools. Any nested model call that closes after the read, or any retry that spawns a sibling, corrupts the total. This is the default most agents...",
    "operational_effect": "Agents that ship eager aggregation exhibit a characteristic cost profile: **agent-level token totals are systematically low, and tool spans carry chat usage.** The bias is not random — it scales with tool depth. A planner with three tools where one nests a model call undercounts by that tool's usage every turn; a multi-agent swarm where \"delegate\" is a tool ..."
  },
  "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": "9daadefedcb8a1232eb36b04533e8d5eb50fa735ae8c74e048d2ba6eaba3cd2a",
    "verified_classes": [
      "UsageAttributionError",
      "Span"
    ],
    "verified_functions": [
      "assert_usage_invariant",
      "emit_agent_usage",
      "_descendants",
      "walk"
    ],
    "verified_functions_count": 4,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "UsageAttributionError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
