Function: subtractToFit()
function subtractToFit(
ws: WorkingSet,
contextWindow: number,
relevantToolNames: string[],
options: SubtractToFitOptions,
): ThriftTrace;Defined in: src/batteries/context/thrift/subtractive_pass.ts:410
The subtractive pass. Start wide; measure every bucket; then shed lowest-signal first until the dispatch fits the active window's budget — the image (the single biggest hog) goes first when it doesn't fit, then tools the turn doesn't need, the tail of the retrieval ranking, low-value memories, the oldest conversation turns. Each cut is by EVIDENCE (a measured bucket), never a guess. If even the floor (system prompt + newest turn) won't fit, the dispatch REFUSES (fits: false) — a bounded refusal beats a truncated, incoherent dispatch.
Parameters
| Parameter | Type | Description |
|---|---|---|
ws | WorkingSet | The working set to subtract in place. Mutated: thoughts, retrievables, memories, messages, toolCalls, image.kept, and the visible/hidden state of tools may all change. |
contextWindow | number | The active model's context window. |
relevantToolNames | string[] | The names (from ws.tools.all()) that should start VISIBLE for this turn — every other registered tool starts hidden (0 schema tokens, still callable via a catalog). The last-resort shed (step 9) may hide some of these too. |
options | SubtractToFitOptions | See SubtractToFitOptions. |
Returns
Remarks
options.estimateTokens is REQUIRED — this battery ships with no bundled tokenizer, so a missing estimator throws @nhtio/adk/batteries/context/exceptions!E_CONTEXT_RESOLVER_MISSING immediately (naming the option) rather than silently guessing at token counts.
Throws
@nhtio/adk/batteries/context/exceptions!E_CONTEXT_RESOLVER_MISSING When options.estimateTokens is not a function.