strands/context_manager/strategies/offload/mod.rs
1//! Offload strategies: reduce content in the context window by dropping it, truncating it to a
2//! preview or summarizing it.
3
4mod base;
5pub(crate) mod conditions;
6mod drop;
7mod summarize;
8pub(crate) mod target;
9mod truncate;
10
11pub use base::EmergencyTruncateStrategy;
12pub use conditions::{ConditionValue, OffloadConditions, OffloadConditionsBuilder};
13pub use drop::DropStrategy;
14pub use summarize::{SummarizeConfig, SummarizeConfigBuilder, SummarizeStrategy};
15pub use target::OffloadTarget;
16pub use truncate::TruncateStrategy;
17
18use super::deferred_tool_loading::{DeferConditions, DeferStrategy, DeferTarget};
19use crate::context_manager::methods::truncate::TruncateConfig;
20use crate::error::Result;
21
22/// Builds the offload strategies (`Offload`). Each returns a strategy; chain `when` to set when and
23/// on what it acts.
24///
25/// Target [`OffloadTarget::All`] acts on whole messages; any other target acts on each block.
26/// `block_threshold` measures whichever the target acts on, and `context_utilization` only gates
27/// when the strategy fires. Targets `All` and `Media` need conditions from `when`; any other target
28/// without them acts on each matching block as it arrives, before the model reads it.
29///
30/// ```
31/// use strands::context_manager::{
32/// Offload, OffloadConditions, OffloadTarget, SummarizeConfig, TruncateConfig,
33/// };
34///
35/// // Per block, eagerly: truncate each tool result over 1,500 tokens to a 750-token preview.
36/// let truncate = Offload::truncate(
37/// OffloadTarget::ToolResults,
38/// TruncateConfig { preview_tokens: Some(750.0), ..Default::default() },
39/// )?
40/// .when(OffloadConditions { block_threshold: Some(1500.into()), ..Default::default() })?;
41/// // Whole messages: summarize the oldest at 70% context utilization, keeping the newest four.
42/// let summarize = Offload::summarize(OffloadTarget::All, SummarizeConfig::default())?.when(OffloadConditions {
43/// context_utilization: Some("70%".into()),
44/// preserve_recent: Some(4.into()),
45/// ..Default::default()
46/// })?;
47/// // Whole messages: once the window is full, drop the oldest, keeping the newest 70%.
48/// let drop = Offload::drop(OffloadTarget::All)?.when(OffloadConditions {
49/// context_utilization: Some("100%".into()),
50/// preserve_recent: Some("70%".into()),
51/// ..Default::default()
52/// })?;
53/// # let _ = (truncate, summarize, drop);
54/// # Ok::<(), strands::Error>(())
55/// ```
56#[derive(Debug, Clone, Copy)]
57#[non_exhaustive]
58pub struct Offload;
59
60impl Offload {
61 /// Drops matching content from the context window entirely (`Offload.drop`). Without `when`
62 /// conditions, drops each block on arrival.
63 ///
64 /// # Errors
65 ///
66 /// A tool list target that is empty or excludes every tool it includes.
67 pub fn drop(target: OffloadTarget) -> Result<DropStrategy> {
68 DropStrategy::new(target, None)
69 }
70
71 /// Replaces oversized content with a preview (`Offload.truncate`). Without `when` conditions,
72 /// truncates each block on arrival.
73 ///
74 /// # Errors
75 ///
76 /// A tool list target that is empty or excludes every tool it includes.
77 pub fn truncate(target: OffloadTarget, config: TruncateConfig) -> Result<TruncateStrategy> {
78 TruncateStrategy::new(target, config, None)
79 }
80
81 /// Replaces content with a model-written summary (`Offload.summarize`). Without `when`
82 /// conditions, summarizes each block on arrival.
83 ///
84 /// # Errors
85 ///
86 /// A tool list target that is empty or excludes every tool it includes.
87 pub fn summarize(target: OffloadTarget, config: SummarizeConfig) -> Result<SummarizeStrategy> {
88 SummarizeStrategy::new(target, config, None)
89 }
90
91 /// Keeps tools' input schemas out of the context until the model loads them with
92 /// `load_tools` (`Offload.defer`). The model still sees each deferred tool's name and
93 /// description. Without [`when`](DeferStrategy::when) conditions, defers at any tool count;
94 /// either of its conditions is enough. One per context manager, the `deferredToolLoading`
95 /// preset included.
96 ///
97 /// # Errors
98 ///
99 /// A [`DeferTarget::Tools`] list that is empty, lists `toolSpecs`, or excludes every tool it
100 /// includes.
101 pub fn defer(target: DeferTarget) -> Result<DeferStrategy> {
102 DeferStrategy::new(target, DeferConditions::default())
103 }
104}