{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-tool-call-span-attribution-from-silently-double-counting-tokens-across-nested-genai-spans",
  "title": "Which invariants prevent tool-call span attribution from silently double-counting tokens across nested GenAI spans",
  "date_of_record": "2026 November 30",
  "scope": "Applies to agent runtimes that emit OpenTelemetry GenAI semantic-convention spans (`gen_ai.*`) for LLM inference and tool execution, where a single logical turn may nest a parent chat span around child tool-call spans that themselves trigger further inference. Does not apply to single-shot, non-nested inference telemetry or to pure transport-level tracing without GenAI attributes.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-tool-call-span-attribution-from-silently-double-counting-tokens-across-nested-genai-spans.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-tool-call-span-attribution-from-silently-double-counting-tokens-across-nested-genai-spans.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-tool-call-span-attribution-from-silently-double-counting-tokens-across-nested-genai-spans.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1636,
    "python_slice_tokens": 340,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The failure is not \"missing spans\" or \"broken export.\" It is **attribution ambiguity under nesting**: the GenAI semantic conventions define `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as attributes of a chat span, and separately define tool-call spans. When an agent loop executes `model -> tool -> model`, the second model call is a *new* inf...",
    "what_was_observed": "A three-step agent turn (`plan -> call search tool -> synthesize`) produced a trace with four spans: one root `invoke_agent`, one `chat` for planning, one `execute_tool` for search, one `chat` for synthesis. The root span was populated by a wrapper that copied the *terminal* chat span's `gen_ai.usage.*` onto the root for \"convenience.\" The synthesis chat span also carried its own usage. A dashboard summing `gen_ai.us...",
    "condition_of_validity": "The invariant that must hold for token sums to be meaningful: > **Exactly one span per logical inference operation carries `gen_ai.usage.*`, and that span is the leaf-most span whose operation is that inference. No ancestor span and no event within the span may carry the same usage.** This is valid only when: - The emitting runtime distinguishes *operation s...",
    "decision_boundary": "The boundary is **whether a span's usage attribute describes the operation the span *is*, or the subtree the span *contains***. The GenAI conventions are written for the former. Any wrapper that treats a coordination span as a container for usage crosses the boundary and creates an aggregatable duplicate. Concrete decision rules: - If a span has child spans ...",
    "operational_effect": "Enforcing the leaf-only invariant changed the reported input-token total from 6640 to 4230 for the sample turn — a 36% overcount eliminated without touching the provider. The retry double-count was eliminated by re-parenting retried attempts, which also fixed a latency-attribution bug (retry latency had been attributed to the parent, inflating p99 for the co..."
  },
  "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": "ef7f668883d203d9a8603191b0976c880d98d22d7bef177396f6236008a8c5d2",
    "verified_classes": [
      "UsageAttributionError",
      "GenAISpan"
    ],
    "verified_functions": [
      "validate_usage_attribution",
      "descendants"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "UsageAttributionError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
