---
url: >-
  https://adk.nht.io/api/@nhtio/adk/batteries/context/thrift/subtractive_pass/functions/subtractToFit.md
---

# Function: subtractToFit()

```ts
function subtractToFit(
  ws: WorkingSet,
  contextWindow: number,
  relevantToolNames: string[],
  options: SubtractToFitOptions,
): ThriftTrace;
```

Defined in: [src/batteries/context/thrift/subtractive\_pass.ts:410](https://github.com/NHTIO/ADK/blob/v1.20260713.1/src/src/batteries/context/thrift/subtractive_pass.ts#L410)

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`](../interfaces/WorkingSet.md)                     | 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`](../interfaces/SubtractToFitOptions.md) | See [SubtractToFitOptions](../interfaces/SubtractToFitOptions.md).                                                                                                                                                               |

## Returns

[`ThriftTrace`](../interfaces/ThriftTrace.md)

## 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](../../../exceptions/variables/E_CONTEXT_RESOLVER_MISSING.md)
immediately (naming the option) rather than silently guessing at token counts.

## Throws

[@nhtio/adk/batteries/context/exceptions!E\_CONTEXT\_RESOLVER\_MISSING](../../../exceptions/variables/E_CONTEXT_RESOLVER_MISSING.md) When
`options.estimateTokens` is not a function.
