CHAPTER 12 · CONTEXT BUDGET
Compaction: 85% 임계값과 선택적 two-pass
기본 정책은 컨텍스트 사용률이 85%에 도달하면 자동 압축을 허용합니다. memory flush와 two-pass는 기본적으로 비활성화되어 있으며, 활성화 후에야 해당 흐름이 실행됩니다.
학습 목표
CompactionPolicy의 5개 실제 필드와 기본값을 기억하고, Agent::should_auto_compact와 xai-token-estimation의 임계값 판단 로직까지 추적할 수 있습니다.핵심 시각 · 자동 압축 임계값
0% · 공간 충분100% · 컨텍스트 한계
used × 100 >= context_window × threshold_percent. 비교 시 포화 곱셈을 사용하며 context window가 0이면 false를 반환합니다.
CompactionPolicy 기본값
auto_compact_threshold_percent: u3285자동 압축 임계값 백분율입니다.
compact_model: Option<String>None지정하지 않으면 현재 Session 모델을 사용합니다.
memory_flush_enabled: boolfalse활성화 시 압축 전 memory flush turn이 실행됩니다.
wall_clock_budget_secs: u64300단일 압축의 wall clock 예산(초 단위)입니다.
two_pass_enabled: boolfalse설정 파싱 후 기록됩니다. 기본적으로 single-pass 경로를 사용합니다.
Two-pass가 작동하는 경우
PASS 1 · PREFIRE
이전 히스토리 프리픽스 미리 요약
two_pass_enabled가 true일 때만, 임계값에 근접하면 백그라운드에서 투기적으로 히스토리 프리픽스를 요약하여 NOTE₁을 생성합니다.
→
PASS 2 · COMPACT
NOTE₁과 최근 tail 요약
정식 압축 시 NOTE₁과 recent tail을 결합하여 다시 요약합니다. 설정이 false이면 기존 single-pass 경로를 유지합니다.
실제 소스코드 증거
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,
}
}
}
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
)
}
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
)
}
호출 위치:
Agent::should_auto_compact는 total_tokens와 NonZeroU64 context_window를 받아 xai_token_estimation::exceeds_threshold를 호출합니다. 정책 구조체 자체에는 가상의 should_compact(&self, usage: f64) 메서드가 없습니다.소스코드 스냅샷 참고: 이 페이지는 로컬 동기화 복사본을 기반으로 검토되었습니다. 해당 복사본에는
.git 메타데이터가 없으므로 특정 commit 버전에 해당한다고 주장하지 않습니다.실습 문제
경계값 직접 계산
context window가 100,000이고 임계값이 85일 때, 84,999 · 85,000 · 90,000 tokens가 각각 압축을 트리거하는지 판단하세요. 또한 two_pass_enabled를 활성화하면 압축 경로가 변경되지만 기본값이 true로 바뀌지는 않음을 설명하세요.
Takeaway: 기본 임계값은 85, wall clock은 300초, memory flush와 two-pass는 모두 false입니다. Two-pass는 명시적 설정 기능이며, 자동 트리거 판단은 Agent와 token estimation에 있습니다.