Context Compaction
Automatic, event-emitting context compaction for long sessions. Default import enables it; /strict opts in explicitly. Threshold and overflow modes.
When a session's message history grows past the model's context window, an agent will start failing or thrashing. TechFabric Harness solves this with automatic compaction. Older session entries are summarized into a single compact entry, recent entries are kept verbatim, and the agent keeps running.
Defaults
| SDK | Default | Why |
|---|---|---|
Default import (@fabric-harness/sdk) | enabled: true | Headless agents are often webhook/serverless handlers. Failing at the context window is the wrong default. |
Strict import (@fabric-harness/sdk/strict) | not injected, undefined until you set it | Required for Temporal replay determinism: auto-compaction is non-deterministic across replays. Opt in only when you don't need replay safety. |
⚠️ Selecting
runtime: 'temporal'from the default import emits a one-timeconsole.warnbecause of this exact reason. Either switch to/strictor passcompaction: { enabled: false }explicitly.
Trigger modes
- Threshold, before each model call, the harness estimates token usage from session history. When estimated tokens exceed
compactAtTokens(auto-derived ascontextWindowTokens - reserveTokensif not set), it compacts before sending the next prompt. - Overflow, if the model returns a
context overflowerror anyway, the harness compacts and retries the prompt once. SetrecoverFromOverflow: falseto disable. Only a genuine provider overflow triggers this path: cancellation and harness errors whose message merely mentions context or tokens (tool, policy, validation, cost, or turn-budget failures) surface unchanged. Completed responses stay in the durable history, and the retry always ends with the pending user prompt, even when compaction summarized the entry that carried it. - After a terminating tool, when a prompt ends because a tool returned
terminate: true, the harness applies the threshold check immediately so the next turn starts from a manageable context. A failure here is reported as anerrorevent and retried before the next model call.
Compaction that runs inside a prompt uses the prompt's signal and modelTimeoutMs for its summary call. The deadline covers the whole summary, and it defaults to 120 seconds when modelTimeoutMs is unset. A summary that misses the deadline falls back to the deterministic summary. An abort during threshold or overflow compaction rejects the prompt with AbortError and records no compaction. An abort during compaction after a terminating tool skips the compaction, and the prompt still resolves with its result because that result was already final.
Both paths emit a typed compaction event with reason: 'threshold' | 'overflow', messagesBefore, messagesAfter, and tokensBefore.
Configuration
import { defineAgent } from '@fabric-harness/sdk';
export default defineAgent<{ message: string }>({
name: 'long-running',
run: async ({ init, input }) => {
// Compaction is on by default. Override only if you need to.
const session = await (await init({
compaction: {
enabled: true,
reserveTokens: 8192, // default 20000
keepRecentEntries: 30, // default 20
// compactAtTokens auto-derived from contextWindow if not set
},
})).session();
return { reply: await session.prompt<string>(input.message) };
},
});To disable: init({ compaction: { enabled: false } }).
import { defineAgent, schema } from '@fabric-harness/sdk/strict';
export default defineAgent({
name: 'long-running',
input: schema.object({ message: schema.string() }),
run: async ({ init, input }) => {
// Off by default in /strict — opt in explicitly. Avoid for Temporal.
const session = await (await init({
runtime: 'inline',
sandbox: 'local',
compaction: {
enabled: true,
reserveTokens: 16_384,
keepRecentEntries: 30,
recoverFromOverflow: true,
},
})).session();
return await session.prompt(input.message);
},
});CompactionSettings reference
| Field | Type | Default | Purpose |
|---|---|---|---|
enabled | boolean | Default import: true; /strict: not injected | Master switch. |
compactAtTokens | number | derived from contextWindowTokens - reserveTokens | Trigger threshold. |
reserveTokens | number | 20000 | Headroom kept below the model's context window. |
keepRecentTokens | number | 8000 | Tokens of recent context kept verbatim during compaction. Older entries get folded into the summary. |
keepRecentEntries | number | 20 | Recent entries kept verbatim during compaction. Older entries get folded into the summary. The cut moves back to the nearest tool-batch boundary, so it can keep a few more entries than requested. |
recoverFromOverflow | boolean | true | Catch provider context-overflow errors, compact, and retry once. |
All fields can also be set per-prompt via session.prompt(text, { compactAtTokens, reserveTokens, compactionKeepRecentEntries, recoverContextOverflow }). Per-prompt values win.
Subscribing to compaction events
import { init, isEvent, type AgentEvent } from '@fabric-harness/sdk';
const fabric = await init({
onEvent: (event: AgentEvent) => {
if (isEvent(event, 'compaction')) {
console.log(
`compacted ${event.data.messagesBefore} → ${event.data.messagesAfter}`,
`(reason: ${event.data.reason}, ${event.data.tokensBefore} tokens)`,
);
}
},
});See the Events reference for the full event taxonomy.
Manual compaction
You can also force compaction outside the auto-loop.
const result = await session.compact({
keepRecentEntries: 10,
generateSummary: true,
reason: 'manual',
});
console.log(result.summary, result.compactedEntries);How the summary is generated
The summary is produced by the configured model with a deterministic fallback when no model is available. Compaction never separates a tool call from its result. Token-based and count-based cuts both land on a tool-batch boundary, so a kept result always keeps its call and later prompts replay a transcript strict providers accept. For keepRecentEntries, the cut moves back to the nearest boundary (or forward when no earlier boundary exists). References to files read or modified are appended as a separate block so the agent can re-discover them after compaction. Tool calls that never ran (truncated by the output token limit, or skipped because the prompt was aborted) are left out of those references.
The summary entry replaces the compacted slice in the session history; subsequent prompts see only the summary plus the recent entries you chose to keep.
When not to enable compaction
- Short prompts that always fit (one-shot classifiers).
- Agents that need an exact, replayable conversation history (legal/audit). Use Temporal-backed durability instead.
- Tasks where summary loss could change the outcome (e.g., the agent must remember a specific identifier mentioned 50 turns ago). In that case, persist key facts as artifacts and reference them by name.
See also
- Events: typed
compactionevent payload - Sessions and prompts: where
prompt()evaluates the budget - Telemetry: exporting compaction metrics
Streaming protocol
The HTTP wire protocol for delivering messages to an agent conversation and reading it back by offset, over JSON or SSE.
Connector Recipes (`fh add`)
Pipe Markdown connector recipes into your coding agent and have it scaffold a TechFabric Harness sandbox, MCP or data adapter into your project.