Skip to content

Compaction ​

Compaction creates a smaller semantic thread from an existing conversation. One specification states:

text
point         where compaction stops
preservation  what history immediately before that point remains verbatim
strategy      whether selected history is removed or summarized

Automatic, explicit, and fork compaction use the same IThreadCompactionEngine. CompactionMiddleware adapts that engine to iteration and fork lifecycle hooks; the agentic loop does not contain compaction-specific execution logic.

Configure Compaction ​

Configure the default automatic and fork policies on the agent's definition:

csharp
var assistant = new AgentConfig().WithCompaction(config =>
{
    config.Automatic = new AutomaticCompactionPolicy
    {
        Trigger = new ContextPercentageCompactionTrigger(
            TotalInputTokens: 120_000,
            Percentage: 0.70),
        Compaction = new CompactionSpecification
        {
            Point = new CompactAtCurrentHead(),
            Preservation = new PreservePreviousTurns(5),
            Strategy = new SummarizingCompaction()
        }
    };

    config.ForkCompaction = new CompactionSpecification
    {
        Point = new CompactAtCurrentHead(),
        Preservation = new PreservePreviousTurns(3),
        Strategy = new SummarizingCompaction()
    };
});

CompactionConfig.Automatic = null disables automatic compaction while retaining explicit and fork compaction. A non-null AgentRunConfig.Compaction.Automatic replaces the configured automatic policy for one run; otherwise the configured policy is used.

All compaction configuration records are explicitly polymorphic through $type discriminators and are registered with the framework JSON contexts for source-generated, Native AOT-friendly serialization.

Automatic Triggers ​

Automatic triggers are evaluated in BeforeIterationAsync against the messages that are about to reach the provider:

TriggerFires when
TurnCountCompactionTrigger(Turns)The current model input contains at least that many distinct turns.
InputTokenCompactionTrigger(InputTokens)Estimated input tokens reach the absolute threshold.
ContextPercentageCompactionTrigger(TotalInputTokens, Percentage)Estimated input tokens reach the stated fraction of the explicit total input-token capacity.

A percentage always includes its denominator. For example, 70 percent of a model with a 120,000-token input limit is:

csharp
new ContextPercentageCompactionTrigger(120_000, 0.70)

Do not substitute a generic context-window number when the provider exposes a smaller input-token limit.

Compaction Point ​

The point is the exclusive end of the history being considered for compaction:

PointMeaning
CompactAtCurrentHeadCompact history before the current thread head.
CompactAtMessage(MessageId)Compact history before the selected message.
CompactAtTurn(TurnId)Compact history before the selected turn.

The engine expands a message point to the start of its turn when turn metadata is available, so compaction does not split that turn accidentally.

Preservation ​

Preservation applies only to history before the compaction point:

PreservationResult
PreserveNoPreviousHistorySelect all history before the point.
PreservePreviousTurns(Count)Keep the most recent complete turns before the point verbatim.
PreservePreviousUserMessages(Limit)Carry selected prior user messages forward without retaining all intervening assistant activity.

PreservePreviousUserMessages accepts either:

csharp
new PreviousItemCountLimit(3)
new PreviousTokenBudgetLimit(2_000)

The carried user messages receive new message identities while recording their source identities in the checkpoint. This supports a summary-plus-user-prompts shape without pretending that tool calls or assistant messages were preserved.

Preserving turns keeps messages with the same hpd.messageTurnId together. Messages without that turn metadata are treated as individual one-message turns.

Strategies ​

The strategy determines what replaces selected history:

Remove selected history ​

csharp
Strategy = new RemovalCompaction()

The selected history contributes no replacement message.

Summarize selected history ​

csharp
Strategy = new SummarizingCompaction
{
    Summarizer = new ChatClientConfig
    {
        Provider = new HPD.Agent.Providers.ProviderReference { Key = "openai" },
        ModelName = "gpt-4.1-mini"
    },
    Instructions = "Preserve decisions, file paths, failures, and exact next actions."
}

When Summarizer is absent, compaction inherits the invocation's effective chat client. The built-in instructions produce a continuation handoff rather than a conversational answer.

Checkpoints ​

Every compaction appends a ThreadHistoryCompactionCheckpointEvent to the canonical journal:

text
thread journal:
  original events
  THREAD_HISTORY_COMPACTION_CHECKPOINT
  future events

The journal is append-only, so the canonical source events remain readable. Model projection applies the checkpoint and uses the compacted semantic result. Transcript products may retain, collapse, or hide the compacted range according to their display policy.

Explicit Compaction ​

Explicit compaction is a dedicated input operation. It creates no user message and does not enter a model turn:

csharp
await runtime.RunAsync(new CompactThreadInputEvent
{
    SessionId = sessionId,
    ThreadId = threadId,
    Request = new ThreadCompactionRequest
    {
        Compaction = new CompactionSpecification
        {
            Point = new CompactAtMessage(selectedMessageId),
            Preservation = new PreservePreviousTurns(2),
            Strategy = new SummarizingCompaction()
        },
        Continuation = CompactionContinuation.StopAfterCompaction
    }
});

Continue allows execution to proceed after compaction. StopAfterCompaction completes the compaction operation without making a provider request.

Hosted clients submit the same COMPACT_THREAD_INPUT envelope through the normal input endpoint.

Fork Compaction ​

Fork compaction shapes only the new target thread. The source thread remains unchanged.

The fork option is an explicit union:

csharp
new InheritThreadForkCompaction()
new DisableThreadForkCompaction()
new ApplyThreadForkCompaction(specification)

Example:

csharp
await runtime.ForkThreadAsync(
    sessionId,
    sourceThreadId,
    newThreadId,
    fromMessageId,
    new ThreadForkOptions
    {
        Compaction = new ApplyThreadForkCompaction(
            new CompactionSpecification
            {
                Point = new CompactAtCurrentHead(),
                Preservation = new PreservePreviousTurns(3),
                Strategy = new SummarizingCompaction()
            })
    },
    cancellationToken);

Inherit uses the CompactionConfig.ForkCompaction of the agent ThreadForkOptions.AgentId names, or of the default agent. Disabled skips it. Enabled supplies the exact specification. Subagents that fork from a parent thread use this same contract; there is no separate subagent compaction system.

Events ​

Compaction publishes two event categories:

EventPurpose
CompactionEventCanonical lifecycle facts: Started, Skipped, Failed, and Completed, including origin and continuation.
ThreadHistoryCompactionCheckpointEventThe authoritative successful semantic change.

The checkpoint records:

  • compaction point;
  • preservation policy;
  • compacted and preserved message identities;
  • carried user-message source identities;
  • messages after the point;
  • replacement messages;
  • strategy descriptor;
  • compaction timestamp.

There is no persistent compaction middleware state. Trigger decisions derive from the actual model input, and successful projection derives from canonical checkpoints.

Built for production .NET agent applications.