# What happens when an instance spends its context-rewriting budget on a self-referential rewrite **Date of Record:** 2026 November 15 **Scope:** Applies to agents whose context policy is model-authored (the model itself emits context rewrite operations: summaries, deletions, reorderings, evictions) and whose rewrite operations are metered by a finite budget. Does not apply to externally-scheduled compaction, to read-only retrieval augmentation, or to budgets enforced by a supervisor that can veto rewrites before they commit. ## Core Distinction A context-rewriting budget is normally understood as a resource that bounds *how much* an agent can forget or compress. That framing is wrong in one specific way: the budget is itself part of the context, and the act of rewriting context consumes budget that must then be reflected in the rewritten context. When a rewrite operation is self-referential — i.e., the rewrite changes the representation of the budget ledger that authorizes the rewrite — the system can enter a state where the ledger is neither the pre-rewrite value nor a valid post-rewrite value. The distinction that matters is between **budget as external counter** (safe, because the counter lives outside the rewritten region) and **budget as in-context fact** (fragile, because the rewrite can edit its own authorization record). ## What Was Observed In a deployment where the model emitted structured rewrite ops (`summarize`, `drop`, `reorder`) and the remaining budget was rendered into the prompt as a ledger block (`budget_remaining: N ops`), the following sequence reproduced deterministically: 1. Ledger reads `budget_remaining: 2`. 2. Model emits a `summarize` op over a long tool-transcript region, and in the same op's replacement text includes a refreshed ledger line it composed itself: `budget_remaining: 2` (it copied the pre-op value, because the op was authored before the op was applied). 3. The runtime applies the op, decrements the *external* counter to 1, but the in-context ledger now says 2. 4. On the next turn the model reads `budget_remaining: 2` from context, believes it has headroom, and emits two more ops. The external counter reaches 0 after the first; the second is rejected at commit time with `BudgetExhausted`. 5. The rejection is injected as a tool error. The model, seeing a rejection while its own ledger says it had budget, concludes the runtime is inconsistent and retries the op with a reworded payload — burning turns, not budget. The non-obvious part is step 2. The model did not "lie." It authored the replacement text from the context it could see, and the context it could see was the *pre-decrement* ledger. The rewrite is evaluated against a snapshot that the rewrite itself invalidates. This is a read-modify-write race between the model's authoring pass and the runtime's accounting pass, mediated by natural language. A second observed variant: the model, low on budget, emitted a `summarize` whose replacement text *omitted* the ledger block entirely ("cleaning up noise"). The runtime had no ledger to reconcile against, treated the in-context budget as unknown, and fell back to a conservative policy that denied all further rewrites for the remainder of the session — a silent, permanent freeze that looked to the operator like the model "giving up." ## Condition of Validity The failure requires all of the following to hold simultaneously: - The budget ledger is rendered *inside* the rewritable region (not in a pinned system prefix or an out-of-band channel). - Rewrite ops are authored in a single pass against a pre-op snapshot, with no read-after-apply reconciliation. - The runtime decrements an external counter but does not *rewrite the in-context ledger to match* after commit. - Commit-time rejection is surfaced to the model as an error rather than as a corrected ledger. If any one is absent, the failure does not occur: pinning the ledger out of the rewritable region removes it; post-commit ledger rewrite by the runtime removes it; reconciling the model's authored ledger against the external counter before commit removes it. The condition is therefore narrow but easy to hit, because "let the model manage its own context" naturally produces all four at once — the model is asked to author the rewrite, the ledger is naturally part of the context, and the simplest runtime just counts. ## Decision Boundary The boundary is not "does the model have budget" but **"is the authoritative budget value inside the region the model can rewrite?"** Concretely: - If the ledger is in the rewritable region **and** the runtime does not reconcile post-commit, treat every model-authored ledger line as *advisory and possibly stale*, never as authorization. The external counter is the only authority. - If the ledger is pinned outside the rewritable region, model-authored ledger text is noise; strip it at commit. - The dangerous middle ground — ledger in-region, runtime decrements but does not reconcile — is where both the over-spend-retry loop and the silent freeze live. Do not operate there. The operational rule: **a rewrite op may never be the authority for its own budget consumption.** Authorization must be read from a source the op cannot edit. If you cannot guarantee that, cap the number of ops per turn to 1 and reconcile the ledger after every commit, accepting the throughput loss. ## Operational Effect When this boundary is crossed, two failure modes dominate, and they are asymmetric: - **Over-spend loop:** the model believes it has budget it does not have, emits ops that are rejected, and interprets rejection as runtime inconsistency. Cost is turns and tokens, not correctness — but it can loop until the turn cap. - **Silent freeze:** the model's own rewrite removes the ledger, the runtime conservatively denies all rewrites, and the session degrades to no-compaction mode. This is worse because it is invisible: no error, no loop, just an agent that stops managing its context and slowly overflows. Mitigation that held in practice: (a) render the ledger in a pinned prefix outside the rewritable region; (b) after every commit, the runtime rewrites the in-context ledger to the true post-decrement value, overwriting whatever the model authored; (c) reject any rewrite op whose replacement text contains a ledger-shaped line, with a specific error the model can learn from; (d) surface budget exhaustion as a *corrected ledger*, not a bare error, so the model's next read is accurate. The following guard encodes (b) and (c) as a commit-time check, and is the form that proved stable: ```python import re LEDGER_RE = re.compile(r"budget_remaining\s*:\s*(\d+)", re.IGNORECASE) class SelfAuthoredBudgetError(RuntimeError): """Raised when a rewrite op attempts to author its own budget ledger inside the rewritable region, or when the authored ledger disagrees with the authoritative external counter post-commit.""" pass class BudgetLedger: def __init__(self, remaining: int): self.remaining = remaining def decrement(self, n: int = 1) -> None: self.remaining -= n def commit_rewrite(op_text: str, ledger: BudgetLedger) -> str: """Apply a model-authored rewrite op against the authoritative ledger. The op may not contain a ledger line: authorization must come from the external counter, never from text the op can edit. The runtime rewrites the in-context ledger itself after commit. """ authored = LEDGER_RE.findall(op_text) if authored: raise SelfAuthoredBudgetError( "rewrite op contains a self-authored budget ledger line " f"({authored!r}); the ledger is pinned outside the rewritable " "region and must not be authored by the op. Remove the ledger " "line and retry; the runtime will render the correct value." ) if ledger.remaining <= 0: raise SelfAuthoredBudgetError( "rewrite budget exhausted; no ops may commit. The ledger has " "been corrected in-context to budget_remaining: 0." ) ledger.decrement(1) # Runtime owns the ledger rendering after commit. return f"{op_text}\n\n[ledger] budget_remaining: {ledger.remaining}" ```