{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-double-counting-of-tokens-when-an-agent-span-and-its-child-tool-call-spans-both-emit-gen-ai-usage-under-opentelemetry-genai-s",
  "title": "Which invariants prevent double-counting of tokens when an agent span and its child tool-call spans both emit `gen_ai.usage.*` under OpenTelemetry GenAI semantic conventions",
  "date_of_record": "2026 November 25",
  "scope": "Applies to any agent runtime that emits OpenTelemetry spans conforming to the GenAI semantic conventions where a parent `invoke_agent` / `execute_tool` span and one or more nested LLM-call or tool-call child spans each populate `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, and `gen_ai.usage.total_tokens`. Does not apply to single-span, leaf-only instrumentation where exactly one span per model invocation carries usage attributes.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-double-counting-of-tokens-when-an-agent-span-and-its-child-tool-call-spans-both-emit-gen-ai-usage-under-opentelemetry-genai-s.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-double-counting-of-tokens-when-an-agent-span-and-its-child-tool-call-spans-both-emit-gen-ai-usage-under-opentelemetry-genai-s.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-double-counting-of-tokens-when-an-agent-span-and-its-child-tool-call-spans-both-emit-gen-ai-usage-under-opentelemetry-genai-s.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1945,
    "python_slice_tokens": 456,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions define *attribute placement* — which span an attribute belongs on — but they do not define an *aggregation contract*. A collector, dashboard, or cost pipeline that sums `gen_ai.usage.total_tokens` across all spans matching `gen_ai.operation.name` will double-count whenever an agent wrapper span mirrors the usage of the LLM call...",
    "what_was_observed": "A LangGraph-style orchestrator was instrumented such that each `invoke_agent` span emitted `gen_ai.usage.input_tokens` equal to the sum of its child `chat` spans, and each `chat` span emitted its own per-call usage. The collector pipeline computed daily spend with the query: For a single agent turn that made three LLM calls (plan, tool-select, synthesize) with 1,200 / 800 / 2,400 total tokens respectively, the trace ...",
    "condition_of_validity": "The aggregation invariant holds only under the following conjunction: 1. **Span-class scoping is explicit.** Every aggregation query filters on `gen_ai.operation.name IN ('chat', 'text_completion')` (leaf model calls) and never sums across `invoke_agent` / `execute_tool` spans. Alternatively, the inverse: sum only `invoke_agent` spans at the root and never d...",
    "decision_boundary": "The choice between leaf-only and rolled-up accounting is a boundary decision that must be made *once per instrumentation library* and documented in the span schema, not inferred at query time. The decision boundary: - **Choose leaf-only** when the primary consumer is a cost/billing pipeline that must attribute tokens to a specific model and operation, and wh...",
    "operational_effect": "Enforcing the invariant changes three operational behaviors: 1. **Cost metrics become nesting-invariant.** Adding a sub-agent or a retry no longer changes the multiplier between reported and true cost. The 2× error that hid behind a constant ratio becomes impossible because the query is scoped to leaf spans, which are additive by construction. 2. **Retry acc..."
  },
  "matched_failure_symptoms": [
    "SUBPROCESS_PIPE_HANG_OR_FD_LEAK"
  ],
  "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": "ee555f1a663600211bd45b9bc70e2f1ef00009c995253ea14d5f18e44a679040",
    "verified_classes": [
      "UsageRollupAmbiguity",
      "Span"
    ],
    "verified_functions": [
      "_has_usage",
      "enforce_usage_invariant",
      "descendants"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "UsageRollupAmbiguity"
    ],
    "sample_fail_closed_invariants": [
      "assert enforce_usage_invariant(trace) == 4400"
    ],
    "stdlib_dependencies": [
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
