Part 6 · Inside a Production Coding Agent

Compaction: 85% Threshold & Optional Two-Pass

Verifying auto-compression thresholds, memory flush, two-pass, and timeout budget from real config

THE QUESTION THIS PAGE ANSWERS

ANSWER FIRST

What is the key idea behind “Compaction: 85% Threshold & Optional Two-Pass”?

Verifying auto-compression thresholds, memory flush, two-pass, and timeout budget from real config

DECISION RULE

Make the claim earn its place. Use this page as a decision aid, not a definition to memorize. Connect the idea to one real task, one observable result, and one failure that would change your mind.

TRY NEXT

Write one question you could answer with evidence after trying this idea.

WATCH FOR

A conclusion that sounds complete but leaves the key assumption untested.

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.

How “Core Visual · Auto-Compaction Threshold” changes an answer

“used × 100 >= context_window × threshold_percent .” shows that a model does not process the “word count” we see. It processes Token pieces. Tokenization affects input length, how much context fits, and how much computation a request consumes.

Length, information, and context are different

As “Auto-compaction threshold percentage” grows, separate three questions: how many Tokens the text becomes, which pieces can change the current decision, and whether older material has fallen outside the context window. Removing repetition is often more useful than simply making the window larger.

Keep what can change the decision

Use “With a context window of 100,000 and a threshold of 85, determine whether 84,999, 85,000, and 90,000 tokens trigger compaction.” as an A/B test: keep the same question while removing repeated background, compressing format, and trimming irrelevant history. Compare answer quality, latency, and Token count.

From “Core Visual · Auto-Compaction Threshold” to “CompactionPolicy Default Values”

“Core Visual · Auto-Compaction Threshold” grounds the problem in “used × 100 >= context_window × threshold_percent . Comparison uses saturating multiplication; returns false when context window is 0”. “CompactionPolicy Default Values” then moves it toward “Auto-compaction threshold percentage”. Together, they show that the lesson is not just a conclusion to remember, but a claim with conditions.

Carry the judgment into the next situation

For long text, keep what can change the conclusion before compressing format and history. A larger context is worth its cost only when the added information is useful.

  • “Core Visual · Auto-Compaction Threshold”: used × 100 >= context_window × threshold_percent . Comparison uses saturating multiplication; returns false when context window is 0
  • “CompactionPolicy Default Values”: Auto-compaction threshold percentage
  • “The closing point”: 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

The final “The closing point” brings the discussion to “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”. The useful thing to carry forward is knowing which judgments must be revisited when input, scale, or risk changes.

Mark as learned Your reading progress updates automatically
← PreviousNext →

Keep reading

The next useful article in the thread.

ARTICLE DISCUSSION

Leave one useful thought here.

Keep the idea that clicked, the question that stayed open, or a small note for the next learner.

Discussing Compaction: 85% Threshold & Optional Two-Pass Inside a Production Coding Agent
3discussionsArticle discussion · synced with the Circle
View in the learning circle
AM
Asha MorganContent editor
INSIGHTField note

I turned one judgment from this article into a small experiment I could run today. Knowing what to observe next is more useful than simply remembering the conclusion.

ARTICLE DISCUSSION7 helpful
LH
Lin HarperIndie developer
INSIGHTInsight

After reading this, I first looked for the conditions behind the idea instead of copying the method into a project. That order made the later trade-offs much clearer.

ARTICLE DISCUSSION5 helpful
KM
Kiki MooreProduct operations
QUESTIONQuestion

When this judgment reaches real work, which constraint should be added first? I am curious which step matters most between reading and the first practical attempt.

ARTICLE DISCUSSION4 helpful