#9010·pi

Context compaction causes memory spikes with local LLMs due to in-process string duplication

Author: t2txCreated Sep 2, 2026Updated Sep 27, 2026
Labelslast-read

Summary

Context compaction (packages/coding-agent/src/core/compaction/) runs entirely in the main process with no subagent/worker isolation. During compaction, the conversation history is copied into memory multiple times as large strings, causing significant memory spikes. This is especially problematic when using local LLMs where the prompt stays in-process during inference.

Root Cause Analysis

1. Triple string copy in generateSummaryWithUsage() (compaction.ts:656)

The summarization prompt is built by:

  1. convertToLlm(currentMessages) — full message conversion (copy 1)
  2. serializeConversation(llmMessages) — text serialization (copy 2)
  3. Template literal concatenation <conversation>\n${conversationText}\n</conversation> (copy 3)

All three copies coexist in memory until GC collects the intermediates.

2. Unbounded toolCall argument serialization (utils.ts:109)

serializeConversation() truncates tool results to TOOL_RESULT_MAX_CHARS = 2000, but assistant-side toolCall arguments are serialized without any limit. A Write tool call with a large file content in its arguments is included verbatim via JSON.stringify(v).

3. Split-turn double prompt construction (compaction.ts:887)

When isSplitTurn === true, two full prompt constructions + LLM calls run sequentially (generateSummaryWithUsage + generateTurnPrefixSummary). If V8 hasn't GC'd the first prompt's intermediates before the second starts, peak memory doubles.

4. Redundant buildSessionContext in prepareCompaction (compaction.ts:776)

typescript
const tokensBefore = estimateContextTokens(buildSessionContext(pathEntries).messages).tokens;

This creates a full message array just for token estimation, while the caller may also build context separately.

5. Local LLM amplification

With cloud APIs, the prompt string is sent over HTTP and can be GC'd during the network wait. With local LLMs, the prompt remains in the Node.js heap during the entire inference duration, preventing collection and amplifying peak memory.

Proposed Improvements

Improvement Impact
Apply truncation to toolCall arguments in serializeConversation (e.g., cap Write content) Reduce prompt size
Avoid triple string copy — stream or reuse intermediates in generateSummaryWithUsage Reduce peak memory ~2-3x
Ensure GC opportunity between split-turn LLM calls (nullify references before second call) Halve peak for split turns
Reuse buildSessionContext result in prepareCompaction instead of calling it separately Eliminate redundant allocation
(Longer term) Run compaction in a worker thread or subprocess Isolate memory impact from main agent loop

Affected Files

  • packages/coding-agent/src/core/compaction/compaction.ts
  • packages/coding-agent/src/core/compaction/utils.ts
  • packages/agent/src/harness/compaction/compaction.ts
  • packages/agent/src/harness/compaction/utils.ts
  • packages/coding-agent/src/core/agent-session.ts

Reproduction

Use a local LLM backend (e.g., llama.cpp, Ollama) with a long session (~80k+ context tokens). Trigger compaction (manual /compact or automatic threshold). Observe Node.js RSS spike during the summarization prompt construction phase.