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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//! Dynamic token budget allocation for multi-turn conversations.
//!
//! Provides [`TokenBudgetManager`] which tracks per-turn token usage and
//! applies one of several allocation policies to distribute a total budget
//! across system prompt, conversation history, and model response.
use std::collections::VecDeque;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
// ---------------------------------------------------------------------------
// Policy
// ---------------------------------------------------------------------------
/// How the budget should be split each turn.
#[derive(Debug, Clone)]
pub enum BudgetPolicy {
/// Every turn gets the same fixed split.
Fixed(usize),
/// Percentage-based split (fractions must sum to ≤ 1.0).
Proportional {
system_pct: f64,
history_pct: f64,
response_pct: f64,
},
/// Response window adapts based on recent usage; history gets the rest.
Adaptive {
min_response: usize,
max_response: usize,
},
/// Budget is re-computed over the last `window_turns` turns.
Rolling { window_turns: usize },
}
// ---------------------------------------------------------------------------
// Per-turn structures
// ---------------------------------------------------------------------------
/// Token allocation for a single conversation turn.
#[derive(Debug, Clone)]
pub struct TurnBudget {
pub system_tokens: usize,
pub history_tokens: usize,
pub response_tokens: usize,
pub total: usize,
pub turn_id: usize,
}
/// Efficiency report for a single turn.
#[derive(Debug, Clone)]
pub struct BudgetUsage {
pub allocated: usize,
pub used: usize,
pub efficiency: f64,
pub overflow: bool,
}
// ---------------------------------------------------------------------------
// Summary
// ---------------------------------------------------------------------------
/// High-level summary across all turns managed so far.
#[derive(Debug, Clone)]
pub struct BudgetSummary {
pub total_budget: usize,
pub total_used: usize,
pub efficiency: f64,
pub turns_completed: usize,
pub avg_turn_tokens: f64,
}
// ---------------------------------------------------------------------------
// Manager
// ---------------------------------------------------------------------------
/// Manages token budgets across multiple conversation turns.
pub struct TokenBudgetManager {
pub total_budget: usize,
policy: BudgetPolicy,
history: VecDeque<TurnBudget>,
/// Tracks actual usage for the *current* turn.
used_this_turn: Arc<AtomicUsize>,
/// Recorded usages per completed turn (allocated, used).
usage_history: VecDeque<(usize, usize)>,
/// Adaptive response target — updated by [`adaptive_adjust`].
adaptive_response_target: usize,
}
impl TokenBudgetManager {
/// Create a new manager with the given total budget and policy.
pub fn new(total_budget: usize, policy: BudgetPolicy) -> Self {
let adaptive_response_target = match &policy {
BudgetPolicy::Adaptive { min_response, max_response } => {
(min_response + max_response) / 2
}
BudgetPolicy::Fixed(n) => *n / 4,
_ => total_budget / 4,
};
Self {
total_budget,
policy,
history: VecDeque::new(),
used_this_turn: Arc::new(AtomicUsize::new(0)),
usage_history: VecDeque::new(),
adaptive_response_target,
}
}
/// Allocate tokens for a new turn given known system and history sizes.
pub fn allocate_turn(
&mut self,
turn_id: usize,
system_size: usize,
history_size: usize,
) -> TurnBudget {
// Reset current-turn counter.
self.used_this_turn.store(0, Ordering::SeqCst);
let budget = self.total_budget;
let (system_tokens, history_tokens, response_tokens) = match &self.policy {
BudgetPolicy::Fixed(per_turn) => {
let avail = (*per_turn).min(budget);
let sys = system_size.min(avail / 4);
let hist = history_size.min(avail / 2);
let resp = avail.saturating_sub(sys + hist);
(sys, hist, resp)
}
BudgetPolicy::Proportional {
system_pct,
history_pct,
response_pct,
} => {
let sys = ((budget as f64 * system_pct) as usize).min(system_size);
let hist = ((budget as f64 * history_pct) as usize).min(history_size);
let resp = (budget as f64 * response_pct) as usize;
(sys, hist, resp)
}
BudgetPolicy::Adaptive {
min_response,
max_response,
} => {
let resp = self
.adaptive_response_target
.clamp(*min_response, *max_response);
let remaining = budget.saturating_sub(resp);
let sys = system_size.min(remaining / 3);
let hist = history_size.min(remaining.saturating_sub(sys));
(sys, hist, resp)
}
BudgetPolicy::Rolling { window_turns } => {
let window = *window_turns;
let avg_used = self.rolling_average_usage(window);
let est_total = avg_used as usize;
let sys = system_size.min(est_total / 4);
let hist = history_size.min(est_total / 2);
let resp = budget.saturating_sub(sys + hist);
(sys, hist, resp)
}
};
let allocated = system_tokens + history_tokens + response_tokens;
let turn = TurnBudget {
system_tokens,
history_tokens,
response_tokens,
total: allocated,
turn_id,
};
self.history.push_back(turn.clone());
turn
}
/// Record how many tokens were actually consumed this turn.
pub fn record_usage(&self, used: usize) {
self.used_this_turn.store(used, Ordering::SeqCst);
}
/// Remaining budget after subtracting all recorded usage.
pub fn remaining_budget(&self) -> usize {
let used: usize = self.usage_history.iter().map(|(_, u)| u).sum();
let current = self.used_this_turn.load(Ordering::SeqCst);
self.total_budget.saturating_sub(used + current)
}
/// Per-turn efficiency report (most recent turns first).
pub fn efficiency_report(&self) -> Vec<BudgetUsage> {
self.history
.iter()
.zip(self.usage_history.iter())
.map(|(turn, (allocated, used))| BudgetUsage {
allocated: *allocated,
used: *used,
efficiency: if *allocated == 0 {
0.0
} else {
(*used as f64) / (*allocated as f64)
},
overflow: *used > *allocated,
})
.collect()
}
/// Update the adaptive target based on the actual response token count.
pub fn adaptive_adjust(&mut self, actual_response_tokens: usize) {
// Commit the current turn's usage to history.
let used = self.used_this_turn.load(Ordering::SeqCst);
if let Some(last) = self.history.back() {
self.usage_history.push_back((last.total, used));
}
match &self.policy {
BudgetPolicy::Adaptive {
min_response,
max_response,
} => {
// Exponential moving average with α = 0.3.
let alpha = 0.3_f64;
let new_target = alpha * actual_response_tokens as f64
+ (1.0 - alpha) * self.adaptive_response_target as f64;
self.adaptive_response_target =
(new_target as usize).clamp(*min_response, *max_response);
}
_ => {
// For non-adaptive policies just record the usage.
}
}
}
/// Rolling average of total tokens used over the last `window` turns.
pub fn rolling_average_usage(&self, window: usize) -> f64 {
if self.usage_history.is_empty() {
return (self.total_budget / 2) as f64;
}
let skip = self.usage_history.len().saturating_sub(window);
let slice: Vec<_> = self.usage_history.iter().skip(skip).collect();
if slice.is_empty() {
return 0.0;
}
let sum: usize = slice.iter().map(|(_, u)| u).sum();
sum as f64 / slice.len() as f64
}
/// Suggest which content pieces to trim and by how much.
///
/// Returns a parallel `Vec<usize>` where each entry is the number of
/// tokens to cut from the corresponding `content_sizes` entry.
pub fn suggest_truncation(&self, content_sizes: &[usize]) -> Vec<usize> {
let remaining = self.remaining_budget();
let total_content: usize = content_sizes.iter().sum();
if total_content <= remaining {
return vec![0; content_sizes.len()];
}
let excess = total_content - remaining;
// Distribute cuts proportionally.
content_sizes
.iter()
.map(|&size| {
if total_content == 0 {
0
} else {
let cut = (size as f64 / total_content as f64 * excess as f64) as usize;
cut.min(size)
}
})
.collect()
}
/// High-level summary of overall budget consumption.
pub fn budget_summary(&self) -> BudgetSummary {
let total_used: usize = self.usage_history.iter().map(|(_, u)| u).sum();
let turns = self.usage_history.len();
let efficiency = if self.total_budget == 0 {
0.0
} else {
total_used as f64 / self.total_budget as f64
};
let avg_turn_tokens = if turns == 0 {
0.0
} else {
total_used as f64 / turns as f64
};
BudgetSummary {
total_budget: self.total_budget,
total_used,
efficiency,
turns_completed: turns,
avg_turn_tokens,
}
}
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fixed_policy_allocates() {
let mut mgr = TokenBudgetManager::new(
4096,
BudgetPolicy::Fixed(1024),
);
let tb = mgr.allocate_turn(0, 200, 400);
assert!(tb.response_tokens > 0);
assert_eq!(tb.turn_id, 0);
}
#[test]
fn proportional_policy() {
let mut mgr = TokenBudgetManager::new(
10_000,
BudgetPolicy::Proportional {
system_pct: 0.1,
history_pct: 0.4,
response_pct: 0.5,
},
);
let tb = mgr.allocate_turn(1, 500, 2000);
assert!(tb.system_tokens <= 1000);
assert!(tb.response_tokens <= 5000);
}
#[test]
fn adaptive_policy_adjusts() {
let mut mgr = TokenBudgetManager::new(
8192,
BudgetPolicy::Adaptive {
min_response: 128,
max_response: 2048,
},
);
mgr.allocate_turn(0, 100, 300);
mgr.record_usage(600);
mgr.adaptive_adjust(512);
let tb = mgr.allocate_turn(1, 100, 300);
assert!(tb.response_tokens >= 128);
}
#[test]
fn suggest_truncation_sums_correctly() {
let mut mgr = TokenBudgetManager::new(1000, BudgetPolicy::Fixed(200));
mgr.allocate_turn(0, 50, 100);
mgr.record_usage(900); // almost exhausted
mgr.adaptive_adjust(900);
let cuts = mgr.suggest_truncation(&[400, 400, 400]);
let total_cut: usize = cuts.iter().sum();
assert!(total_cut > 0);
}
#[test]
fn rolling_average_no_panic_empty() {
let mgr = TokenBudgetManager::new(4096, BudgetPolicy::Rolling { window_turns: 5 });
let avg = mgr.rolling_average_usage(5);
assert!(avg > 0.0);
}
}