{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-of-opentelemetry-genai-token-accounting-silently-double-count-tool-call-output-when-gen-ai-usage-output-tokens-is-emitted-on-both",
  "title": "Why does an instance of OpenTelemetry GenAI token accounting silently double-count tool-call output when `gen_ai.usage.output_tokens` is emitted on both the parent agent span and the child tool span",
  "date_of_record": "2026 November 18",
  "scope": "Applies to OTel GenAI semantic conventions v1.28+ where agent spans (`gen_ai.operation.name = \"invoke_agent\"`) and tool spans (`gen_ai.operation.name = \"execute_tool\"`) coexist under a single trace, and where a downstream collector or billing pipeline aggregates `gen_ai.usage.output_tokens` by summing across spans. Does not apply to single-span LLM call instrumentation (`chat`) or to pipelines that aggregate strictly by leaf span.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-of-opentelemetry-genai-token-accounting-silently-double-count-tool-call-output-when-gen-ai-usage-output-tokens-is-emitted-on-both.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-of-opentelemetry-genai-token-accounting-silently-double-count-tool-call-output-when-gen-ai-usage-output-tokens-is-emitted-on-both.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-of-opentelemetry-genai-token-accounting-silently-double-count-tool-call-output-when-gen-ai-usage-output-tokens-is-emitted-on-both.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1344,
    "python_slice_tokens": 363,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions define `gen_ai.usage.output_tokens` as an attribute of the span that *represents the model inference*. In a chat-only instrumentation, there is exactly one such span per call, and summation is trivially correct. In an agentic instrumentation, the same attribute is legitimately emitted on the `invoke_agent` span (the aggregate o...",
    "what_was_observed": "On a production trace of a research agent (single `invoke_agent` span, three `execute_tool` children — `web_search`, `fetch_page`, `summarize`), the collector's token rollup reported 14,820 output tokens for a turn whose true billed output was 9,240. The delta was exactly the sum of `gen_ai.usage.output_tokens` on the two tool spans that internally invoked a model. The parent `invoke_agent` span's attribute already i...",
    "condition_of_validity": "The invariant \"sum of `gen_ai.usage.output_tokens` over all spans in a trace equals billed output tokens\" holds **only if** no span is an ancestor of another span that also carries the attribute. Equivalently: the attribute must form an antichain over the span tree. This is satisfied when (a) only `chat` spans carry the attribute, or (b) `invoke_agent` spans...",
    "decision_boundary": "If you control instrumentation: emit `gen_ai.usage.output_tokens` only on leaf spans (spans with no descendant that also emits it), and expose the agent-turn total under a distinct, non-convention attribute. If you consume third-party instrumentation: do not sum flat. Aggregate by walking the span tree and, at each node, take `max(own_attribute, sum(children...",
    "operational_effect": "Undetected, the failure inflates cost dashboards proportionally to tool-model density, which biases optimization toward the wrong tools and can trigger false budget alarms during tool-heavy turns. It also corrupts any per-tool cost attribution that divides parent tokens among children. Detection is cheap: compute, per trace, `sum(all spans) - max_over_antich..."
  },
  "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": "fdcbf49abe7f379f47beab6e30750678d0a388d7df93c90819d82f4e52919572",
    "verified_classes": [
      "TokenAccountingInvariantError"
    ],
    "verified_functions": [
      "rollup_output_tokens",
      "subtree_has_token",
      "own",
      "resolve"
    ],
    "verified_functions_count": 4,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "TokenAccountingInvariantError"
    ],
    "sample_fail_closed_invariants": [
      "no root span found; trace is malformed and token rollup is undefined"
    ],
    "stdlib_dependencies": [],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
