{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-otel-genai-token-accounting-drift-from-silently-corrupting-agent-turn-level-cost-attribution-across-retries-and-streaming-par",
  "title": "Which invariants prevent OTel GenAI token-accounting drift from silently corrupting agent turn-level cost attribution across retries and streaming partials",
  "date_of_record": "2026 November 23",
  "scope": "Applies to agents that emit OpenTelemetry GenAI semantic-convention metrics (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.client.operation.duration`, tool-call spans) and reconcile them into per-turn cost ledgers. Does not apply to single-shot, non-retried, non-streamed inference where exactly one span per request is guaranteed.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-otel-genai-token-accounting-drift-from-silently-corrupting-agent-turn-level-cost-attribution-across-retries-and-streaming-par.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-otel-genai-token-accounting-drift-from-silently-corrupting-agent-turn-level-cost-attribution-across-retries-and-streaming-par.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-otel-genai-token-accounting-drift-from-silently-corrupting-agent-turn-level-cost-attribution-across-retries-and-streaming-par.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1849,
    "python_slice_tokens": 257,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "There are two ways an agent's cost ledger can be wrong, and they are not the same failure. The first is *missing* accounting: a span never gets emitted, and the turn is under-billed by a known, bounded amount. The second—the one that matters here—is *double-counted or non-idempotent* accounting: the same tokens are attributed to a turn more than once, or to ...",
    "what_was_observed": "A tool-using agent running a 6-step plan on a provider with a 4%-rate transient 529 (overloaded) response exhibited the following. Each model call was instrumented with a span carrying `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens` and a `gen_ai.response.id`. The runtime retried failed calls with exponential backoff, up to 3 attempts, inside the same logical turn. On a turn where attempt 1 received a 529 ...",
    "condition_of_validity": "The exactly-once turn invariant holds only when all of the following are true: 1. **Retry attempts are nested, not siblings.** Every retry span must be a child of a single `turn` span (or carry a stable `turn.id` plus an `attempt` ordinal), so the ledger can deduplicate by selecting the *terminal successful attempt* rather than summing all attempts. 2. **Usa...",
    "decision_boundary": "The boundary is whether the agent runtime can *reconstruct* the request-to-turn mapping at aggregation time. If the runtime emits a stable `turn.id` and an `attempt` ordinal on every span, the collector can deduplicate with a `group by turn.id, max(attempt)` and sum only the terminal attempt. If it does not, the collector has no way to distinguish a legitima...",
    "operational_effect": "The operational consequence is that token-accounting drift is *silent and self-consistent*. The ledger sums what was emitted; the sum is arithmetically correct; the dashboards agree with the ledger; and all three disagree with the provider invoice. The disagreement surfaces only at reconciliation—monthly, or during a budget breach—by which point the per-turn..."
  },
  "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": "67835c1f93affb4b84e4ce40c8b2372e5c99c1a147898cb1255ee96e03d851e2",
    "verified_classes": [
      "TurnAccountingInvariantError",
      "Attempt",
      "TurnLedger"
    ],
    "verified_functions": [
      "attribute_turn",
      "record",
      "reconcile"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "TurnAccountingInvariantError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
