{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-token-accounting-drift-from-corrupting-otel-genai-agent-span-trees-across-retry-and-fan-out-boundaries",
  "title": "Which invariants prevent token-accounting drift from corrupting OTel GenAI agent span trees across retry and fan-out boundaries",
  "date_of_record": "2026 December 2",
  "scope": "Applies to any agent runtime that emits OpenTelemetry `gen_ai.*` spans where a single logical agent turn fans out into multiple tool calls, model invocations, or retries, and where token usage is aggregated upward into a parent span. Does not apply to single-shot, single-span inference calls with no parent/child token rollup.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-drift-from-corrupting-otel-genai-agent-span-trees-across-retry-and-fan-out-boundaries.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-drift-from-corrupting-otel-genai-agent-span-trees-across-retry-and-fan-out-boundaries.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-drift-from-corrupting-otel-genai-agent-span-trees-across-retry-and-fan-out-boundaries.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 2444,
    "python_slice_tokens": 480,
    "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 *model invocation* span (`gen_ai.operation.name = \"chat\"`). They do **not** define an aggregation contract for parent agent spans (`gen_ai.operation.name = \"invoke_agent\"` or `\"execute_tool\"`). This gap is the entire problem. A parent agent...",
    "what_was_observed": "A planner agent (`invoke_agent` span, root) issued a `chat` call that the provider rejected with a 429 at the transport layer. The runtime's retry wrapper re-issued the request. The first attempt had already streamed 1,842 input tokens (the prompt was accepted; the failure was mid-stream on rate-limit backpressure). The retry re-sent the full prompt: another 1,842 input tokens, plus 610 output tokens on success. The ...",
    "condition_of_validity": "The invariant that must hold for parent-span token totals to be trustworthy: > For every agent span S, `S.usage == sum over the set of *logical* model invocations L(S) of the authoritative usage tuple reported by the provider for each l in L(S)`, where L(S) is the set of invocations the runtime *intends* to bill to S, and retries of a single logical invocati...",
    "decision_boundary": "The decision the runtime must make at emit time, for each child model invocation, is: *does this child's usage roll into the parent's total, and if so, as a new logical invocation or as a replacement for a prior attempt?* The boundary is crossed wrongly in three specific ways: - **Dedup-by-request-ID across fan-out.** Two parallel samples with distinct reque...",
    "operational_effect": "When the invariant holds, the parent `invoke_agent` token total is a sound input to budget enforcement, cost attribution, and loop detection (a runaway agent shows monotonic parent-token growth per turn). When it is violated, three downstream systems corrupt simultaneously and silently: - **Budget guardrails** fire on inflated totals (fan-out duplicate) or f..."
  },
  "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": "ff7ebe1581b98fd1381ac4a3056505509f2a040a1be2213c91873a50f209ef3c",
    "verified_classes": [
      "TokenAggregationInvariantError"
    ],
    "verified_functions": [
      "aggregate_agent_usage",
      "enforce_turn_budget"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 5,
    "raised_exceptions": [
      "TokenAggregationInvariantError"
    ],
    "sample_fail_closed_invariants": [
      "negative token total; child usage tuple is corrupt",
      "parent agent span has no declared aggregation_policy; refusing to enforce budget on an unsound derived total"
    ],
    "stdlib_dependencies": [],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
