ai_tokenopt 0.5.5

Adaptive token optimization engine for LLM inference pipelines — compresses prompts, conversation history, tool schemas, and output streams to minimize token usage while preserving response quality.
Documentation
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//! Token budget allocation engine
//!
//! Dynamically distributes the available context window across competing
//! concerns: system prompt, RAG context, tool definitions, conversation
//! history, and response headroom. Flags when compaction is needed.

use crate::config::TokenOptimizationConfig;
use crate::estimator::ConversationTokenEstimate;

/// Dynamic token budget allocation across context window sections.
#[derive(Debug, Clone, PartialEq)]
pub struct BudgetAllocation {
    /// Maximum tokens for the system prompt
    pub system_prompt: u32,
    /// Maximum tokens for RAG-injected context
    pub rag_context: u32,
    /// Maximum tokens for tool definitions
    pub tool_definitions: u32,
    /// Maximum tokens for conversation history (messages + summary)
    pub history: u32,
    /// Tokens reserved for the LLM response
    pub response_headroom: u32,
    /// Whether history compaction is required to fit within budget
    pub requires_compaction: bool,
    /// Current pressure level (0.0 = empty, 1.0 = at capacity)
    pub pressure: f32,
}

/// Estimated per-tool token overhead for definition schema.
const TOKENS_PER_TOOL_ESTIMATE: u32 = 150;

/// Token budget calculator.
#[derive(Debug)]
pub struct TokenBudget {
    context_window: u32,
    response_headroom_ratio: f32,
    compaction_trigger_ratio: f32,
    system_prompt_budget_ratio: f32,
    rag_budget_ratio: f32,
}

impl TokenBudget {
    /// Create a new budget calculator from configuration.
    #[must_use]
    pub fn new(config: &TokenOptimizationConfig) -> Self {
        Self {
            context_window: config.context_window_tokens,
            response_headroom_ratio: config.response_headroom_ratio,
            compaction_trigger_ratio: config.compaction_trigger_ratio,
            system_prompt_budget_ratio: config.system_prompt_budget_ratio,
            rag_budget_ratio: config.rag_budget_ratio,
        }
    }

    /// Compute a dynamic budget allocation based on actual content sizes.
    ///
    /// The algorithm:
    /// 1. Reserve response headroom (fixed fraction of context window)
    /// 2. Allocate system prompt — min(actual, budget cap)
    /// 3. Allocate RAG context — min(actual, budget cap)
    /// 4. Allocate tool definitions if tools are present
    /// 5. Remaining budget goes to conversation history
    /// 6. If history exceeds remaining budget → flag compaction required
    #[must_use]
    pub fn allocate(
        &self,
        estimate: &ConversationTokenEstimate,
        has_tools: bool,
        tool_count: usize,
    ) -> BudgetAllocation {
        // 1. Reserve response headroom
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let response_headroom =
            (f64::from(self.context_window) * f64::from(self.response_headroom_ratio)) as u32;
        let available = self.context_window.saturating_sub(response_headroom);

        // 2. System prompt: take the minimum of actual size and budget cap
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let system_prompt_cap =
            (f64::from(available) * f64::from(self.system_prompt_budget_ratio)) as u32;
        let system_prompt = estimate.system_prompt.min(system_prompt_cap);

        // 3. RAG context budget
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let rag_cap = (f64::from(available) * f64::from(self.rag_budget_ratio)) as u32;
        // RAG tokens are part of system_prompt estimate in the current pipeline,
        // so this cap is for the optimizer to enforce during RAG dedup.
        let rag_context = rag_cap;

        // 4. Tool definitions (if present)
        #[allow(clippy::cast_possible_truncation)]
        let tool_definitions = if has_tools {
            (tool_count as u32) * TOKENS_PER_TOOL_ESTIMATE
        } else {
            0
        };

        // 5. Remaining budget for history
        let used_by_fixed = system_prompt + tool_definitions;
        let history_budget = available.saturating_sub(used_by_fixed);

        // 6. Check if compaction is needed
        let history_actual = estimate.history + estimate.summary;
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let compaction_threshold =
            (f64::from(history_budget) * f64::from(self.compaction_trigger_ratio)) as u32;
        let requires_compaction = history_actual > compaction_threshold && history_actual > 0;

        // Overall pressure: how full is the context window
        let total_used = estimate.total + tool_definitions;
        #[allow(clippy::cast_possible_truncation)]
        let pressure = if available > 0 {
            (f64::from(total_used) / f64::from(available)) as f32
        } else {
            1.0
        };

        BudgetAllocation {
            system_prompt,
            rag_context,
            tool_definitions,
            history: history_budget,
            response_headroom,
            requires_compaction,
            pressure: pressure.min(1.0),
        }
    }

