{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-opentelemetry-genai-span-hierarchies-from-silently-double-counting-tokens-across-agent-handoffs",
  "title": "Which invariants prevent OpenTelemetry GenAI span hierarchies from silently double-counting tokens across agent handoffs",
  "date_of_record": "2026 November 19",
  "scope": "Applies to agents instrumented with the OpenTelemetry GenAI semantic conventions (`gen_ai.*` attributes) where a parent agent span emits child spans for LLM invocations and tool calls, and where token usage is aggregated from child spans up to a root. Does not apply to single-span, single-LLM-call instrumentation with no span nesting, nor to billing systems that read provider-side usage directly rather than reconstructing it from traces.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-opentelemetry-genai-span-hierarchies-from-silently-double-counting-tokens-across-agent-handoffs.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-opentelemetry-genai-span-hierarchies-from-silently-double-counting-tokens-across-agent-handoffs.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-opentelemetry-genai-span-hierarchies-from-silently-double-counting-tokens-across-agent-handoffs.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 2057,
    "python_slice_tokens": 456,
    "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 *individual model invocation* span. They deliberately do **not** define an aggregation rule for a parent agent span that spans multiple invocations. This leaves a gap: an agent orchestrator span that wraps three LLM calls and two tool calls ...",
    "what_was_observed": "A retrieval-augmented agent was instrumented with a root span `agent.run`, child spans `gen_ai.chat` per model call, and `gen_ai.tool` spans for retrieval. A summarization tool internally called a second model to compress retrieved chunks. On a normal trace the root's derived `input_tokens` matched the provider bill. Under load, three anomalies appeared together: 1. A `gen_ai.chat` span that timed out was retried; bo...",
    "condition_of_validity": "The aggregation invariant `root.input_tokens = Σ_{leaf gen_ai.chat} input_tokens` holds **if and only if** all of the following are true: - **Partition:** every token-consuming model invocation is represented by exactly one span bearing usage attributes, and no span bearing usage attributes contains another such span as a descendant. This forbids usage attri...",
    "decision_boundary": "The boundary is between **trusting the tree** and **trusting a partition witness**. Below the boundary — shallow agents, single model call per span, no retries, no nested LLM-in-tool — summing descendants is safe because the tree happens to be a partition. Above the boundary, the instrumentation must carry an explicit witness that the descendant set is a par...",
    "operational_effect": "Without the partition witness, three downstream systems break simultaneously and silently: - **Cost attribution** over-charges the agent that owns the root span, because retries and handoffs inflate its derived total, making per-agent budgets meaningless. - **Capacity autoscaling** keys on tokens-per-second derived from these spans; the inflated rate trigger..."
  },
  "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": "0dce78354bd83fbfcb3c6fff49a65560597f80bb4b0bfa99fd3b10c280a492f3",
    "verified_classes": [
      "TokenAggregationViolation",
      "GenAISpan"
    ],
    "verified_functions": [
      "_assert_partition",
      "aggregate_tokens",
      "_find",
      "carries_usage",
      "walk"
    ],
    "verified_functions_count": 5,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "TokenAggregationViolation"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
