← Back to PRs

#10465: Context pruning: strip image blocks instead of skipping

by quentintou open 2026-02-06 14:51 View on GitHub →
agents stale
## Summary The context pruner currently **skips** tool results that contain image blocks (`pruner.ts` lines 160-161 and 228-230). This means base64-encoded images accumulate indefinitely in the session context window, never getting pruned. In production with multi-agent setups receiving Telegram images, this causes `context overflow: prompt too large` errors. This PR changes the pruner to **strip image blocks** from prunable tool results, replacing them with a short text note (e.g. `[1 image pruned from context]`), then applies normal soft-trim/hard-clear to the remaining text content. ### Changes **`pruner.ts`:** - Add `stripImageBlocks()` helper that replaces image blocks with a text placeholder - In `softTrimToolResultMessage`: strip images first, then apply normal text trimming (instead of returning `null`) - In `pruneContextMessages` main loop: remove the `hasImageBlocks` skip guard so image-containing tool results are included in the prunable set - Images in the protected tail (last N assistant turns) are still preserved **`context-pruning.test.ts`:** - Update existing "skips tool results that contain images" test → now verifies images ARE stripped and text is pruned - Add new test: "strips images but preserves short text" — verifies image blocks are removed while short text content is kept intact ### Context - Base64 images in tool results are typically 150KB+ each (after JPEG optimization) - The `IMAGE_CHAR_ESTIMATE` (8000 chars) was already counting images toward context usage, but the pruner was never acting on them - In a real production case: 94 images / 20MB of base64 accumulated across 12 sessions, causing repeated context overflow errors - The Anthropic API requires inline base64 (no file references), so images must be stored this way — the pruner is the right place to reclaim this space ## Test plan - [ ] Existing context-pruning tests pass (updated test verifies new behavior) - [ ] New test verifies image stripping with short text preservation - [ ] Images in protected tail (last N assistants) are not affected - [ ] Tool results without images continue to be pruned as before 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> - Updates context pruner to handle tool results containing inline image blocks by stripping image blocks and then applying existing soft-trim/hard-clear logic. - Removes the previous guard that skipped pruning image-containing tool results, so they now participate in pruning calculations. - Adjusts and extends unit tests to validate image stripping, hard-clear behavior, and preservation of short tool-result text. <h3>Confidence Score: 4/5</h3> - Mostly safe to merge, but there is one behavioral inconsistency around preserving the image-pruned placeholder after soft-trim. - Core change (strip image blocks rather than skipping) is straightforward and covered by updated tests, but the current soft-trim path drops the image-pruned note for long tool results, which is likely unintended given the PR’s stated goal of leaving a placeholder. - src/agents/pi-extensions/context-pruning/pruner.ts <!-- greptile_other_comments_section --> <sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub> <!-- /greptile_comment -->

Most Similar PRs