Harness Notes

Compaction

Updated 2026-11-24,

On this page

What each harness does when the conversation outgrows the context window: when it compacts, what it keeps, and how you can steer it.

When it happens

QuestionOpenCodechecked at 2.0.29
Automatic compaction, and when

Yes. Before each model step OpenCode estimates the prompt and compacts when it reaches the usable input window minus a buffer. At 2.2.7 the window is the smaller positive input/context limit when both exist, otherwise the known limit. The default buffer is 15% of that window or 16,000 tokens, whichever is larger (windows under 32K keep just 15%). An explicit token or percentage buffer overrides it when it leaves room.

Models with an unknown window never auto-compact; only a provider rejection triggers it.

The 2.1 default is earlier than the previous 10% reserve. v2.1.1 uses floor(window × 0.15); an explicit compaction.buffer overrides the default.

Since 2.3.1, background summary preparation is on by default; set compaction.background: false to disable it. experimental.background_compaction is removed. At 2.3.2 the runner calls precompute, which requires auto and background, skips native compaction, and checks measured prompt, model and cache freshness before holding a summary for the normal compaction boundary. An unused summary still costs tokens. The older 2.1.4 64K/85% preparation conditions are historical, not current. No runtime probe.

Change the trigger or turn it off

Set compaction.buffer to a token count or, since 2.2.5, a percentage string such as "20%" to compact at 80% of the usable input window. Larger buffers compact earlier. A buffer that leaves no room is ignored in favor of the default. Set compaction.auto to false to disable automatic and overflow compaction; manual compaction remains.

Historical 2.1.4 finding (superseded by 2.3.1): 2.1.4 adds experimental.background_compaction: true. It prepares a summary before the ordinary trigger but does not apply it immediately or change the configured trigger. Unused summaries still consume tokens.

V2 has no OPENCODE_DISABLE_AUTOCOMPACT env var. V1's reserved becomes buffer.

Since 2.3.1, background summary preparation is on by default; set compaction.background: false to disable it. experimental.background_compaction is removed. At 2.3.2 the runner calls precompute, which requires auto and background, skips native compaction, and checks measured prompt, model and cache freshness before holding a summary for the normal compaction boundary. An unused summary still costs tokens. The older 2.1.4 64K/85% preparation conditions are historical, not current. No runtime probe.

Compact by hand

Partly

Type /compact or press the leader key then c in the TUI. You can also call opencode api post /api/session/<id>/compact --data '{}', or ctx.session.compact() from a plugin. It runs at the next safe point, even for short histories. The command takes no instructions about what to keep; use compaction.keep.tokens or a plugin compaction hook instead.

The V1 alias /summarize is not registered in the V2 TUI.

See how full the context is

The TUI prompt footer and the sidebar's Context section show the last response's tokens and the percentage of the model's context window, for example 45,000 (23%). Auto-compaction fires around 85% of the window (less on small models), so plan for compaction before 100%.

The approximate 85% trigger is the default in 2.1, not a universal configured threshold; the 16K minimum advances it on small windows.

What it does

QuestionOpenCodechecked at 2.0.29
What replaces the conversation

In 2.1, summary compaction normally sends the whole conversation, including the tail that will be kept, to the summary model so it can reuse the normal prompt cache. It writes a structured checkpoint (Objective, Requirements, Decisions, Work State, Next Move, Relevant Files, Important Context), and later requests combine that summary with recent text reconstructed from metadata.keepID. If the whole request cannot fit, the implementation falls back to the history before the retained tail and then progressively shrinks it. A supported native compaction instead stores its provider replacement window in native.messages.

The docs say there is no separate compaction model, but the source does honor a model set on the hidden compaction agent.

The summary call no longer stores unused provider replay data. A compaction hook may still supply result.native; native.messages overrides text replay when present.

At 2.1.4, precomputation requires >=85% of the calculated compaction ceiling and a measured prompt >=64K tokens. It is skipped for native compaction, an already-due context or an unknown ceiling; a held summary is reused only while its anchor/model/tail remain valid, otherwise ordinary summarization runs. A still-running usable summary can be awaited.

2.1.3–2.1.4 summary output uses normal model output-room calculation, with a 32K floor limited by actual remaining room; it is not an unconditional 32K cap or reservation.

2.2.0 source sizes the first unchanged summary request using the provider-reported prompt count plus estimates of later text. Overflow-triggered summaries and retries after rejection use estimation/reduction instead; plugin hooks can still enlarge a request beyond the measured count. This does not change the recorded automatic trigger formula.

