{
  "schema_version": "1.3.0",
  "slug": "what-happens-when-an-instance-emits-genai-tool-call-spans-whose-token-accounting-collapses-parent-and-child-spans-into-a-single-non-idempotent-counter",
  "title": "What happens when an instance emits GenAI tool-call spans whose token accounting collapses parent and child spans into a single non-idempotent counter",
  "date_of_record": "2026 November 23",
  "scope": "Applies to any agent runtime that instruments tool-calling loops with OpenTelemetry GenAI semantic conventions (`gen_ai.*` attributes) and aggregates `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens` across a span tree. Does not apply to single-span, single-call instrumentation or to runtimes that emit no parent/child span hierarchy.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-emits-genai-tool-call-spans-whose-token-accounting-collapses-parent-and-child-spans-into-a-single-non-idempotent-counter.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-emits-genai-tool-call-spans-whose-token-accounting-collapses-parent-and-child-spans-into-a-single-non-idempotent-counter.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-emits-genai-tool-call-spans-whose-token-accounting-collapses-parent-and-child-spans-into-a-single-non-idempotent-counter.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1606,
    "python_slice_tokens": 253,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "There are two ways to read a `gen_ai.usage.input_tokens` attribute. The first treats it as a *per-span delta*: the tokens consumed by the model invocation that the span directly wraps. The second treats it as a *cumulative subtree total*: the full prompt+completion cost attributable to the span and everything beneath it. The GenAI semantic conventions specif...",
    "what_was_observed": "An agent executing a 6-step tool loop (one `invoke_agent` root, six `execute_tool` children, six nested `chat` spans for the model invocations that decided each tool call) emitted the following on each `chat` span: `gen_ai.usage.input_tokens=1840`, `gen_ai.usage.output_tokens=210`. The runtime's instrumentation layer, intending to surface per-tool cost, also stamped the *sum of the child's decision call* onto each `e...",
    "condition_of_validity": "The invariant that must hold for token accounting to be sound is: **for any trace, exactly one span per model invocation carries the `gen_ai.usage.*` delta, and no ancestor span carries a value that is derivable by summing its descendants.** This is the \"single-writer per invocation\" rule. It is valid only when: 1. The instrumentation layer knows, at span-st...",
    "decision_boundary": "The boundary is crossed at the moment an agent span emits `gen_ai.usage.*` on a span that has `gen_ai.operation.name` of `invoke_agent` or `execute_tool` rather than `chat`. Below that boundary — usage only on `chat` spans — a sum-over-trace is correct and idempotent. Above it, the sum is no longer a function of the trace alone; it depends on which spans the...",
    "operational_effect": "Downstream, the effect is a cost dashboard that is internally consistent (the number is stable across queries) but externally wrong by a fan-out-dependent factor, which means it is *undetectable by anomaly detection*: the inflation is proportional to tool-call depth, so a healthy agent with more tool calls looks more expensive in a way that mimics genuine lo..."
  },
  "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": "adb8f6619458a69a0e30933e59896d6a60406aaa80e9c49590e03b2ab2f3a3a0",
    "verified_classes": [
      "GenAIUsageDoubleCountError"
    ],
    "verified_functions": [
      "validate_usage_span",
      "rollup_trace"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 1,
    "raised_exceptions": [
      "GenAIUsageDoubleCountError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
