Implement durable compaction
Keep full session history while sending compacted context to models.
Durable compaction lets long-lived agents keep their complete raw transcript for UI, audit, and export while sending a smaller projected context to the model.
Use this guide when your app stores agent conversations in a database, append log, Durable Object, or workflow-owned state.
What you will build
You will build a host-owned compaction layer with this shape:
raw history = append-only messages and compaction rows
model context = latest checkpoint message + messages after it
runtime writes = newly created messages only
summary storage = durable compaction payload, not a synthetic transcript messageYolk provides protocol helpers, planning helpers, checkpoint formatting, transcript formatting, and one-shot context-overflow retry. Your app owns storage, summary generation, thresholds, auth, and active-run guards.
Prerequisites
- A server-owned transcript or session log.
- A model provider layer for normal model calls.
- A transaction or compare-and-set guard for appending messages.
- A place to store compaction payloads beside messages.
Install
pnpm add @yolk-sdk/agent@canary effect@4.2.0Design the ledger
Store raw protocol messages and compaction payloads as first-class rows. Do not overwrite older messages with the summary.
This is a host-owned shape, not an SDK schema:
import type { AgentMessage } from '@yolk-sdk/agent/protocol'
type HostConversationEntry =
| {
readonly kind: 'message'
readonly sessionId: string
readonly revision: number
readonly seq: number
readonly message: AgentMessage
readonly runId?: string
}
| {
readonly kind: 'compaction'
readonly sessionId: string
readonly revision: number
readonly seq: number
readonly compaction: HostCompactionPayload
readonly runId?: string
}
type HostCompactionPayload =
| {
readonly _tag: 'Completed'
readonly reason: 'auto' | 'manual' | 'overflow'
readonly summary: string
readonly recent: string
readonly compactedThroughSeq: number
readonly createdAtMs: number
readonly tokensBefore?: number
readonly tokensAfter?: number
}
| {
readonly _tag: 'Failed'
readonly reason: 'auto' | 'manual' | 'overflow'
readonly message: string
readonly createdAtMs: number
}Production stores usually also keep a small session state row with the active revision, next sequence number, usage, active run id, and HITL state.
Project messages for the model
Before any completed compaction exists, send the full active transcript. After compaction, send one
synthetic checkpoint message plus messages appended after the compaction row. The projection below
expects rows already filtered to one session's active revision and sorted by ascending seq.
This fragment shows the projection:
import { makeCompactionCheckpointMessage } from '@yolk-sdk/agent/compaction'
import type { AgentMessage } from '@yolk-sdk/agent/protocol'
const projectMessagesForModel = (
rows: ReadonlyArray<HostConversationEntry>
): ReadonlyArray<AgentMessage> => {
const latestCompaction = rows
.filter(row => row.kind === 'compaction' && row.compaction._tag === 'Completed')
.at(-1)
const messages = rows.filter(row => row.kind === 'message')
if (latestCompaction === undefined) {
return messages.map(row => row.message)
}
const checkpoint = makeCompactionCheckpointMessage({
summary: latestCompaction.compaction.summary,
recent: latestCompaction.compaction.recent,
createdAtMs: latestCompaction.compaction.createdAtMs
})
const laterMessages = messages
.filter(row => row.seq > latestCompaction.seq)
.map(row => row.message)
return [checkpoint, ...laterMessages]
}Do not persist the checkpoint returned by makeCompactionCheckpointMessage as a raw message. It is a
model-input projection only.
If you use host instructions (for example the code
mode catalog), keep the instruction messages the checkpoint folds away: put
latestInstructionMessages(compacted) from @yolk-sdk/agent/protocol after the checkpoint, minus
keys that laterMessages already carry. The loop compares instruction state with the full
transcript and never re-appends them.
Plan when to compact
Use the same token estimate you use for model-context warnings. planWindowCompaction keeps a recent
tail and avoids splitting tool results away from recent assistant tool calls.
This fragment plans an automatic compaction:
import {
contextBudgetStatus,
estimateAgentMessagesTokens,
makeContextBudget,
planWindowCompaction
} from '@yolk-sdk/agent/compaction'
import type { AgentMessage } from '@yolk-sdk/agent/protocol'
const budget = makeContextBudget({
contextWindowTokens: 200_000,
reservedOutputTokens: 8_000,
warningRatio: 0.75,
compactionRatio: 0.9
})
declare const hostCountTextTokens: (text: string) => number
const estimateTokens = (messages: ReadonlyArray<AgentMessage>) =>
estimateAgentMessagesTokens(messages, {
countTextTokens: hostCountTextTokens
})
const planCompaction = (messages: ReadonlyArray<AgentMessage>) => {
const inputTokens = estimateTokens(messages)
if (contextBudgetStatus(inputTokens, budget) !== 'compact') {
return { _tag: 'Skip' as const }
}
return planWindowCompaction(messages, {
thresholdTokens: budget.compactionInputTokens,
estimateTokens
})
}The planner does not call a model. It only chooses which messages to summarize and which messages to
keep as recent context. Without countTextTokens, Yolk uses a provider-neutral character heuristic
plus fixed message and media overhead. A synchronous host tokenizer improves selected message text,
reasoning, and host tool-call estimates only. Exact provider-request accounting must also include the
system prompt, tool definitions, vendor framing, and a safety margin. Reuse the same
estimateTokens function for warnings, planning, transformers, and before/after checks.
Generate and store a summary
Generate the summary with your app's provider configuration, then persist it as a compaction row.
Use formatAgentMessagesForCompaction to serialize messages into summary input. It includes user
text, assistant text/reasoning, host and provider tool calls/results, and truncated tool parameters
and results. This format is intentionally lossy: it omits message envelope metadata, structured tool
result content, and error flags. Supply a host formatter when those fields matter to the summary.
This fragment shows the summary boundary:
import {
compactionSummarySourceMessages,
formatAgentMessagesForCompaction,
makeCompactionCheckpointMessage
} from '@yolk-sdk/agent/compaction'
const summarizeAndPersist = async (input: {
readonly plan: Extract<ReturnType<typeof planCompaction>, { readonly _tag: 'Compact' }>
readonly previousCompaction?: {
readonly summary: string
readonly recent: string
}
readonly expectedSeq: number
}) => {
const messagesForSummary = compactionSummarySourceMessages({
hasPreviousSummary: input.previousCompaction !== undefined,
messages: input.plan.compactedMessages
})
const formattedHistory = formatAgentMessagesForCompaction(messagesForSummary)
const summary = await hostSummarizeCompaction({
previousSummary: input.previousCompaction?.summary,
previousRecent: input.previousCompaction?.recent,
history: formattedHistory
})
const recent = formatAgentMessagesForCompaction(input.plan.recentMessages)
const createdAtMs = Date.now()
const payload = {
_tag: 'Completed' as const,
reason: 'auto' as const,
summary,
recent,
compactedThroughSeq: input.expectedSeq,
createdAtMs,
tokensBefore: input.plan.beforeTokens,
tokensAfter: estimateTokens([makeCompactionCheckpointMessage({ summary, recent, createdAtMs })])
}
if (payload.tokensAfter >= input.plan.beforeTokens) {
return { _tag: 'Skipped' as const }
}
await hostAppendCompaction({
expectedSeq: input.expectedSeq,
payload
})
return { _tag: 'Compacted' as const, payload }
}hostSummarizeCompaction and hostAppendCompaction are app-owned. Your storage append should verify
the active run id and expected sequence so a stale run cannot compact or append to a newer transcript.
When a previous compaction exists, the summarizer must fold both its summary and recent text into
the new summary before the leading synthetic checkpoint is dropped.
Retry provider context overflow once
Providers can still reject a request when their exact token accounting differs from your estimate.
Wrap the provider with makeContextOverflowRetryProvider to compact and retry once when the provider
raises LLMError with cause: 'context_overflow'.
This fragment wires the retry wrapper:
import { Effect, Ref } from 'effect'
import { makeContextOverflowRetryProvider } from '@yolk-sdk/agent/compaction'
const messagesRef = await Effect.runPromise(Ref.make(projectedMessages))
const retryProvider = await Effect.runPromise(
makeContextOverflowRetryProvider({
provider: appProvider,
messagesRef,
compact: messages =>
maybeCompactAndProject({
reason: 'overflow',
messages
})
})
)The compactor returns either:
type ContextOverflowRetryCompactionResult =
| { readonly _tag: 'Compacted'; readonly messages: ReadonlyArray<AgentMessage> }
| { readonly _tag: 'Skipped'; readonly messages: ReadonlyArray<AgentMessage> }If compaction succeeds, Yolk retries the provider call once with the compacted messages. If compaction
is skipped or the compactor fails, the original overflow fails normally. A retry-provider failure,
including a second overflow, propagates from the retry stream.
The retry budget resets for each provider.stream(...) call and only intercepts tagged LLMError
values with cause: 'context_overflow'. messagesRef is an optional host side channel; the wrapper
updates it after successful compaction but does not persist or replace runtime transcript state.
Use this wrapper only when the provider reports context overflow before emitting deltas. It does not retract already-emitted text/usage or emit compaction lifecycle events before starting the retry.
For private media, put host authorization/signing inside this retry wrapper. The compactor then works on refs, and each provider attempt resolves fresh URLs from its effective context. See Resolve private attachments for the tested composition and limits.
Persist only new messages
When a model or tool step finishes, append only messages created by that step:
- user input rows from user actions
- assistant message rows from model steps
- tool-result rows from tool steps
- partial created messages from failure paths
Never persist projectMessagesForModel(...) back into the raw ledger. That projection may contain a
synthetic checkpoint that is not part of the user-visible transcript.
Handle edits and deletes
For destructive transcript mutations, create a new revision rather than rewriting old rows. The new revision can contain the edited transcript and no compaction rows. Old revisions remain available for audit or debugging.
This rule keeps compaction simple: the active revision has one append cursor, and the latest completed compaction in that revision defines model context.
Verify the integration
- Set a low test threshold and append enough messages to trigger compaction.
- Confirm the raw transcript remains unchanged and a completed compaction payload is appended.
- Confirm the next provider request starts with one synthetic checkpoint plus later messages.
- Script one pre-emission
context_overflowand confirm the host compactor runs once. - Script a second overflow and confirm it terminates without another retry.
The expected result is a complete durable transcript, bounded provider input, and at most one overflow compaction retry per provider stream.
Production checklist
- Keep raw messages append-only.
- Store completed and failed compactions as ledger entries, not raw messages.
- Project checkpoints only at model/provider boundaries.
- Keep the latest instruction message of each key after the checkpoint when you use host instructions.
- Guard compaction writes with active run id and expected sequence.
- Ignore failed compaction rows during projection.
- Merge previous compactions by passing both
previousSummaryandpreviousRecent, then drop the leading checkpoint from the new summary input. - Compact before model calls and once on provider
context_overflow. - Surface compaction markers in your UI, but keep ledger rows as source of truth.
Related APIs
See Agent APIs for the public compaction surface.
planWindowCompactionmakeCompactionCheckpointMessageformatAgentMessagesForCompactioncompactionSummarySourceMessagesmakeContextOverflowRetryProviderestimateAgentMessagesTokens