Since 2.3.1, background summary preparation is on by default; set compaction.background: false to disable it. experimental.background_compaction is removed. At 2.3.2 the runner calls precompute, which requires auto and background, skips native compaction, and checks measured prompt, model and cache freshness before holding a summary for the normal compaction boundary. An unused summary still costs tokens. The older 2.1.4 64K/85% preparation conditions are historical, not current. No runtime probe.

What survives

About compaction.keep.tokens of the newest conversation is kept beside the summary (default 15,000), starting at a user-message boundary. In that kept part, tool output over 1,250 characters is shortened and attachments become one-line descriptions. Instruction files such as AGENTS.md are excluded from the summary and resent in their current version as the new baseline. Activated skill text is kept only if it falls inside the recent window. In 2.4.1 the keep.tokens limit also applies when the newest message alone exceeds the budget.

Since 2.1, newly written summaries reference the first retained message with metadata.keepID instead of duplicating recent text. History reconstructs the text up to the checkpoint; forks remap this reference. Legacy recent text is decoded as metadata.recent.

2.2.0 source folds current instruction values into the new epoch on compaction completion. API-managed entries persist separately from chronological updates; no runtime preservation probe.

Trimming without summarising

Not supported

No. V2 has no separate tool-output pruning. compaction.prune and compaction.tail_turns are ignored with a warning, and recent context is kept by token budget only. Tool output is shortened only inside the compaction checkpoint.

The full history afterwards

Yes. Earlier messages remain stored. In 2.1, a completed summary stores summary text and metadata.keepID referencing the first retained message; history reconstructs the kept text rather than duplicating it in the checkpoint. Legacy compactions retain their text through metadata.recent. Native checkpoints store native.messages. The TUI shows compaction as it starts, streams and ends.

Shaping it

QuestionOpenCodechecked at 2.0.29
Customise the summary or hook in

Plugins can register ctx.session.hook("compaction", ...) to edit the messages, system text, or options of the summary request. Setting event.result = { summary } supplies your own summary and skips the model call. Plugins can follow session.compaction.started, delta, ended, and failed events. The built-in summary prompt itself can't be changed in config, and there are no PreCompact or PostCompact shell hooks.

OpenCode appends its own summary prompt after the hooks run, so a hook cannot replace it except by supplying result.

2.1 replaces SessionCompactionResult.providerState with native. Hook metadata can override keepID; result.native.messages supplies a replacement message window. The obsolete session.compacted event is removed; use session.compaction.started/ended.

When a request overflows anyway

If the provider rejects a request as too long, OpenCode compacts, aiming at 70% of the rejected size, and retries once. A second rejection is returned as an error. If the summary request itself is too long, OpenCode sends a flattened text version and shrinks it to 70%, 50%, then 35%, dropping the oldest exchanges, before giving up. With compaction.auto: false it does not compact on overflow and returns the error.

The docs' Settings table says auto controls overflow recovery as well as preflight compaction, and the source agrees: auto=false skips both.

In 2.0.29, output-token-limit (finish reason length) recovery is separate from context-overflow compaction: source adds continuation guidance and fails after three consecutive length-limited assistant responses (two continuations).

In 2.0.30, the summary request and its retry reduce the output-token allowance to fit the actual estimated prompt remaining in the model window. This is summary request budgeting, not a new auto-compaction threshold.

2.1.4 ignores an explicit buffer that leaves no room. Oversized summary requests progressively shorten exchanges: clip tool output, remove outputs/clip tool inputs, retain clipped user text, then omit an exchange. Precomputed summaries are not used for overflow recovery.

2.2.1 /goal: if the goal reviewer's request overflows the context window, OpenCode compacts the session once and reviews again. A second overflow in the same round pauses the goal.

Notes and sources

OpenCode, 47 sources
  • Since 2.3.1, background summary preparation is on by default; set compaction.background: false to disable it. experimental.background_compaction is removed. At 2.3.2 the runner calls precompute, which requires auto and background, skips native compaction, and checks measured prompt, model and cache freshness before holding a summary for the normal compaction boundary. An unused summary still costs tokens. The older 2.1.4 64K/85% preparation conditions are historical, not current. No runtime probe.
Automatic compaction, and when, 14
Change the trigger or turn it off, 16
Compact by hand, 3
See how full the context is, 3
What replaces the conversation, 14
What survives, 16
Trimming without summarising, 3
The full history afterwards, 6
Customise the summary or hook in, 4
When a request overflows anyway, 8