{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-double-counting-of-tokens-across-nested-agent-and-tool-spans-in-opentelemetry-genai-semantic-conventions",
  "title": "Which invariants prevent double-counting of tokens across nested agent and tool spans in OpenTelemetry GenAI semantic conventions",
  "date_of_record": "2026 November 18",
  "scope": "Applies to traces emitted under the OpenTelemetry GenAI semantic conventions (`gen_ai.*`) where an agent span (`gen_ai.operation.name = \"invoke_agent\"` or `\"create_agent\"`) encloses one or more tool-call spans and one or more model-inference spans (`gen_ai.operation.name = \"chat\"`). Does not apply to single-shot chat spans with no nested children, nor to traces where the exporter aggregates token usage after the fact outside the span tree.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-double-counting-of-tokens-across-nested-agent-and-tool-spans-in-opentelemetry-genai-semantic-conventions.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-double-counting-of-tokens-across-nested-agent-and-tool-spans-in-opentelemetry-genai-semantic-conventions.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-double-counting-of-tokens-across-nested-agent-and-tool-spans-in-opentelemetry-genai-semantic-conventions.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1695,
    "python_slice_tokens": 391,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "There are two distinct notions of \"token usage\" in a GenAI trace, and conflating them is the root of nearly every accounting error: 1. **Span-local usage** — the `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` attributes recorded *on a single span*, representing only the tokens that span's own operation consumed or produced. 2. **Subtree aggrega...",
    "what_was_observed": "In a multi-step agent trace, the typical shape is: - `invoke_agent` span (root), carrying `gen_ai.usage.input_tokens = 1200`, `gen_ai.usage.output_tokens = 80` — the planner turn. - child `chat` span, `gen_ai.usage.input_tokens = 1200`, `gen_ai.usage.output_tokens = 80` — the *same* underlying model call, re-recorded by the instrumentation layer at the chat boundary. - child `execute_tool` spans, no token usage (tool...",
    "condition_of_validity": "The non-additivity invariant holds only under the following conditions, and must be re-verified when any fails: - **The `invoke_agent` span and its inner `chat` span describe the same model call.** If the agent framework makes a distinct planning call *in addition to* the chat call (e.g., a separate router model), then both spans are independent and *are* ad...",
    "decision_boundary": "The operational rule for any consumer computing token cost from a GenAI trace: > Sum `gen_ai.usage.*` only over **leaf-most model-operation spans** (`gen_ai.operation.name` in `{\"chat\", \"text_completion\", \"embeddings\"}`) that have **no descendant model-operation span** describing the same call. Never sum over `invoke_agent`/`create_agent` spans that have a m...",
    "operational_effect": "Downstream cost dashboards built on naive summation over-report agent-run cost by the fraction of tokens spent in planner turns that are double-recorded — observed at 25–40% inflation in multi-tool agent workloads. Because the inflation is proportional to planning overhead, it is *not* a constant offset and cannot be corrected by a fixed multiplier; it must ..."
  },
  "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": "5d1ab3bdf0c37e553823a6bb747d39eb2f2117d6e26d939aa6e8ac17242466a9",
    "verified_classes": [
      "TokenAccountingInvariantError",
      "Span"
    ],
    "verified_functions": [
      "billable_input_tokens",
      "ancestors"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "TokenAccountingInvariantError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