    /// Allocate budget with pressure-aware priority ordering.
    ///
    /// When the context window is under pressure (> 90% full), this method
    /// rebalances budgets using a fixed priority order:
    ///
    /// 1. **History** is cut first (lowest priority)
    /// 2. **Tool definitions** are reduced second
    /// 3. **System prompt** is protected (highest priority)
    ///
    /// Below 90% pressure, this behaves identically to [`allocate`](Self::allocate).
    #[must_use]
    pub fn allocate_with_pressure_priority(
        &self,
        estimate: &ConversationTokenEstimate,
        has_tools: bool,
        tool_count: usize,
    ) -> BudgetAllocation {
        let base = self.allocate(estimate, has_tools, tool_count);

        // Only apply pressure rebalancing above 90% pressure
        if base.pressure < 0.9 {
            return base;
        }

        // Under high pressure: guarantee system prompt gets its full cap,
        // then tools get what they need (capped), then history gets the rest.
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let response_headroom =
            (f64::from(self.context_window) * f64::from(self.response_headroom_ratio)) as u32;
        let available = self.context_window.saturating_sub(response_headroom);

        // System prompt: protected — gets full cap
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let system_prompt_cap =
            (f64::from(available) * f64::from(self.system_prompt_budget_ratio)) as u32;
        let system_prompt = estimate.system_prompt.min(system_prompt_cap);

        // RAG context: same cap
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let rag_cap = (f64::from(available) * f64::from(self.rag_budget_ratio)) as u32;
        let rag_context = rag_cap;

        // Tools: capped at half the remaining space (leave room for history)
        let after_system = available.saturating_sub(system_prompt);
        #[allow(clippy::cast_possible_truncation)]
        let tool_estimate = if has_tools {
            (tool_count as u32) * TOKENS_PER_TOOL_ESTIMATE
        } else {
            0
        };
        let tool_definitions = tool_estimate.min(after_system / 2);

        // History: whatever is left
        let history_budget = after_system.saturating_sub(tool_definitions);
        let history_actual = estimate.history + estimate.summary;
        let requires_compaction = history_actual > history_budget && history_actual > 0;

        BudgetAllocation {
            system_prompt,
            rag_context,
            tool_definitions,
            history: history_budget,
            response_headroom,
            requires_compaction,
            pressure: base.pressure,
        }
    }

