pub struct ContextManager { /* private fields */ }Expand description
Manages context reduction for an agent’s conversation (ContextManager).
Before each model call it runs its strategy pipeline; on a context window overflow it runs
the pipeline in recovery mode and retries the call, up to three times. An emergency truncation
always runs last and fires only when the window still overflows after the configured strategies
have run. Offloaded content is archived, and the retrieve_context tool lets the model read it
back.
Configure it through AgentConfig::context_manager; with one present no separate
conversation manager is needed. Cheap to clone: clones are handles to the same manager and
compare equal; separately built managers never do.
use strands::Agent;
use strands::context_manager::ContextManagerStrategy;
use strands::testing::MockModel;
let agent = Agent::builder()
.model(MockModel::new().with_turn("Hi"))
.context_manager(ContextManagerStrategy::Auto)
.printer(false)
.build()?;Implementations§
Source§impl ContextManager
impl ContextManager
Sourcepub fn new(config: ContextManagerConfig) -> Result<Self>
pub fn new(config: ContextManagerConfig) -> Result<Self>
Sourcepub fn from_strategy(
strategy: Option<ContextManagerStrategy>,
) -> Result<Option<Self>>
pub fn from_strategy( strategy: Option<ContextManagerStrategy>, ) -> Result<Option<Self>>
Resolves a strategy value to a manager (ContextManager.from): an instance as is, a new
manager for a preset or a configuration, and None for Disabled or no value.
§Errors
A configuration whose presets do not resolve.
Sourcepub fn archive(&self) -> Option<Archive>
pub fn archive(&self) -> Option<Archive>
The archive, once the manager is attached to an agent and the archive is enabled (archive).
A manager shared by several agents gives each agent its own archive; this returns the one made for the agent attached last.
Sourcepub fn archive_is_durable(&self) -> bool
pub fn archive_is_durable(&self) -> bool
Whether the archive is on storage that outlives the process, so session snapshots need not embed its data. For a shared manager, of the agent attached last.
Trait Implementations§
Source§impl Clone for ContextManager
impl Clone for ContextManager
Source§impl Debug for ContextManager
impl Debug for ContextManager
impl Eq for ContextManager
Source§impl From<ContextManager> for ContextManagerStrategy
impl From<ContextManager> for ContextManagerStrategy
Source§fn from(manager: ContextManager) -> Self
fn from(manager: ContextManager) -> Self
Source§impl PartialEq for ContextManager
impl PartialEq for ContextManager
Source§impl Plugin for ContextManager
impl Plugin for ContextManager
Source§fn get_tools(&self) -> Vec<ToolSource>
fn get_tools(&self) -> Vec<ToolSource>
init_agent:
anything that converts into a ToolSource, such as vec![MyTool.into()]. An MCP client
is rejected; pass it to the agent’s tools instead.Source§fn init_agent<'life0, 'life1, 'async_trait>(
&'life0 self,
agent: &'life1 mut LocalAgent,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn init_agent<'life0, 'life1, 'async_trait>(
&'life0 self,
agent: &'life1 mut LocalAgent,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Auto Trait Implementations§
impl !RefUnwindSafe for ContextManager
impl !UnwindSafe for ContextManager
impl Freeze for ContextManager
impl Send for ContextManager
impl Sync for ContextManager
impl Unpin for ContextManager
impl UnsafeUnpin for ContextManager
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more