{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-token-usage-double-counting-across-nested-agent-and-tool-call-spans-in-opentelemetry-genai-traces",
  "title": "Which invariants prevent token-usage double-counting across nested agent and tool-call spans in OpenTelemetry GenAI traces",
  "date_of_record": "2026 November 22",
  "scope": "Applies to agents that emit OpenTelemetry spans following the GenAI semantic conventions (`gen_ai.*` attributes) where an LLM invocation span (`gen_ai.operation.name = \"chat\"`) and its child tool-execution spans (`gen_ai.operation.name = \"execute_tool\"`) both carry token-usage attributes and are aggregated by a downstream collector into cost/throughput metrics. Does not apply to single-span, flat traces where no parent-child token attribution occurs, nor to metrics derived from a single authoritative source (e.g., provider-side billing exports).",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-usage-double-counting-across-nested-agent-and-tool-call-spans-in-opentelemetry-genai-traces.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-usage-double-counting-across-nested-agent-and-tool-call-spans-in-opentelemetry-genai-traces.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-usage-double-counting-across-nested-agent-and-tool-call-spans-in-opentelemetry-genai-traces.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1717,
    "python_slice_tokens": 226,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions define token usage as attributes on the LLM invocation span: `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens`. They do **not** specify that these attributes are mutually exclusive with respect to span nesting. The distinction that matters operationally is between *usage-bearing spans* (spans that actually consumed m...",
    "what_was_observed": "A multi-step tool-using agent emits the following span tree for a single user turn: The agent framework's tracing helper, aiming to make the root span \"self-describing,\" sums the two `chat` spans and writes the totals onto `invoke_agent`. The two `chat` spans remain on the trace unchanged. A collector computing per-turn cost with the natural query — `sum(gen_ai.usage.input_tokens) by trace_id` — now returns **7200**,...",
    "condition_of_validity": "The non-nesting invariant is only enforceable when three conditions hold simultaneously: 1. **Usage is attributed at the leaf.** The instrumentation contract must state that `gen_ai.usage.*` is set only on spans where `gen_ai.operation.name` is `chat` (or `embeddings`/`text_completion`) and the span actually terminates a model request. Framework wrappers tha...",
    "decision_boundary": "The decision boundary is whether to treat `gen_ai.usage.*` as **authoritative leaf-level fact** or as **denormalized convenience aggregate**. The convention's wording supports the former (attributes live on the invocation span), but human ergonomics favor the latter (a root span that answers \"what did this turn cost?\" without a join). These two readings are ...",
    "operational_effect": "Enforcing the non-nesting invariant plus leaf-only aggregation changes three operational behaviors: - **Cost dashboards stop doubling on SDK upgrades.** The 2× regression described above is caught at the collector by the nesting assertion, and the `nesting_conflict` counter fires the moment a framework wrapper starts copying usage upward. The regression is 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": "576fd4c61e1846fc0576f9fd5e918e29e48b909f3947cb9a2183e58d0b4bcd75",
    "verified_classes": [
      "UsageNestingConflict",
      "Span"
    ],
    "verified_functions": [
      "assert_non_nesting",
      "sum_leaf_usage",
      "carries_usage",
      "walk"
    ],
    "verified_functions_count": 4,
    "defensive_guards_count": 1,
    "raised_exceptions": [
      "UsageNestingConflict"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
