{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-token-accounting-span-collapse-when-genai-agent-traces-cross-a-sampling-or-batching-boundary",
  "title": "Which invariants prevent token-accounting span collapse when GenAI agent traces cross a sampling or batching boundary",
  "date_of_record": "2026 November 29",
  "scope": "Applies to OpenTelemetry-instrumented LLM agents that emit `gen_ai.*` spans (chat, execute_tool, invoke_agent) through a batching or tail-sampling collector pipeline. Does not apply to synchronous, single-process exporters that flush one span per request with no intermediate processor.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-span-collapse-when-genai-agent-traces-cross-a-sampling-or-batching-boundary.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-span-collapse-when-genai-agent-traces-cross-a-sampling-or-batching-boundary.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-span-collapse-when-genai-agent-traces-cross-a-sampling-or-batching-boundary.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1621,
    "python_slice_tokens": 209,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The failure here is not \"lost traces.\" It is a *partial* trace: an `invoke_agent` parent span survives with its `gen_ai.usage.input_tokens` / `output_tokens` attributes intact, but its child `gen_ai.chat` and `gen_ai.execute_tool` spans are dropped by a tail sampler or evicted from a full batch queue. The parent's token attributes are *aggregate projections*...",
    "what_was_observed": "A multi-step agent (planner → 3 tool calls → summarizer) ran under an OTel SDK with `BatchSpanProcessor` (queue 2048, batch 512, 5s schedule) feeding a tail-sampling collector configured to keep traces with `gen_ai.usage.total_tokens > 10000` and drop the rest of the trace's spans below a 1% probabilistic floor. Trigger condition: the planner's `invoke_agent` span closed *last* (correct parent-close ordering), carryi...",
    "condition_of_validity": "The conservation invariant `parent.total_tokens = Σ(descendant.total_tokens) + parent.own_overhead` holds **only** when all of the following are true simultaneously: - Every descendant span that contributed to the parent aggregate is emitted, and emitted *before* the parent closes (so the parent can be validated against children at export). - The sampling de...",
    "decision_boundary": "The architectural guardrail is a **provenance seal on aggregate attributes**, enforced at span close and verified at collector ingest: - On `invoke_agent` close, the agent writes `gen_ai.usage.total_tokens` *and* a companion attribute `gen_ai.usage.descendant_span_ids` (a set of child span IDs that contributed) plus `gen_ai.usage.descendant_count`. - The col...",
    "operational_effect": "With the seal in place, the failure mode converts from silent under-attribution to an explicit, countable `aggregate_unreconciled` rate. In the observed pipeline this rate was 4.7% of traces at the 1% floor and dropped to 0.0% once the sampler was reconfigured to sample at trace granularity with a queue sized to 2× max trace span count. The trade-off is memo..."
  },
  "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": "302ddba257e77182417b2e01f78238341df219f9c2214fb7957475d8fdf3b150",
    "verified_classes": [
      "AggregateProvenanceError"
    ],
    "verified_functions": [
      "seal_agent_span",
      "admit_root_span",
      "enforce_conservation"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "AggregateProvenanceError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
