Skip to content

Pre-shrunk compaction request defeats server-side prefix caching by construction #4204

Open

Description

@belenov-maker

Environment

  • kimi-code (Node) v2.1.1, macOS arm64
  • Main model: GLM-5.3, 256K context
  • Serving: vLLM-style endpoint with automatic prefix caching (APC)

Component

agent-core-v2/src/agent/fullCompaction/fullCompactionService.ts → preShrinkHistoryToWindowBudget + takeRecentMessagesWithinTokenBudget.

Mechanism

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.
  • 31 "successful" compactions × ~80K cold tokens each.

Expected

A cache-friendly request shape (keep a stable head, cut the middle) or explicit protocol-level prefix re-binding; at minimum, document the cost.

Reproduction

  1. Large-window model behind a vLLM-style endpoint with APC; a session grown to ~165K.
  2. Trigger an auto-compaction.
  3. Inspect cached_tokens in the provider response for the compaction call — it stays at exactly system+tools (67,584) while the input is ~165K.

Related issues:

Activity

  1. luoshengming commented on Oct 25, 2026

    @luoshengming

    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)
    • Preceding loop call: 890,760 input / 886,528 cache-read (99.5%)
    • Compaction call: 798,329 input / 24,832 cache-read (3.1%), 9,246 output
    • messageCount: 872, droppedCount: 28, maxTokens: 131,072
    • 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.

    Related: #3357, #4205, #4389.

  2. luoshengming commented on Oct 25, 2026

    @luoshengming

    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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions