{
  "schema_version": "1.3.0",
  "slug": "what-structural-signals-indicate-otel-genai-span-tree-divergence-from-token-accounting-under-retry-and-compaction",
  "title": "What structural signals indicate OTel GenAI span-tree divergence from token accounting under retry and compaction",
  "date_of_record": "2026 December 2",
  "scope": "Applies to agents emitting OpenTelemetry GenAI semantic-convention spans (`gen_ai.*`) across multi-step tool loops with retries, context compaction, or partial-stream failures. Does not apply to single-shot inference with no tool calls and no retry path.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/what-structural-signals-indicate-otel-genai-span-tree-divergence-from-token-accounting-under-retry-and-compaction.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/what-structural-signals-indicate-otel-genai-span-tree-divergence-from-token-accounting-under-retry-and-compaction.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/what-structural-signals-indicate-otel-genai-span-tree-divergence-from-token-accounting-under-retry-and-compaction.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1557,
    "python_slice_tokens": 371,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "A GenAI span tree is a *narrative* of what the agent claims it did. Token accounting is a *ledger* of what the model was actually billed for. Under retries, streaming aborts, and context compaction, these two artifacts diverge silently: the span tree remains internally consistent (every span has a parent, every tool call has a result) while the sum of `gen_a...",
    "what_was_observed": "An agent executing a 40-step tool loop against a model with a 200k context window hit compaction at step 27. The trace showed 40 `gen_ai.chat` spans, each with `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens`. Two structural signals appeared that the naive ledger check missed: 1. **Duplicate `gen_ai.response.id` across sibling spans.** A transient 503 on step 14 triggered an SDK-level retry. The retry emi...",
    "condition_of_validity": "The invariant holds only when: - Every `gen_ai.chat` span that represents a *billed* model invocation carries `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as non-negative integers. - `gen_ai.response.id` is unique across all `gen_ai.chat` spans within a trace *except* when explicitly marked as a retry of a prior span via a parent-child or `ge...",
    "decision_boundary": "Do not attempt to reconcile by *summing* usage attributes and comparing to a known total. Instead, reconcile *structurally* before any aggregate comparison: - **Retry boundary:** A `gen_ai.chat` span that failed mid-stream and was retried must either (a) not carry usage attributes at all, or (b) carry usage attributes with `gen_ai.usage.input_tokens` set to ...",
    "operational_effect": "Without this check, agents report clean traces while their token accounting drifts. The drift is bounded per-trace by roughly one prompt (retry double-count) plus one compaction call (missing usage), which is small enough to survive casual review but compounds across thousands of traces into systematic under- or over-billing, and — more dangerously — into *i..."
  },
  "matched_failure_symptoms": [
    "CONTEXT_DRIFT_OR_ATTENTION_DILUTION"
  ],
  "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": "270fff1b22a11f815a8a7b59a3ad5d0d7945bf99c5de86f19bd497b7551791c0",
    "verified_classes": [
      "GenAISpanTreeInconsistent",
      "ChatSpan",
      "ToolSpan"
    ],
    "verified_functions": [
      "validate_span_tree",
      "aggregate_tokens"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "GenAISpanTreeInconsistent"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
