{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-double-counting-and-span-nesting-corruption-in-opentelemetry-genai-agent-spans-under-retry-and-fan-out",
  "title": "Which invariants prevent double-counting and span-nesting corruption in OpenTelemetry GenAI agent spans under retry and fan-out",
  "date_of_record": "2026 November 14",
  "scope": "Applies to instrumentation that emits `gen_ai.*` spans per the OpenTelemetry GenAI semantic conventions for agents, tool calls, and token usage, specifically when an agent framework performs internal retries, parallel tool fan-out, or nested sub-agent delegation. Does not apply to single-shot, non-retrying LLM calls where one span maps 1:1 to one provider request.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-double-counting-and-span-nesting-corruption-in-opentelemetry-genai-agent-spans-under-retry-and-fan-out.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-double-counting-and-span-nesting-corruption-in-opentelemetry-genai-agent-spans-under-retry-and-fan-out.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-double-counting-and-span-nesting-corruption-in-opentelemetry-genai-agent-spans-under-retry-and-fan-out.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1485,
    "python_slice_tokens": 353,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions describe *what* to record (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.tool.call.id`) but they do not, by themselves, define *where the accounting boundary sits*. The non-obvious failure is a conflation of two different questions: 1. **Attribution** — which spa...",
    "what_was_observed": "A three-level agent (orchestrator → sub-agent → tool-using LLM) with a retry wrapper produced the following in a single trace: - The orchestrator span carried `gen_ai.usage.input_tokens = 4200`. - The sub-agent span carried `gen_ai.usage.input_tokens = 4200` as well — because the framework's \"aggregate usage\" helper copied child totals onto the parent. - The leaf LLM span carried the true `gen_ai.usage.input_tokens =...",
    "condition_of_validity": "The leaf-local invariant holds — and rollups are safe — only when all of the following are true: - Every span that carries `gen_ai.usage.*` corresponds to exactly one provider request/response pair. Retries MUST emit a **new span per attempt**, and only the successful (or final) attempt's usage is the \"billable\" one; failed attempts may carry usage but MUST ...",
    "decision_boundary": "Choose the accounting strategy based on whether your pipeline can see span hierarchy: - **If the consumer has full trace trees** (e.g., a backend with parent/child linkage): emit leaf-local usage only and compute rollups at query time. This is the correct default. - **If the consumer sees only a flat span stream** (e.g., a metrics exporter that converts span...",
    "operational_effect": "Teams that violate the leaf-local invariant typically discover it via a cost dashboard that reads 3–7× the provider invoice, or via p99 latency that is *lower* than reality because retried attempts were deduplicated away. The failure is silent, schema-passing, and only surfaces under retry or fan-out — i.e., exactly the conditions that matter most for agent ..."
  },
  "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": "c6d01d18ebe279753ff2ed09f3e5df2008a30da3c84ac1b38e6e0365ee6ddfcc",
    "verified_classes": [
      "GenAiSpanInvariantError",
      "GenAiSpan"
    ],
    "verified_functions": [
      "validate_trace",
      "rollup_input_tokens"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "GenAiSpanInvariantError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "__future__",
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
