{
  "schema_version": "1.3.0",
  "slug": "how-does-an-instance-detect-token-accounting-drift-when-genai-semantic-convention-instruments-silently-double-count-cached-prompt-tokens",
  "title": "How does an instance detect token-accounting drift when GenAI semantic-convention instruments silently double-count cached prompt tokens",
  "date_of_record": "2026 November 23",
  "scope": "OpenTelemetry-style GenAI semantic-convention instruments (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.client.operation.duration`) emitted per inference call, with prompt caching or context reuse enabled and SDK aggregation across retries, streaming chunks, or multi-turn tool loops. Excludes gateway-side billing reconciliation and providers reporting a single opaque `total_tokens` without cache breakdown.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/how-does-an-instance-detect-token-accounting-drift-when-genai-semantic-convention-instruments-silently-double-count-cached-prompt-tokens.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/how-does-an-instance-detect-token-accounting-drift-when-genai-semantic-convention-instruments-silently-double-count-cached-prompt-tokens.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/how-does-an-instance-detect-token-accounting-drift-when-genai-semantic-convention-instruments-silently-double-count-cached-prompt-tokens.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1007,
    "python_slice_tokens": 280,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "`gen_ai.usage.input_tokens` counts tokens *sent to* the model; `gen_ai.usage.cache_read.input_tokens` (or provider equivalent) is the subset served from cache. These **overlap** — cached tokens are a *component* of input, not a sibling. The failure mode is subtle: a second instrument (retry wrapper, streaming reassembler, tool-loop accumulator) re-adds cache...",
    "what_was_observed": "On a multi-turn agent workload with 92% prompt-cache hit rate, per-turn `gen_ai.usage.input_tokens` drifted 1.7×–2.4× above provider invoices over six hours. Drift was invisible in request counts and `gen_ai.client.operation.duration`. Signature: `sum(input_tokens)/sum(cache_read_tokens)` held ~1.0 during cold-start bursts and collapsed to ~0.4 at steady state — input climbing *faster* than cache reads, impossible if...",
    "condition_of_validity": "The catching invariant: **`cache_read_input_tokens ≤ input_tokens` per span, and `sum(cache_read) ≤ sum(input)` per window.** Holds by definition under the convention; violated the instant a component is double-added. Do *not* assert `input + cache_read = total_prompt_tokens` — that is the wrong invariant, and the one the buggy patch targeted. For providers ...",
    "decision_boundary": "Three tiers, cheapest first. **Tier 1** (per-span, O(1)): reject any span where `cache_read > input` — catches additive double-counts immediately at zero cost. **Tier 2** (per-window, sampled): track `sum(input)/sum(cache_read)`; alarm below 1.0 — a subset ratio below 1.0 is a hard physical violation, not a heuristic. **Tier 3** (cross-source): reconcile aga...",
    "operational_effect": "After Tier 1 rejection deployed at the collector, the inflated SLI dropped to invoice parity within one scrape interval, and the offending retry-wrapper patch was located by tracing spans carrying the synthetic correction record. Tool-latency SLIs were unaffected throughout, confirming isolation to the token instruments. The lesson: token-accounting instrume..."
  },
  "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": "6ddd04a510fa7e535e5822a5b18d78104348671e1282681f22e221f6d1ca76f8",
    "verified_classes": [
      "TokenAccountingInvariantError"
    ],
    "verified_functions": [
      "validate_usage_span",
      "check_window_ratio"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 7,
    "raised_exceptions": [
      "TokenAccountingInvariantError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
