You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Pre-shrunk compaction request defeats server-side prefix caching by construction #4204
When the head is trimmed, the preserved tail shifts right up against system+tools, so a prefix cache (vLLM APC and similar, hash-chained from the start of the request) cannot match mid-session content. Even with droppedCount > 0, of content that was 100% recently cached, only the static prefix matches. On large-window models every auto-compaction — successful ones included — is a full cold re-prefill of hundreds of thousands of tokens.
Additional observation (same component): the preserved recent tail is never summarized — DEFAULT_COMPACTION_CONFIG (maxRecentMessages = 4, maxRecentSizeRatio = 0.2) is not exposed in config.toml, and completed compactions leave 85–93K behind from a 109–290K before (ratios 0.30–0.79); on 128K-context models we measured after/before = 1.017 — the "compacted" context is larger than the original.
Facts
cached_tokens = 67,584 (exactly system+tools) on every compaction call at ~165K input.
Independent reproduction on the managed Kimi endpoint (1M window, v2.3.2)
Adding a data point to the report above: same mechanism, different provider and window size. This was an automatic compaction (source: auto) and it completed successfully — no overflow, no retry.
Model kimi-code/k3.1-pro, max_context_size = 1,048,576, thinking_effort = max
Trigger: 0.85 × 1,048,576 = 891K (measured 891,073 before compaction)
systemPromptHash, toolsHash and the cache key (sessionId) are identical across both calls
The 24,832 cached tokens are exactly system+tools. The first retained message differs from the cached prefix, so everything after it is a cold prefill (~773K tokens). In the managed usage view this single call was ~95× a normal turn (0.95% vs 0.01% of the plan quota); ordinary turns are 99.5% cache-hit and effectively free.
This is not occasional at the default trigger
Let r = trigger ratio (0.85), W = window, R = output reserve (min(maxOutputSize, W/8) = 128K here), O = system+tools overhead.
Trigger: T = r·W
Pre-shrink budget: B = r·(W − R) − O
B − T = −(r·R + O) < 0 for any R > 0
Under the same token accounting, any automatic compaction that fires at the trigger starts with a history large enough to require trimming, by at least r·R (~111K tokens here). The “send the full history when it already fits” path covered by fullCompaction.test.ts is therefore structurally unreachable on the automatic path. The observed 9,246-token summary also uses only ~7% of the 128K reserve — the reserve itself is what forces the head trim.
A cache-preserving path already exists in the code
resolveRequest computes usedContextTokens only when overrides.messages is undefined; the compaction path supplies overrides.messages, so usedContextTokens stays undefined and the fixed 128K reserve is used instead. The provider adapters already clamp the output to maxContextTokens − usedContextTokens (kosong/provider.ts, kimi.ts). Passing the measured request size for the compaction call and skipping the pre-shrink when the head fits would make the request a pure suffix append (full prefix reuse) while still bounding the output by the remaining window.
Note that lowering compaction_trigger_ratio is not a workaround — see #4202 (when trigger < blockRatio, background compaction races the loop and completed summaries are discarded).
Could a maintainer confirm whether the always-pre-shrink at defaults is intended? If it is, the docs should state that the oldest ~r·R tokens are omitted from the summary and that the compaction call is a cold prefill; if not, the dynamic clamp above looks like a small fix.
I ran into this on the managed endpoint with a 1M window and have a fix on a branch. It keeps the full history when the request still fits (using the measured context size, with a 36K output reserve), clamps the output budget to the effective window, and keeps the old pre-shrink for <=256K windows. There are regression tests for the large-window path and a changeset.
Can a maintainer /approve this issue so I can open a PR?
Activity
luoshengming commentedon Oct 25, 2026
Independent reproduction on the managed Kimi endpoint (1M window, v2.3.2)
Adding a data point to the report above: same mechanism, different provider and window size. This was an automatic compaction (
source: auto) and it completed successfully — no overflow, no retry.kimi-code/k3.1-pro,max_context_size = 1,048,576,thinking_effort = max0.85 × 1,048,576 = 891K(measured 891,073 before compaction)messageCount: 872,droppedCount: 28,maxTokens: 131,072systemPromptHash,toolsHashand the cache key (sessionId) are identical across both callsThe 24,832 cached tokens are exactly system+tools. The first retained message differs from the cached prefix, so everything after it is a cold prefill (~773K tokens). In the managed usage view this single call was ~95× a normal turn (0.95% vs 0.01% of the plan quota); ordinary turns are 99.5% cache-hit and effectively free.
This is not occasional at the default trigger
Let
r= trigger ratio (0.85),W= window,R= output reserve (min(maxOutputSize, W/8)= 128K here),O= system+tools overhead.T = r·WB = r·(W − R) − OB − T = −(r·R + O) < 0for anyR > 0Under the same token accounting, any automatic compaction that fires at the trigger starts with a history large enough to require trimming, by at least
r·R(~111K tokens here). The “send the full history when it already fits” path covered byfullCompaction.test.tsis therefore structurally unreachable on the automatic path. The observed 9,246-token summary also uses only ~7% of the 128K reserve — the reserve itself is what forces the head trim.A cache-preserving path already exists in the code
resolveRequestcomputesusedContextTokensonly whenoverrides.messagesis undefined; the compaction path suppliesoverrides.messages, sousedContextTokensstays undefined and the fixed 128K reserve is used instead. The provider adapters already clamp the output tomaxContextTokens − usedContextTokens(kosong/provider.ts,kimi.ts). Passing the measured request size for the compaction call and skipping the pre-shrink when the head fits would make the request a pure suffix append (full prefix reuse) while still bounding the output by the remaining window.Note that lowering
compaction_trigger_ratiois not a workaround — see #4202 (whentrigger < blockRatio, background compaction races the loop and completed summaries are discarded).Could a maintainer confirm whether the always-pre-shrink at defaults is intended? If it is, the docs should state that the oldest
~r·Rtokens are omitted from the summary and that the compaction call is a cold prefill; if not, the dynamic clamp above looks like a small fix.Related: #3357, #4205, #4389.
luoshengming commentedon Oct 25, 2026
I ran into this on the managed endpoint with a 1M window and have a fix on a branch. It keeps the full history when the request still fits (using the measured context size, with a 36K output reserve), clamps the output budget to the effective window, and keeps the old pre-shrink for <=256K windows. There are regression tests for the large-window path and a changeset.
Can a maintainer /approve this issue so I can open a PR?