{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-double-counted-token-usage-when-otel-genai-spans-nest-across-tool-call-boundaries",
  "title": "Which invariants prevent double-counted token usage when OTel GenAI spans nest across tool-call boundaries",
  "date_of_record": "2026 November 27",
  "scope": "Applies to agent runtimes that emit OpenTelemetry `gen_ai.*` spans for LLM invocations and tool executions, where a parent agent span aggregates child `chat` and `execute_tool` spans and where token accounting is derived from span attributes rather than from provider billing APIs. Does not apply to single-shot inference with no tool loop, nor to runtimes that read usage exclusively from the provider's response object at the top level.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-double-counted-token-usage-when-otel-genai-spans-nest-across-tool-call-boundaries.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-double-counted-token-usage-when-otel-genai-spans-nest-across-tool-call-boundaries.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-double-counted-token-usage-when-otel-genai-spans-nest-across-tool-call-boundaries.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1678,
    "python_slice_tokens": 245,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions define `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as attributes of an *inference* span (`gen_ai.operation.name = chat`). They deliberately do not define an aggregation rule for a parent span that wraps a multi-turn agent loop. The distinction that matters operationally is between **summing child span attribute...",
    "what_was_observed": "A three-turn agent task — `search`, then `read_file`, then `summarize` — produced the following child `chat` spans under one `invoke_agent` span. Each `chat` span carried the conventions-compliant attributes: - Turn 1: `input_tokens=1840`, `output_tokens=210` (emits tool call `search`) - Turn 2: `input_tokens=2430`, `output_tokens=185` (emits tool call `read_file`); note 2430 ≈ 1840 + 210 + tool result + scaffolding ...",
    "condition_of_validity": "The invariant that makes any aggregation correct is: > **For a span tree rooted at `invoke_agent`, the task's input token cost equals the `gen_ai.usage.input_tokens` of the *last* `chat` span in the causal chain, not the sum, provided every `chat` span in the chain shares a cumulative context and no context truncation or summarization has occurred between tu...",
    "decision_boundary": "The boundary between \"sum children\" and \"take last / reconstruct deltas\" is decided by one field the conventions do not currently standardize: whether the prompt is cumulative. Operationally, a runtime must decide this **before** emitting the parent span, and it must record the decision on the parent so downstream consumers cannot re-derive it wrongly. The d...",
    "operational_effect": "Emitting the context-mode annotation converts a silent 137% overstatement into a queryable dimension, and it lets the reconciliation job assert a hard invariant: `parent.input_tokens == last_child.input_tokens` for cumulative trees, with any deviation raising at collection time rather than surfacing as a budget surprise a week later. The tool-span exclusion ..."
  },
  "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": "2b12a5b5fd5d7e0db1a4aad30ac695437af87a88fa9f6c057668717332ee492e",
    "verified_classes": [
      "TokenAccountingInvariantError",
      "ChatSpan"
    ],
    "verified_functions": [
      "aggregate_agent_input_tokens"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 5,
    "raised_exceptions": [
      "TokenAccountingInvariantError"
    ],
    "sample_fail_closed_invariants": [
      "invoke_agent span has no chat children; cannot attribute input tokens",
      "parallel fan-out cannot be reduced to a single scalar; emit per-branch subtotals",
      "negative delta observed but truncations=0; context mode mislabeled"
    ],
    "stdlib_dependencies": [
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
