Skip to main content

Memory

Trait Memory 

Source
pub trait Memory: Send + Sync {
    // Required methods
    fn record<'life0, 'async_trait>(
        &'life0 mut self,
        message: Message,
    ) -> Pin<Box<dyn Future<Output = Result<(), MemoryError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn context<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<Message>, MemoryError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided method
    fn record_protected<'life0, 'async_trait>(
        &'life0 mut self,
        message: Message,
    ) -> Pin<Box<dyn Future<Output = Result<(), MemoryError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
}
Expand description

Manages the agent’s context: decides which messages the model sees on each turn.

The inference loop interacts with Memory in only two ways:

  • hands each turn’s new messages (user input, assistant replies, tool results) to record / record_protected;
  • fetches the full context via context before each request and passes it to the Provider.

How messages are stored, trimmed, and compacted is up to the implementation; the inference loop is unaware of it. For example, WindowMemory trims to a budget on retrieval and guarantees that a tool message and its Assistant message stay paired; no implementation should split that pair (message interface constraint). Send + Sync ensures Box<dyn Memory> can be held across await points in agent implementations.

Required Methods§

Source

fn record<'life0, 'async_trait>( &'life0 mut self, message: Message, ) -> Pin<Box<dyn Future<Output = Result<(), MemoryError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Records a message, appending it to the end of the session context.

§Errors

Returns MemoryError when storing or counting fails (e.g., a remote counting API is unavailable).

Source

fn context<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Vec<Message>, MemoryError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Returns the full message sequence of the current context (in order of occurrence), which the agent hands to the Provider as-is.

The returned value is a clone; mutating it does not affect Memory’s internal state.

§Errors

Returns MemoryError when reading from storage or trimming fails.

Provided Methods§

Source

fn record_protected<'life0, 'async_trait>( &'life0 mut self, message: Message, ) -> Pin<Box<dyn Future<Output = Result<(), MemoryError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Records a protected message: implementations must not remove it when trimming.

Used for persistent behavior guidance such as skill bodies — trimming one silently degrades behavior (the model keeps running but loses the dedicated instruction, with no visible error). The default implementation behaves like record; implementations that trim should override it to exempt protected messages.

§Errors

Same as record.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§