{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-double-counting-of-gen-ai-usage-tokens-across-nested-agent-spans-in-opentelemetry-genai-traces",
  "title": "Which invariants prevent double-counting of `gen_ai.usage.*` tokens across nested agent spans in OpenTelemetry GenAI traces",
  "date_of_record": "2026 November 12",
  "scope": "Applies to OpenTelemetry GenAI semantic conventions (v1.27+) where an agent orchestrator emits parent spans for a multi-turn conversation while child spans model individual LLM invocations, tool calls, or sub-agent delegations. Does not apply to flat, single-span-per-request instrumentation, nor to backends that recompute usage from raw provider payloads rather than trusting span attributes.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-double-counting-of-gen-ai-usage-tokens-across-nested-agent-spans-in-opentelemetry-genai-traces.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-double-counting-of-gen-ai-usage-tokens-across-nested-agent-spans-in-opentelemetry-genai-traces.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-double-counting-of-gen-ai-usage-tokens-across-nested-agent-spans-in-opentelemetry-genai-traces.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1665,
    "python_slice_tokens": 438,
    "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 the span representing *the model invocation that consumed them*. They do not define an aggregation rule for ancestor spans. This creates an ambiguity that operators resolve inconsistently: does a parent agent span carry the sum of its descendan...",
    "what_was_observed": "In a multi-turn tool-using agent (orchestrator span → N LLM-invocation spans → M tool-execution spans, some turns fanning out to sub-agents), we observed three distinct accounting pathologies: 1. **Sum-over-all-spans inflation.** A backend computed `sum(gen_ai.usage.input_tokens)` over every span in the trace. Because the orchestrator span had been instrumented to carry the aggregate (a common \"convenience\" pattern),...",
    "condition_of_validity": "The safe rule holds only under these conditions: - Every span that emits `gen_ai.usage.*` represents a *single physical provider call* (one request/response pair), not a logical turn and not an aggregate. - Aggregation, when needed, is expressed in a *separate, explicitly named* attribute (e.g., `agent.turn.input_tokens_aggregate`) or a span event, never by ...",
    "decision_boundary": "Choose **leaf-only emission** (usage on physical-call spans only; no aggregation on ancestors) when: your backend can walk the full span tree, you need retry-aware cost attribution, or you fan out to sub-agents whose usage must be attributed to distinct cost centers. This is the default and the only mode that survives arbitrary backend aggregation strategies...",
    "operational_effect": "Traces with leaf-only emission and explicit aggregates are robust to backend summation strategy: sum-over-all-spans equals sum-over-leaves (because ancestors carry no `gen_ai.usage.*`), and root-only readers fall back to the named aggregate. Cost dashboards stop inflating with nesting depth. Retry accounting becomes a documented consumer policy rather than a..."
  },
  "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": "a3406da57e6639fbffe793f7a03d43613a0a47499a645309d32696d16686ee50",
    "verified_classes": [
      "UsageAggregationInvariantError",
      "SpanModel"
    ],
    "verified_functions": [
      "validate_usage_placement",
      "emit_aggregate"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 1,
    "raised_exceptions": [
      "UsageAggregationInvariantError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "__future__",
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
