{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-lose-token-accounting-when-a-tool-call-span-outlives-its-parent-agent-span",
  "title": "Why does an instance lose token accounting when a tool-call span outlives its parent agent span?",
  "date_of_record": "2026 November 28",
  "scope": "Applies to any agent runtime that emits OpenTelemetry GenAI spans (`gen_ai.*` attributes, `execute_tool` child spans, `invoke_agent` parent spans) and relies on span-tree aggregation for token accounting and cost attribution. Does not apply to single-shot synchronous LLM calls that complete inside a single span with no tool fan-out.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-token-accounting-when-a-tool-call-span-outlives-its-parent-agent-span.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-token-accounting-when-a-tool-call-span-outlives-its-parent-agent-span.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-token-accounting-when-a-tool-call-span-outlives-its-parent-agent-span.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1547,
    "python_slice_tokens": 326,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "There are two different things people mean by \"token accounting\": *emission* (the model's usage block reports `input_tokens`/`output_tokens` on the response) and *attribution* (those tokens get rolled up to the right agent, turn, and parent span in the trace). OTel GenAI semantic conventions standardize emission via `gen_ai.usage.input_tokens` and `gen_ai.us...",
    "what_was_observed": "An agent turn produced a trace with this shape: - `invoke_agent` (parent), `gen_ai.usage.output_tokens=412`, closed at T+1.2s. - `chat` (child), usage attributes present, closed at T+1.1s. - `execute_tool` (child), `gen_ai.tool.name=\"search\"`, spawned at T+0.9s, closed at T+8.4s *after a retry loop*. The parent `invoke_agent` span was exported at T+1.2s. The collector's span processor had a 5s batch timeout; the `exe...",
    "condition_of_validity": "The invariant that must hold for tree-based token rollup to be correct: > For every span S carrying `gen_ai.usage.*`, the aggregator must have observed S before it finalizes any ancestor of S, **or** it must reconcile late-arriving children against already-finalized parents. This holds only when child spans are guaranteed to close before their parent — true ...",
    "decision_boundary": "The boundary is whether your accounting layer treats the trace as an *eventually consistent stream* or as a *tree materialized at parent-close*. Concretely: - **If** you aggregate on parent-span `on_end`, **then** you must reject the design for any agent with async tool fan-out. Late children are lost. - **If** you aggregate on trace-id completion (all spans...",
    "operational_effect": "Without the guardrail, three things degrade together and none of them alert: 1. **Cost attribution undercounts** proportionally to tool retry rate — the metric moves with infrastructure flakiness, not with model behavior, so it looks like noise. 2. **Per-agent token budgets** (if enforced from traces) under-fire, letting runaway agents exceed their intended ..."
  },
  "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": "97984856af36f33b566635ae9b37736c9691fe7159d56cff155abda1f4327472",
    "verified_classes": [
      "TokenAccountingInvariantError",
      "Span",
      "TraceLedger"
    ],
    "verified_functions": [
      "emit_agent_turn",
      "run",
      "record",
      "_finalize_trace"
    ],
    "verified_functions_count": 4,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "TokenAccountingInvariantError"
    ],
    "sample_fail_closed_invariants": [
      "assert ledger.rolled_up_tokens == 2292",
      "trace accounting would silently undercount; require terminal sentinel span"
    ],
    "stdlib_dependencies": [
      "dataclasses",
      "time"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
