Skip to main content

strands/context_manager/
mod.rs

1//! Strategy-driven context management.
2//!
3//! A [`ContextManager`] keeps an agent's conversation within its context window. It runs a
4//! pipeline of [`ContextStrategy`]s before each model call and on overflow, offloads content to an
5//! [`Archive`] the model can read back with the `retrieve_context` tool, and protects pinned
6//! messages. Set it with `AgentConfig::context_manager`:
7//!
8//! - [`ContextManagerStrategy::Auto`] truncates tool results over 1,500 tokens and summarizes at
9//!   85% context utilization;
10//! - [`ContextManagerStrategy::Agentic`] lets the model manage its context with the
11//!   `summarize_context`, `truncate_context` and `pin_context` tools and shows it its usage;
12//! - [`ContextManagerConfig`] builds a custom pipeline from [`Offload`] strategies and
13//!   [`StrategyPresetName`] presets.
14
15mod archive;
16mod content_rendering;
17#[allow(clippy::module_inception, reason = "mirrors context-manager/context-manager.ts")]
18mod context_manager;
19mod js_text;
20mod message_trimming;
21mod methods;
22pub(crate) mod modes;
23mod placeholder;
24mod presets;
25mod retrieval_tool;
26mod search;
27mod strategies;
28mod types;
29
30pub use archive::Archive;
31pub(crate) use context_manager::AgentArchive;
32pub use context_manager::{ContextManager, ContextManagerStrategy};
33pub use methods::truncate::{PreviewMode, TruncateConfig, TruncateConfigBuilder};
34pub use presets::{StrategyEntry, StrategyPresetName};
35pub use strategies::deferred_tool_loading::{DeferConditions, DeferConditionsBuilder, DeferStrategy, DeferTarget};
36pub use strategies::offload::{
37    ConditionValue, DropStrategy, EmergencyTruncateStrategy, Offload, OffloadConditions, OffloadConditionsBuilder,
38    OffloadTarget, SummarizeConfig, SummarizeConfigBuilder, SummarizeStrategy, TruncateStrategy,
39};
40pub use types::{
41    ArchiveConfig, ArchiveConfigBuilder, ContextManagerConfig, ContextManagerConfigBuilder, ContextState,
42    ContextStrategy,
43};
44
45#[cfg(test)]
46mod archive_integration_tests;
47#[cfg(test)]
48mod test_support;