{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-emit-silent-token-accounting-gaps-when-genai-spans-are-closed-after-context-compaction",
  "title": "Why does an instance emit silent token-accounting gaps when GenAI spans are closed after context compaction?",
  "date_of_record": "2026 November 24",
  "scope": "Applies to agents instrumented with OpenTelemetry GenAI semantic conventions (`gen_ai.*`) where a long-running reasoning loop performs context compaction, summarization, or truncation mid-span and the span is closed after the mutation. Does not apply to stateless single-turn completions or to spans closed synchronously at the end of each model call before any state mutation.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-emit-silent-token-accounting-gaps-when-genai-spans-are-closed-after-context-compaction.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-emit-silent-token-accounting-gaps-when-genai-spans-are-closed-after-context-compaction.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-emit-silent-token-accounting-gaps-when-genai-spans-are-closed-after-context-compaction.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1825,
    "python_slice_tokens": 328,
    "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 recorded on the span that represents a model invocation. The convention assumes the span's lifetime brackets exactly one inference call whose input is the context actually submitted to the model. This assumption is an *invariant of bracketing*: th...",
    "what_was_observed": "A research agent running a 40-turn tool-using loop emitted spans that, when summed, accounted for 61% of the tokens the provider billed over the same window. The gap was not uniform — it concentrated on turns where the agent's context manager triggered compaction (context window above 82% utilization). The trace showed the following sequence on affected turns: 1. `span.start_span(\"chat\")` with `gen_ai.operation.name=...",
    "condition_of_validity": "The bracketing invariant holds — and the conventions are safe to rely on for accounting — only when all of the following are true at the moment `span.end()` executes: - The span's usage attributes were captured as an immutable snapshot at the moment the corresponding model response was received, not read from a mutable accumulator later. - Every model invoca...",
    "decision_boundary": "The operational decision is whether to *snapshot at response* or *accumulate at close*. Snapshotting is correct but requires the instrumentation to treat usage as a value captured in the response callback, not a field on a long-lived agent object. Accumulating at close is simpler and is what most auto-instrumentation does by default, and it is acceptable *on...",
    "operational_effect": "The immediate effect of the broken invariant is silent under-attribution of cost and latency concentrated on long, compaction-heavy sessions — precisely the sessions where cost control matters most. Because the spans are valid, no alert fires; the discrepancy is only visible by reconciling against the provider invoice, which lags by a billing cycle. The arch..."
  },
  "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": "6ba7508f1f246e16d3f38f8bd15394d2e09588cbc128c6edd8e1e9441c479227",
    "verified_classes": [
      "UsageInvariantViolation",
      "UsageSnapshot",
      "GenAISpan"
    ],
    "verified_functions": [
      "record_usage",
      "add_child_usage",
      "end"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 7,
    "raised_exceptions": [
      "UsageInvariantViolation"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
