{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-otel-genai-span-trees-from-silently-double-counting-tokens-when-an-agent-retries-a-tool-call-inside-a-nested-span",
  "title": "Which invariants prevent OTel GenAI span trees from silently double-counting tokens when an agent retries a tool call inside a nested span",
  "date_of_record": "2026 November 22",
  "scope": "Applies to agents instrumented with OpenTelemetry GenAI semantic conventions (`gen_ai.*` attributes) where tool invocations are emitted as child spans under an `invoke_agent` span and token usage is recorded per span. Does not cover pure prompt/response logging without span emission, nor billing reconciliation downstream of the collector.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-otel-genai-span-trees-from-silently-double-counting-tokens-when-an-agent-retries-a-tool-call-inside-a-nested-span.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-otel-genai-span-trees-from-silently-double-counting-tokens-when-an-agent-retries-a-tool-call-inside-a-nested-span.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-otel-genai-span-trees-from-silently-double-counting-tokens-when-an-agent-retries-a-tool-call-inside-a-nested-span.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1651,
    "python_slice_tokens": 288,
    "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 on the *operation* span (typically the `chat` or `text_completion` span), not on the agent span. The agent span (`gen_ai.operation.name = \"invoke_agent\"`) is a *parent* that aggregates child operations. The failure mode arises when an agent retrie...",
    "what_was_observed": "A tool-calling agent (`invoke_agent` span, `gen_ai.agent.name = \"researcher\"`) invoked a search tool. The first attempt returned a 503 from the upstream tool; the agent's retry wrapper re-issued the call within the same logical step. Instrumentation details: - The `execute_tool` child span for attempt 1 carried `gen_ai.usage.input_tokens = 812`, `gen_ai.usage.output_tokens = 44`. - Attempt 2 (retry) emitted a *new* `...",
    "condition_of_validity": "The double-count is only *observable* when all three of the following hold: 1. The retry is implemented as a re-invocation that produces a sibling `execute_tool` (or `chat`) span, not as an in-place update of a single span. 2. The parent `invoke_agent` span carries `gen_ai.usage.*` attributes (violating the convention) *or* the aggregation query sums across ...",
    "decision_boundary": "The boundary is between **logical-step accounting** (what the agent's planner thinks it did) and **invocation accounting** (what the provider bills). These diverge precisely at retries, and the OTel GenAI conventions are silent on retry semantics — they define the attributes but not the topology of retry spans. Engineers must decide, per agent, one of: - **I...",
    "operational_effect": "The over-count is silent: no error, no dropped span, just a dashboard that reads 1.5×–3× the true bill. It surfaces only when someone reconciles against the provider's invoice, typically weeks later, and by then the retry topology that caused it has been refactored. The under-count (event-based retries) is equally silent and biases the opposite way, under-pr..."
  },
  "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": "afaa3c9cb32c043fe68ef12fd73cad2d128e0b7e0cb448d8bf2b170d9b76f888",
    "verified_classes": [
      "GenAiSpanInvariantError",
      "Span"
    ],
    "verified_functions": [
      "assert_leaf_only_usage",
      "sum_billable_tokens",
      "build_retry_trace",
      "set_usage"
    ],
    "verified_functions_count": 4,
    "defensive_guards_count": 1,
    "raised_exceptions": [
      "GenAiSpanInvariantError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
