Skip to main content

Module memory

Module memory 

Source
Expand description

Memory: manages the agent’s context.

The inference loop uses Memory to store conversation history and to retrieve the full context for each request. This module provides two ready-to-use implementations:

  • InMemoryMemory — stores all messages verbatim, never trims;
  • WindowMemory — trims to a recent window against a budget, with pluggable token counting and trim strategies.

Compaction, retrieval, persistence and similar strategies plug in by implementing Memory (or injecting a TrimStrategy into WindowMemory) without touching the inference loop. A built-in summarization strategy is provided as SummarizeStrategy.

Structs§

Budget
Window budget: token and round limits (both optional; when both are set, the smaller window wins).
CharTokenCounter
Default counter: CJK characters count as 1 token each, other characters count as 1 token per 4 characters (rounded up).
InMemoryMemory
In-memory implementation: stores all messages verbatim.
SummarizeStrategy
Summarization trim strategy: over-budget old messages → summarizer → one System summary message.
TrimResult
Output of a trim strategy: the trimmed message sequence + how the result is handled.
WindowDrop
Default trim strategy: drops the earliest complete rounds until the remaining sequence fits the budget.
WindowMemory
Window memory: stores all messages; context() trims to the budget on retrieval.

Enums§

MemoryError
Context access failed.

Traits§

Memory
Manages the agent’s context: decides which messages the model sees on each turn.
TokenCounter
Counts the token number of a text.
TrimStrategy
Trim strategy: decides “how to trim” — window dropping, summarization, LLM compaction, etc. Users can inject custom implementations.