Skip to content

Compaction

Conversation compaction shrinks older turns into a structured <compaction-summary> message when a long session approaches the model context window, while preserving recent turns verbatim and keeping critical state deterministic.

Compaction runs either automatically or on demand:

  • Automatic: Controlled by [compaction] in config.toml (enabled = true by default). After a turn finishes, ox checks latest-turn context usage against the effective threshold min(window * auto_compact_pct / 100, window - reserve_tokens) (defaults: auto_compact_pct = 85, reserve_tokens = 16384).
  • Manual: Run /compact [focus] in the TUI at any time. Optional focus text is appended to the summarizer input as user focus instructions.

See Configuration for the full [compaction] schema and Commands for slash command usage.

Before summarizing, ox walks backward through the conversation to find a split index (to_summarize, to_keep):

  • Each message is estimated at chars / 4 tokens plus 256 tokens per attachment.
  • ox accumulates at least keep_recent_tokens (default 20000) of recent messages to keep verbatim.
  • The cut always snaps to a human user-turn boundary (index > 0), so ToolCall / ToolResult pairs are never split across the compaction boundary.

If the entire conversation fits within keep_recent_tokens or no earlier user-turn boundary exists, compaction stops without modifying history.

Critical session state is reconstructed deterministically alongside the model-generated summary:

  • System prompt (Request.system): Refreshed from disk on compaction and sent outside the compacted message history, keeping system prompt sections, AGENTS.md, project memory index, and always skills current.
  • Active on-demand skills (<active-skills>): ox collects skill names from load_skill tool calls, <skill name="..."> tags, and any prior <active-skills> block, reloads their bodies from disk (skipping always skills), and embeds them within a 16,000-character budget (MAX_PRESERVED_SKILLS_CHARS) using fair water-filling allocation. Small skills stay intact while oversized skills are truncated with \n[truncated], and skills whose allocated budget falls below 64 characters are recorded as load_skill("<name>") pointers. After compaction, loaded_skills is reset so on_files and on_tools skills re-fire when matching activity recurs.
  • Tool outputs in the summarizer transcript: ToolResult bodies passed to the built-in summarizer share a 64,000-character budget (MAX_COMPACTION_TOOL_OUTPUTS_CHARS) via fair water-filling allocation, keeping small outputs intact and truncating oversized outputs with \n[truncated].
  • File tracking (<modified-files> and <read-files>): Extracted directly from tool calls in the compacted prefix (write, edit, multi_edit, patch, apply_patch for modified files, and read, view, read_file for read files) merged with any prior summary lists. Paths present in <modified-files> are omitted from <read-files>.

Incremental Summarization and Customization

Section titled “Incremental Summarization and Customization”

Compaction inserts a single user message wrapped in <compaction-summary>...</compaction-summary> ahead of the kept turns. When a later compaction sees a leading <compaction-summary>, it switches to an incremental merge prompt so the summary updates across cycles while dropping obsolete details.

The built-in summarizer resolves its system prompt in precedence order (highest to lowest):

  1. <cwd>/.ox/prompts/compact.md
  2. [compaction].prompt_file (relative paths resolve against <cwd>)
  3. [compaction].prompt
  4. Built-in initial or incremental template

Custom compaction prompts support Template Placeholders.

Additional customization hooks into the same pipeline:

  • Runtime focus: /compact <focus> appends user focus instructions to the summarizer transcript.
  • Hooks: PreCompact command hooks can supply extra compaction guidance. See Hooks.
  • Plugin override: A plugin implementing compaction/compact runs before the built-in summarizer. If the plugin returns an error or an empty summary, ox logs a warning and falls back to the built-in summarizer automatically. See Plugin Protocol.

Compaction appends a boundary record to the SQLite compactions table while leaving all original rows in messages intact.

Session resume loads the latest compaction summary followed by messages after that boundary, and /rewind and /fork continue to operate cleanly across compaction boundaries. See Sessions for the SQLite schema and rewind semantics.