1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* Example 25: Auto-Compact Context API Demo
*
* Demonstrates the auto-compact context management API:
* - Query effective context window size
* - Get auto-compact threshold
* - Calculate token warning states
* - Check if auto-compact is enabled
* - Monitor compaction tracking state
*
* Run: cargo run --example 25_auto_compact
*
* Environment variables from .env:
* - AI_BASE_URL: LLM server URL
* - AI_AUTH_TOKEN: API authentication token
* - AI_MODEL: Model name
*
* Optional overrides:
* - AI_CODE_AUTO_COMPACT_WINDOW: Override context window size
* - CLAUDE_AUTOCOMPACT_PCT_OVERRIDE: Override threshold as percentage (0-100)
* - DISABLE_COMPACT: Disable all compaction
* - DISABLE_AUTO_COMPACT: Disable auto-compact only
*/
use ai_agent::{
AutoCompactTrackingState, calculate_token_warning_state, get_auto_compact_threshold,
get_effective_context_window_size, is_auto_compact_enabled,
};
fn main() {
println!("=== Example 25: Auto-Compact Context API Demo ===\n");
// Default model for demo
let model = "claude-sonnet-4-6";
// ============================================
// 1. Query effective context window size
// ============================================
println!("--- 1. Effective Context Window ---");
let effective_window = get_effective_context_window_size(model);
println!("Model: {}", model);
println!("Effective context window: {} tokens", effective_window);
println!("(This accounts for reserved output tokens for summary generation)\n");
// ============================================
// 2. Get auto-compact threshold
// ============================================
println!("--- 2. Auto-Compact Threshold ---");
let threshold = get_auto_compact_threshold(model);
println!("Auto-compact threshold: {} tokens", threshold);
println!(
"Tokens buffer for compaction: {} tokens",
threshold.saturating_sub(effective_window)
);
println!("\nTip: Set AI_CODE_AUTO_COMPACT_WINDOW=100000 to test with smaller window\n");
// ============================================
// 3. Check if auto-compact is enabled
// ============================================
println!("--- 3. Auto-Compact Status ---");
let enabled = is_auto_compact_enabled();
println!(
"Auto-compact enabled: {}",
if enabled { "Yes" } else { "No" }
);
println!("\nDisable with: DISABLE_COMPACT=1 or DISABLE_AUTO_COMPACT=1\n");
// ============================================
// 4. Calculate token warning states
// ============================================
println!("--- 4. Token Warning States ---");
// Simulate different token usage levels
let test_usages: Vec<u32> = vec![
50_000, // Low usage
100_000, // Medium usage
threshold.saturating_sub(20_000), // Near warning
threshold.saturating_sub(1), // At auto-compact threshold
effective_window.saturating_sub(3_000), // At blocking limit
];
for usage in test_usages {
let state = calculate_token_warning_state(usage, model);
println!("\nToken usage: {} tokens", usage);
println!(" - Percent left: {:.1}%", state.percent_left);
println!(
" - Warning threshold: {}",
if state.is_above_warning_threshold {
"YES"
} else {
"No"
}
);
println!(
" - Error threshold: {}",
if state.is_above_error_threshold {
"YES"
} else {
"No"
}
);
println!(
" - Auto-compact trigger: {}",
if state.is_above_auto_compact_threshold {
"YES"
} else {
"No"
}
);
println!(
" - Blocking limit: {}",
if state.is_at_blocking_limit {
"YES"
} else {
"No"
}
);
}
// ============================================
// 5. Auto-compact tracking state
// ============================================
println!("\n--- 5. Auto-Compact Tracking State ---");
// Create initial tracking state
let mut tracking = AutoCompactTrackingState::new();
println!("Initial state:");
println!(" - Compacted: {}", tracking.compacted);
println!(" - Turn counter: {}", tracking.turn_counter);
println!(" - Turn ID: {}", tracking.turn_id);
println!(
" - Consecutive failures: {}",
tracking.consecutive_failures
);
// Simulate turns passing
for i in 1..=3 {
tracking.turn_counter = i;
tracking.turn_id = uuid::Uuid::new_v4().to_string();
println!("\nAfter turn {}: turn_id = {}", i, &tracking.turn_id[..8]);
}
// Simulate a compaction
tracking.compacted = true;
println!("\nAfter compaction: compacted = {}", tracking.compacted);
// Simulate failure
tracking.consecutive_failures = 1;
println!(
"After failure: consecutive_failures = {}",
tracking.consecutive_failures
);
// Reset on success
tracking.compacted = false;
tracking.consecutive_failures = 0;
println!(
"After success reset: compacted = {}, failures = {}\n",
tracking.compacted, tracking.consecutive_failures
);
// ============================================
// 6. Environment variable overrides
// ============================================
println!("--- 6. Environment Overrides ---");
println!("AI_CODE_AUTO_COMPACT_WINDOW - Override context window size");
println!("CLAUDE_AUTOCOMPACT_PCT_OVERRIDE - Override threshold (0-100%)");
println!("DISABLE_COMPACT - Disable all compaction");
println!("DISABLE_AUTO_COMPACT - Disable auto-compact only");
println!("AI_CODE_BLOCKING_LIMIT_OVERRIDE - Override blocking limit");
println!("\n=== done ===");
}