    /// Allocate budget with presence-aware redistribution.
    ///
    /// Unlike [`allocate`](Self::allocate), this method detects which
    /// components are absent and redistributes their budget:
    ///
    /// - **No RAG**: RAG budget ratio is added to the system prompt cap,
    ///   giving long system prompts more room before trimming.
    /// - **No tools**: tool budget is already 0 and goes to history.
    /// - **Small system prompt**: unused cap space goes to history.
    ///
    /// Priority order: history (highest) > system prompt > RAG > tools.
    #[must_use]
    pub fn allocate_adaptive(
        &self,
        estimate: &ConversationTokenEstimate,
        has_rag: bool,
        has_tools: bool,
        tool_count: usize,
    ) -> BudgetAllocation {
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let response_headroom =
            (f64::from(self.context_window) * f64::from(self.response_headroom_ratio)) as u32;
        let available = self.context_window.saturating_sub(response_headroom);

        // Redistribute absent-component budgets
        let (effective_system_ratio, effective_rag_ratio) = if has_rag {
            (self.system_prompt_budget_ratio, self.rag_budget_ratio)
        } else {
            // No RAG: give its budget to the system prompt cap
            (
                self.system_prompt_budget_ratio + self.rag_budget_ratio,
                0.0_f32,
            )
        };

        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let system_prompt_cap = (f64::from(available) * f64::from(effective_system_ratio)) as u32;
        let system_prompt = estimate.system_prompt.min(system_prompt_cap);

        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let rag_context = (f64::from(available) * f64::from(effective_rag_ratio)) as u32;

        #[allow(clippy::cast_possible_truncation)]
        let tool_definitions = if has_tools {
            (tool_count as u32) * TOKENS_PER_TOOL_ESTIMATE
        } else {
            0
        };

        // History gets everything not consumed by system prompt and tools
        let used_by_fixed = system_prompt + tool_definitions;
        let history_budget = available.saturating_sub(used_by_fixed);

        let history_actual = estimate.history + estimate.summary;
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let compaction_threshold =
            (f64::from(history_budget) * f64::from(self.compaction_trigger_ratio)) as u32;
        let requires_compaction = history_actual > compaction_threshold && history_actual > 0;

        let total_used = estimate.total + tool_definitions;
        #[allow(clippy::cast_possible_truncation)]
        let pressure = if available > 0 {
            (f64::from(total_used) / f64::from(available)) as f32
        } else {
            1.0
        };

        BudgetAllocation {
            system_prompt,
            rag_context,
            tool_definitions,
            history: history_budget,
            response_headroom,
            requires_compaction,
            pressure: pressure.min(1.0),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::TokenOptimizationConfig;

    fn default_budget() -> TokenBudget {
        TokenBudget::new(&TokenOptimizationConfig::default())
    }

    #[test]
    fn empty_conversation_no_compaction() {
        let budget = default_budget();
        let estimate = ConversationTokenEstimate {
            system_prompt: 0,
            summary: 0,
            history: 0,
            total: 0,
        };
        let alloc = budget.allocate(&estimate, false, 0);
        assert!(!alloc.requires_compaction);
        assert!(alloc.pressure < f32::EPSILON);
    }

    #[test]
    fn response_headroom_always_reserved() {
        let budget = default_budget();
        let estimate = ConversationTokenEstimate {
            system_prompt: 100,
            summary: 0,
            history: 6000,
            total: 6100,
        };
        let alloc = budget.allocate(&estimate, false, 0);
        // 8192 * 0.25 = 2048
        assert_eq!(alloc.response_headroom, 2048);
    }

    #[test]
    fn heavy_history_triggers_compaction() {
        let budget = default_budget();
        // Available = 8192 - 2048 = 6144
        // System prompt cap = 6144 * 0.15 = 921
        // System prompt actual = 100 → 100
        // History budget = 6144 - 100 = 6044
        // Compaction threshold = 6044 * 0.70 = 4230
        // History actual 5000 > 4230 → compaction required
        let estimate = ConversationTokenEstimate {
            system_prompt: 100,
            summary: 0,
            history: 5000,
            total: 5100,
        };
        let alloc = budget.allocate(&estimate, false, 0);
        assert!(alloc.requires_compaction);
    }

    #[test]
    fn tools_reduce_history_budget() {
        let budget = default_budget();
        let estimate = ConversationTokenEstimate {
            system_prompt: 100,
            summary: 0,
            history: 2000,
            total: 2100,
        };
        let without_tools = budget.allocate(&estimate, false, 0);
        let with_tools = budget.allocate(&estimate, true, 5);
        assert!(with_tools.history < without_tools.history);
        assert_eq!(with_tools.tool_definitions, 5 * TOKENS_PER_TOOL_ESTIMATE);
    }

    #[test]
    fn pressure_increases_with_usage() {
        let budget = default_budget();
        let low = ConversationTokenEstimate {
            system_prompt: 50,
            summary: 0,
            history: 200,
            total: 250,
        };
        let high = ConversationTokenEstimate {
            system_prompt: 200,
            summary: 100,
            history: 5000,
            total: 5300,
        };
        let low_alloc = budget.allocate(&low, false, 0);
        let high_alloc = budget.allocate(&high, false, 0);
        assert!(high_alloc.pressure > low_alloc.pressure);
    }

    #[test]
    fn system_prompt_capped_at_budget_ratio() {
        let budget = default_budget();
        // Available = 6144, cap = 6144 * 0.15 = 921
        let estimate = ConversationTokenEstimate {
            system_prompt: 2000, // Way over cap
            summary: 0,
            history: 100,
            total: 2100,
        };
        let alloc = budget.allocate(&estimate, false, 0);
        // System prompt should be capped at 921
        assert!(alloc.system_prompt <= 922); // Allow 1 for rounding
        assert!(alloc.system_prompt < 2000);
    }

    #[test]
    fn pressure_priority_low_pressure_same_as_base() {
        let budget = default_budget();
        let estimate = ConversationTokenEstimate {
            system_prompt: 100,
            summary: 0,
            history: 500,
            total: 600,
        };
        let base = budget.allocate(&estimate, true, 3);
        let priority = budget.allocate_with_pressure_priority(&estimate, true, 3);
        assert_eq!(base, priority);
    }

    #[test]
    fn pressure_priority_system_prompt_protected() {
        let budget = default_budget();
        // Push pressure above 0.9: available = 6144, total+tools >> 6144
        let estimate = ConversationTokenEstimate {
            system_prompt: 800,
            summary: 200,
            history: 5000,
            total: 6000,
        };
        let alloc = budget.allocate_with_pressure_priority(&estimate, true, 10);
        // System prompt should still get its full allocation (≤ cap)
        assert!(alloc.system_prompt >= 800);
        // History is most constrained
        assert!(alloc.requires_compaction);
    }

    #[test]
    fn pressure_priority_tools_capped_under_pressure() {
        let budget = default_budget();
        let estimate = ConversationTokenEstimate {
            system_prompt: 500,
            summary: 500,
            history: 5000,
            total: 6000,
        };
        let base = budget.allocate(&estimate, true, 20);
        let priority = budget.allocate_with_pressure_priority(&estimate, true, 20);
        // Under pressure, tools should be capped more aggressively
        assert!(priority.tool_definitions <= base.tool_definitions);
    }

    // ========================================================================
    // allocate_adaptive tests
    // ========================================================================

    #[test]
    fn adaptive_no_rag_boosts_system_prompt_cap() {
        let budget = default_budget();
        let estimate = ConversationTokenEstimate {
            system_prompt: 1500, // Exceeds default 15% cap (921) but fits 30% cap (1843)
            summary: 0,
            history: 500,
            total: 2000,
        };
        let with_rag = budget.allocate_adaptive(&estimate, true, false, 0);
        let without_rag = budget.allocate_adaptive(&estimate, false, false, 0);

        // Without RAG, system prompt cap is 15% + 15% = 30%
        // So more of the system prompt survives
        assert!(
            without_rag.system_prompt > with_rag.system_prompt,
            "Without RAG, system prompt cap should be higher: {} vs {}",
            without_rag.system_prompt,
            with_rag.system_prompt
        );
    }

    #[test]
    fn adaptive_no_rag_rag_context_is_zero() {
        let budget = default_budget();
        let estimate = ConversationTokenEstimate {
            system_prompt: 100,
            summary: 0,
            history: 500,
            total: 600,
        };
        let alloc = budget.allocate_adaptive(&estimate, false, false, 0);
        assert_eq!(alloc.rag_context, 0);
    }

    #[test]
    fn adaptive_with_rag_preserves_rag_budget() {
        let budget = default_budget();
        let estimate = ConversationTokenEstimate {
            system_prompt: 100,
            summary: 0,
            history: 500,
            total: 600,
        };
        let alloc = budget.allocate_adaptive(&estimate, true, false, 0);
        assert!(alloc.rag_context > 0);
        // Should equal the standard 15% cap
        assert_eq!(
            alloc.rag_context,
            budget.allocate(&estimate, false, 0).rag_context
        );
    }

    #[test]
    fn adaptive_no_rag_gives_more_history() {
        let budget = default_budget();
        let estimate = ConversationTokenEstimate {
            system_prompt: 100,
            summary: 0,
            history: 3000,
            total: 3100,
        };
        let with_rag = budget.allocate_adaptive(&estimate, true, false, 0);
        let without_rag = budget.allocate_adaptive(&estimate, false, false, 0);

        // History should be the same here because system_prompt actual is
        // below both caps (15% and 30%). History = available - system_prompt.
        assert_eq!(without_rag.history, with_rag.history);
    }

    #[test]
    fn adaptive_matches_allocate_when_rag_present_no_tools() {
        let budget = default_budget();
        let estimate = ConversationTokenEstimate {
            system_prompt: 100,
            summary: 0,
            history: 500,
            total: 600,
        };
        let adaptive = budget.allocate_adaptive(&estimate, true, false, 0);
        let base = budget.allocate(&estimate, false, 0);
        // When RAG is present and no tools, adaptive should match base
        assert_eq!(adaptive.system_prompt, base.system_prompt);
        assert_eq!(adaptive.rag_context, base.rag_context);
        assert_eq!(adaptive.history, base.history);
    }
}