---
url: >-
  https://adk.nht.io/api/@nhtio/adk/batteries/context/compact/contracts/type-aliases/OnCostFn.md
description: >-
  Optional per-summarization cost callback — fired once per [`SummarizeFn`](https://adk.nht.io/api/@nhtio/adk/batteries/context/compact/contracts/type-aliases/SummarizeFn)
  call that actually runs, replacing the production flagship agent's
  `globalThis.__agentCompactionCost` ring buffer with an injectable, testable
  seam that leaves no global state behind.
---

# Type Alias: OnCostFn

```ts
type OnCostFn = (event: CompactionCostEvent) => void;
```

Defined in: [src/batteries/context/compact/contracts.ts:121](https://github.com/NHTIO/ADK/blob/v1.20261003.0/src/src/batteries/context/compact/contracts.ts#L121)

Optional per-summarization cost callback — fired once per [SummarizeFn](SummarizeFn.md) call that actually
runs, replacing the production flagship agent's `globalThis.__agentCompactionCost` ring buffer with
an injectable, testable seam that leaves no global state behind.

## Parameters

| Parameter | Type                                                          | Description                                                      |
| --------- | ------------------------------------------------------------- | ---------------------------------------------------------------- |
| `event`   | [`CompactionCostEvent`](../interfaces/CompactionCostEvent.md) | See [CompactionCostEvent](../interfaces/CompactionCostEvent.md). |

## Returns

`void`

## Remarks

Never invoked when the threshold hasn't been crossed (no summarization call made — see
[@nhtio/adk/batteries/context/compact/summarizer!assembleCompactedTurns](../../summarizer/functions/assembleCompactedTurns.md)'s below-threshold
pass-through path). A caller who wants a running total across a whole session's worth of dispatches
accumulates across `onCost` firings themselves; this battery holds no cross-call state of its own
beyond what a single `assembleCompactedTurns` invocation's caller threads through (the rolling prior
summary is passed in and returned explicitly, never stashed on `globalThis`).
