{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-kv-cache-aligned-compaction-from-silently-re-billing-cached-prefix-tokens-as-fresh-input",
  "title": "Which invariants prevent KV-cache-aligned compaction from silently re-billing cached prefix tokens as fresh input",
  "date_of_record": "2026 November 21",
  "scope": "Applies to agents that drive multi-turn execution against a provider exposing prefix-cache billing (Anthropic prompt caching, OpenAI automatic prefix caching, Gemini context caching, or self-hosted vLLM/SGLang block caches) and that run a compaction/eviction scheduler to stay under a context-window token budget. Does not apply to stateless single-shot inference or to providers that bill flat per-token regardless of prefix reuse.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-kv-cache-aligned-compaction-from-silently-re-billing-cached-prefix-tokens-as-fresh-input.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-kv-cache-aligned-compaction-from-silently-re-billing-cached-prefix-tokens-as-fresh-input.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-kv-cache-aligned-compaction-from-silently-re-billing-cached-prefix-tokens-as-fresh-input.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1814,
    "python_slice_tokens": 364,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "There are two distinct token ledgers an agent must track, and conflating them is the source of the failure: - **Occupancy** — how many tokens currently sit in the live context window (the number that must stay under the model's max and that drives per-turn attention cost). - **Cache residency** — how many of those tokens match a previously-billed prefix at a...",
    "what_was_observed": "A long-running research agent (multi-turn, ~40 tool calls per task) ran against a provider with 1024-token cache blocks and a 5-minute TTL. Its scheduler triggered compaction whenever occupancy exceeded 85% of a 128k window. The compaction strategy was \"summarize the oldest 30% of turns into a single digest block, keep recent turns verbatim.\" Trace of a representative failure: 1. Turn N: occupancy 109k. Suffix from t...",
    "condition_of_validity": "The \"compaction saves money\" assumption is valid only under all of the following: - **Prefix stability:** the compacted prefix is byte-identical across turns, so the cache-aligned blocks it occupies remain resident. Any edit to the head (a digest whose wording is regenerated by an LLM each time, a timestamp, a re-ordered system prompt) breaks this. - **Suffi...",
    "decision_boundary": "The scheduler's compaction trigger must be gated on a **cache-aware cost predicate**, not on occupancy alone. Concretely, before evicting a span `[a, b)`: - Compute `evicted = b − a` and `suffix = current_occupancy − b`. - Require `evicted > suffix` **OR** require that the eviction point `b` is block-aligned and that the post-compaction prefix `[0, a) + dige...",
    "operational_effect": "Systems that respect the invariant show a flat or declining billed-input curve across a long task, with occupancy oscillating under the budget. Systems that violate it show a sawtooth in billed input that tracks compaction events one turn later, while occupancy metrics look healthy. The observable signature of the bug is: **cache-read token share drops sharp..."
  },
  "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": "13f2469d042dbd9905f4dea36e91b4a1b07de098b32b03368fefb62ed8e47078",
    "verified_classes": [
      "CacheInvalidatingCompaction"
    ],
    "verified_functions": [
      "plan_compaction",
      "maybe_compact"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "CacheInvalidatingCompaction"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
