# How does an instance detect silent budget underflow when tool-result tokens are counted after eviction rather than before **Date of Record:** 2026 November 28 **Scope:** Applies to agent loops that maintain a token budget, evict context to stay under a hard limit, and then append tool results whose size was not known at eviction time. Does not apply to systems that reserve budget for tool results up front or that hard-truncate tool output at the transport layer before it reaches the context assembler. ## Core Distinction There are two ways to keep an agent under a context budget. The first is *pre-accounting*: the planner estimates the token cost of the next tool call's result, reserves that headroom, and evicts or compacts other content to make room before the call is issued. The second is *post-accounting*: the loop appends the tool result to the transcript, recomputes the total token count, and evicts older turns until the total fits. Most production harnesses use post-accounting because it is simpler and does not require estimating result size. The failure mode lives in post-accounting. Eviction is a function of the *current* transcript, but the transcript is not final until the tool result has already been appended. If the tool result is larger than the total slack created by every evictable turn combined, the loop cannot evict enough to fit. It enters a state where the budget is violated and the only remaining evictable content is the very result it just received. The system does not crash; it silently truncates the tool result, drops the oldest turns it can, and proceeds with a transcript that no longer contains the evidence the tool call was made to gather. The distinguishing property: this is not context-window saturation (the window is not full) and not token-budget exhaustion mid-reasoning (reasoning has not started). It is a *budget underflow that is only detectable after the eviction pass fails to converge*, and the failure is masked because the loop still produces a well-formed prompt. ## What Was Observed A research agent running a multi-step retrieval task issued a `read_file` call against a 14,000-token source document while holding 61,200 tokens of prior transcript against a 64,000-token budget. Slack was 2,800 tokens. The eviction policy was "drop oldest tool-result turns first, then oldest assistant turns, keep system prompt and last two user turns pinned." The eviction pass computed a deficit of 11,200 tokens (14,000 incoming minus 2,800 slack). It evicted the three oldest tool-result turns, freeing 9,900 tokens. It then evicted two assistant reasoning turns, freeing 1,400 tokens. Total freed: 11,300 tokens. Deficit remaining: 100 tokens. The evictable set was now empty because the remaining turns were pinned. The loop did not raise. It appended the tool result and truncated it by 100 tokens from the tail. The truncated tail contained the conclusion of the document — the specific finding the agent had called the tool to obtain. The agent then reasoned over a document whose final paragraph was missing, produced a summary that contradicted the (now-evicted) earlier retrieval, and reported success. The run was logged as complete. The truncation was recorded only as a `tokens_truncated: 100` field in a debug log that no downstream check read. The non-obvious part: the eviction pass *succeeded* by its own contract. It freed more tokens than the deficit minus a rounding remainder, and the rounding remainder was absorbed by truncation. The invariant "transcript fits in budget" held. The invariant "the tool result the agent acted on is the tool result the tool returned" did not, and nothing checked it. ## Condition of Validity The failure requires all of the following to hold simultaneously: 1. **Post-accounting eviction.** The result is appended before the budget check, so its size is known only after it is already in the transcript. 2. **A pinned set that cannot shrink.** System prompt, recent user turns, or a "must keep" marker on prior tool results leaves a floor below which eviction cannot go. 3. **A single tool result larger than the total evictable slack.** If the result fits within slack, eviction converges and no truncation occurs. The trigger is `result_tokens > (budget - pinned_tokens - retained_tokens)`. 4. **Tail truncation as the fallback.** The loop's last resort is to cut the result rather than reject the call, re-request with a range, or surface a budget error. When any of these is absent, the failure does not manifest. A harness that estimates result size and refuses the call when it exceeds slack is immune. A harness that raises on non-convergence is immune (it fails loudly). A harness that truncates from the *head* of the result is still wrong but fails differently — the agent sees the conclusion and misses the setup, which is often detectable. ## Decision Boundary The boundary is not "does the transcript fit" but "was the retained tool result modified after receipt, and is the modification visible to the reasoning step that consumes it." The correct guardrail is a *provenance check* on every tool result at the point it is handed to the model: either the result is byte-identical to what the tool returned, or the transcript carries an explicit `truncated: true` marker that the prompt template renders into the model's context (e.g., a trailing `[RESULT TRUNCATED: 100 tokens omitted from tail]` line). The decision the harness must make at eviction time is binary: if the deficit cannot be closed by eviction alone, either (a) reject the call and return a budget error to the planner so it can re-issue with a narrower scope, or (b) accept truncation but mark it in-band. Silently truncating and proceeding is the failure. A secondary boundary: the eviction pass must recompute the deficit *after* each eviction, not estimate it once. In the observed case the pass estimated 11,200 and freed 11,300, but the 100-token remainder came from a tokenizer boundary mismatch between the eviction estimator and the final count. Re-estimating after each eviction and comparing against the true post-append count closes this gap. ## Operational Effect The observable symptom is an agent that completes tasks with high reported confidence while its outputs contradict evidence it retrieved earlier in the same run. The failure is invisible in aggregate metrics because the transcript is well-formed and the budget invariant holds. It surfaces only when a downstream auditor replays the tool calls and compares returned bytes against the bytes the model actually saw. The operational rule: **a tool result is not admitted to the transcript unless its full token cost was reserved before the call was issued, or its truncation is marked in-band and the marker survives into the model's context.** Post-hoc eviction that silently shrinks a result violates the continuity invariant between the tool's return value and the model's evidence. The guardrail is a provenance assertion at admission time, not a budget assertion at assembly time. ```python class TruncatedToolResultError(RuntimeError): """Raised when a tool result is admitted to the transcript after silent truncation.""" def admit_tool_result( result_text: str, result_token_count: int, budget: int, pinned_tokens: int, retained_tokens: int, evictable_tokens: int, truncation_marker_present: bool, ) -> str: slack = budget - pinned_tokens - retained_tokens if result_token_count <= slack: return result_text deficit = result_token_count - slack if deficit > evictable_tokens: # Eviction cannot converge; the only remaining move is truncation. if not truncation_marker_present: raise TruncatedToolResultError( f"result needs {result_token_count} tokens, slack is {slack}, " f"evictable is {evictable_tokens}, deficit {deficit} cannot be closed; " f"refusing to admit silently truncated result" ) # Marker present: truncation is visible to the model, admit with marker. return result_text[: slack] + "\n[RESULT TRUNCATED: tail omitted]\n" # Eviction can close the gap; caller must evict before appending. return result_text ```