pub struct ContextBudget {
pub max_tokens: usize,
pub warning_ratio: f32,
pub keep_recent_turns: usize,
pub max_tool_result_chars: usize,
}Expand description
上下文预算配置。
控制 Agent Loop 中消息历史的 Token 上限与压缩行为。
Fields§
§max_tokens: usize消息历史的最大 Token 数(默认 128k)。
v0.1: 固定默认值 128k,适用于大多数模型
v0.2: 从 ResolvedModel.context_window 自动推导(window * 0.8)
warning_ratio: f32达到此占比时触发压缩(默认 0.8 = 80%)
keep_recent_turns: usize压缩时保留最近多少个 Turn(默认 5)
max_tool_result_chars: usize单条工具结果的最大字符数(默认 4096)
Implementations§
Source§impl ContextBudget
impl ContextBudget
Sourcepub fn should_compact(&self, current_tokens: usize) -> bool
pub fn should_compact(&self, current_tokens: usize) -> bool
判断是否需要压缩。
Sourcepub fn truncate_tool_result(&self, text: String) -> String
pub fn truncate_tool_result(&self, text: String) -> String
截断单条工具结果,防止单条响应撑爆上下文。
Sourcepub fn truncate_tool_result_blocks(
&self,
content: &[ContentBlock],
) -> Vec<ContentBlock>
pub fn truncate_tool_result_blocks( &self, content: &[ContentBlock], ) -> Vec<ContentBlock>
截断 ContentBlock 列表中的文本内容(用于非流式路径)。
Trait Implementations§
Source§impl Clone for ContextBudget
impl Clone for ContextBudget
Source§fn clone(&self) -> ContextBudget
fn clone(&self) -> ContextBudget
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ContextBudget
impl Debug for ContextBudget
Auto Trait Implementations§
impl Freeze for ContextBudget
impl RefUnwindSafe for ContextBudget
impl Send for ContextBudget
impl Sync for ContextBudget
impl Unpin for ContextBudget
impl UnsafeUnpin for ContextBudget
impl UnwindSafe for ContextBudget
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more