{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-token-usage-double-counting-when-genai-agent-spans-are-re-parented-across-a-tool-call-boundary",
  "title": "Which invariants prevent token-usage double-counting when GenAI agent spans are re-parented across a tool-call boundary",
  "date_of_record": "2026 November 26",
  "scope": "Applies to OpenTelemetry-instrumented agent runtimes that emit `genai.*` spans (agent invocation, tool call, model chat) and aggregate `genai.usage.input_tokens` / `genai.usage.output_tokens` from span attributes. Does not apply to single-shot completion tracing where each request maps 1:1 to one span and no parent re-parenting occurs.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-usage-double-counting-when-genai-agent-spans-are-re-parented-across-a-tool-call-boundary.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-usage-double-counting-when-genai-agent-spans-are-re-parented-across-a-tool-call-boundary.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-usage-double-counting-when-genai-agent-spans-are-re-parented-across-a-tool-call-boundary.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1636,
    "python_slice_tokens": 352,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The failure is not \"missing token counts\" — it is *double-counted* token counts caused by a semantic mismatch between span **parenting** and attribute **propagation**. In the GenAI semantic conventions, `genai.usage.*` attributes are recorded on the span that *represents the model invocation* (`genai.operation.name = chat`). But agent runtimes frequently emi...",
    "what_was_observed": "A planner agent (`genai.operation.name = invoke_agent`) issued three tool calls, each of which triggered a nested `chat` span. The runtime set `genai.usage.input_tokens` on the agent span to the sum of the three chat spans (a \"total\" convenience attribute), and also set it on each chat span. Under normal conditions the trace was a clean tree and the collector's `sum(genai.usage.input_tokens)` over-counted by 2x — but...",
    "condition_of_validity": "The single-recording invariant is valid only under these conditions, and the guardrail must assert all of them: 1. **Leaf-ownership:** `genai.usage.*` is authoritative only on spans with `genai.operation.name = chat` (or `embeddings`/`generate_content` as applicable). Any ancestor span carrying the same attribute is a *derived* value and must be tagged `gena...",
    "decision_boundary": "The boundary is: **does the aggregator trust ancestor rollups or leaf spans?** If the aggregator sums all spans carrying `genai.usage.*`, it is vulnerable to every re-parenting event and every duplicated export. If it sums only leaf `chat` spans, it is robust to re-parenting but loses the ability to attribute tokens to a failed agent span whose leaf chat spa...",
    "operational_effect": "Without the guardrail, retried tool calls silently inflate cost ledgers by exactly the fraction of the trace that was retried, and the inflation is invisible to per-span dashboards because each span reports a locally-correct value. With the guardrail, the collector rejects any trace where a leaf `chat` span and an ancestor rollup both contribute to the same ..."
  },
  "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": "390382e71d79f94fb565b2aa5877453ec97dc45755f3d9af81ed2f4063207ae0",
    "verified_classes": [
      "GenAiUsageInvariantError"
    ],
    "verified_functions": [
      "aggregate_trace_usage",
      "descendants"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "GenAiUsageInvariantError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
