feat(core): tool output masking service for context reclamation - #2573
Closed
netbrah wants to merge 2 commits into
Closed
feat(core): tool output masking service for context reclamation#2573netbrah wants to merge 2 commits into
netbrah wants to merge 2 commits into
Conversation
netbrah
requested review from
DennisYu07,
DragonnZhang,
LaZzyMan,
Mingholy,
gwinthis,
pomelo-nwu and
tanzhenxin
as code owners
March 21, 2026 13:32
Add ToolOutputMaskingService that runs before each turn to replace older bulky tool outputs with preview snippets. Full output is preserved on disk. Protects newest outputs within a configurable token budget and optionally shields the latest turn. Includes synchronous token estimation utilities, telemetry for tracking savings, and configurable thresholds under experimental.toolOutputMasking. Fixes #2567 Made-with: Cursor
netbrah
force-pushed
the
feat/tool-output-masking
branch
from
March 21, 2026 13:42
551bb30 to
48dbc7c
Compare
… tests Made-with: Cursor
netbrah
force-pushed
the
feat/tool-output-masking
branch
from
March 22, 2026 04:39
af752f3 to
7c216f9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
ToolOutputMaskingService— a pre-turn pass that replaces older bulky tool outputs with preview snippets while preserving full output on disk. Includes synchronous token estimation and telemetry.Why
In long agentic sessions, old tool outputs are the largest consumers of context budget. The model doesn't need the full content of a file read from 20 turns ago — but it still occupies thousands of tokens. Chat compression helps but requires an LLM call. Tool output masking is zero-cost context reclamation that runs synchronously before each turn.
The approach: protect the newest tool outputs (configurable budget), mask everything older with a preview snippet, save full output to disk (recoverable). Exempt tools that carry semantic state (skill, memory, ask_user_question) are never masked.
Architecture
Supporting components
tokenCalculation.ts— Synchronous token estimation with ASCII/non-ASCII heuristic, media estimates, function response handling. Fast enough to run every turn.ToolOutputMaskingEvent— Telemetry event for monitoring savings across sessions.Configuration
Under
experimental.toolOutputMasking:enabledtruetoolProtectionThreshold50000minPrunableTokensThreshold30000protectLatestTurntrueTest Plan
24 tests across 2 files:
tokenCalculation.test.ts (8 tests):
toolOutputMaskingService.test.ts (8 tests):
Plus existing
client.test.tsandmcp-client.test.tsmock updates for config compatibility.Demo
N/A — internal context management. Observable via telemetry events showing token savings and reduced compression frequency in long sessions.
Fixes #2567