CHAPTER 12 · CONTEXT BUDGET

Compaction: 85% Threshold and Optional Two-Pass

The default policy allows auto-compaction when context usage reaches 85%. Both memory flush and two-pass are disabled by default; they must be explicitly enabled to activate their respective flows.

Learning Objective
Memorize the five real fields and default values of CompactionPolicy, and trace the threshold check in Agent::should_auto_compact and xai-token-estimation.
Core Visual · Auto-Compaction Threshold
0% · Plenty of space available100% · Context limit

used × 100 >= context_window × threshold_percent. Comparison uses saturating multiplication; returns false when context window is 0.

CompactionPolicy Default Values
auto_compact_threshold_percent: u3285

Auto-compaction threshold percentage.

compact_model: Option<String>None

Uses the current session model when unspecified.

memory_flush_enabled: boolfalse

When enabled, a memory flush turn runs before compaction.

wall_clock_budget_secs: u64300

Wall-clock time budget per compaction, in seconds.

two_pass_enabled: boolfalse

Written in from config; defaults to the single-pass path.

When Two-Pass Applies
PASS 1 · PREFIRE

Pre-summarize the History Prefix

Only when two_pass_enabled is true: speculatively summarize the history prefix in the background near the threshold, producing NOTE₁.

PASS 2 · COMPACT

Summarize NOTE₁ + Recent Tail

During the actual compaction, combine NOTE₁ with the recent tail for a second summarization. When set to false, the original single-pass path is preserved.

Real Source Code Evidence
crates/codegen/xai-grok-agent/src/compaction.rs
impl Default for CompactionPolicy {
  fn default() -> Self {
    Self {
      auto_compact_threshold_percent: 85,
      compact_model: None,
      memory_flush_enabled: false,
      wall_clock_budget_secs: 300,
      two_pass_enabled: false,
    }
  }
}
crates/codegen/xai-token-estimation/src/lib.rs
pub fn exceeds_threshold(
  used: u64,
  context_window: u64,
  threshold_percent: u8,
) -> bool {
  if context_window == 0 { return false; }
  used.saturating_mul(100) >=
    context_window.saturating_mul(
      threshold_percent as u64
    )
}
Call Site: Agent::should_auto_compact receives total_tokens and NonZeroU64 context_window, then calls xai_token_estimation::exceeds_threshold. The policy struct itself has no fictitious should_compact(&self, usage: f64) method.
Source Snapshot Note: This page is verified against a locally synced copy. That copy has no .git metadata, so no claim is made about which commit it corresponds to.
Lab Exercise

Calculate the Boundary by Hand

With a context window of 100,000 and a threshold of 85, determine whether 84,999, 85,000, and 90,000 tokens trigger compaction. Then explain how enabling two_pass_enabled changes the compaction path without changing the default value to true.

Takeaway: The default threshold is 85, the wall clock is 300 seconds, and both memory flush and two-pass are false. Two-pass is an explicitly configured capability; the auto-trigger check lives in the Agent and token estimation layer.