Skip to content
1 min read · 242 words

Function: subtractToFit()

ts
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

ParameterTypeDescription
wsWorkingSetThe working set to subtract in place. Mutated: thoughts, retrievables, memories, messages, toolCalls, image.kept, and the visible/hidden state of tools may all change.
contextWindownumberThe active model's context window.
relevantToolNamesstring[]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.
optionsSubtractToFitOptionsSee SubtractToFitOptions.

Returns

ThriftTrace

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.