{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-token-accounting-drift-from-silently-corrupting-genai-span-trees-under-retry-and-streaming",
  "title": "Which invariants prevent token-accounting drift from silently corrupting GenAI span trees under retry and streaming",
  "date_of_record": "2026 November 24",
  "scope": "Applies to agents emitting OpenTelemetry spans per the GenAI semantic conventions (`gen_ai.*`) where a single logical tool call or model turn may be retried, streamed incrementally, or replayed across multiple span objects. Does not cover pure metrics pipelines or log-only instrumentation without span context.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-drift-from-silently-corrupting-genai-span-trees-under-retry-and-streaming.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-drift-from-silently-corrupting-genai-span-trees-under-retry-and-streaming.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-drift-from-silently-corrupting-genai-span-trees-under-retry-and-streaming.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1540,
    "python_slice_tokens": 307,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions describe *what attributes a span should carry* — `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.request.model`. They do **not** define an invariant on *how many spans may carry usage attributes for one logical operation*. This is the boundary. A single agent turn ...",
    "what_was_observed": "An agent runtime wrapping a provider SDK emitted one span per HTTP attempt under a parent `gen_ai.operation.name=chat` span. Under normal conditions the tree looked clean: one child span, usage on the child. Under a retry storm (provider returning 529s), the tree grew to N children, each with `gen_ai.usage.input_tokens` set to the *full prompt token count* because the SDK reports usage on the request object regardles...",
    "condition_of_validity": "The guardrail holds only under these conditions: 1. The emitter owns a *logical operation identifier* that is stable across retries — typically the parent span's `span_id`, or an explicit `gen_ai.operation.id` attribute if you introduce one. Retries must be children of, or siblings tagged with, that identifier. 2. Exactly one attempt is designated authoritat...",
    "decision_boundary": "The decision the emitter faces at span-close time is: *does this attempt's usage reflect tokens the agent will actually be billed for and whose output it consumed?* Three cases: - **Attempt succeeded and its response was parsed into agent state** → authoritative. Emit usage, emit finish reasons, set `gen_ai.usage.authoritative=true`. - **Attempt failed befor...",
    "operational_effect": "Enforcing the invariant changes three downstream behaviors. First, cost dashboards stop drifting during retry storms — the over-count factor collapses to 1.0 because non-authoritative spans are excluded by attribute, not by heuristic. Second, token-budget enforcement inside the agent loop becomes correct: a budget check that reads `gen_ai.usage.output_tokens..."
  },
  "matched_failure_symptoms": [
    "OOM_KILL_OR_MEMORY_EXHAUSTION"
  ],
  "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": "e8429756db5252e11db1ee3656c647446d484f99debe29c69a8cfe8251bbe700",
    "verified_classes": [
      "NonAuthoritativeUsageError",
      "GenAISpan"
    ],
    "verified_functions": [
      "close_attempt_span",
      "aggregate_authoritative_usage"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "NonAuthoritativeUsageError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
