rig-memory
Conversation memory policies for the Rig agent framework.
rig-core ships the ConversationMemory trait and an in-process
InMemoryConversationMemory backend. This crate provides reusable named
policies for shaping loaded history before it is sent to the model:
- [
NoopMemoryPolicy] — identity policy, useful as a default. - [
SlidingWindowMemory] — keep at most the most recentNmessages. - [
TokenWindowMemory] — keep the most recent messages that fit within a token budget supplied by a [TokenCounter].
Both window policies remove the leading prefix through any tool-result
messages whose assistant calls were truncated. Results are detected anywhere
in a user message, including after text and across intervening system messages.
Cleanup stops at the first retained assistant; later paired exchanges remain
intact. Whole removed messages are included in apply_with_demoted's demoted
prefix in original order, including accompanying text, so demotion hooks lose
no content.
Usage
use ;
let memory = new
.with_filter;
For backends other than InMemoryConversationMemory, apply a policy directly:
use ;
let policy = last_messages;
let trimmed = policy.apply?;
To wrap any backend with a policy and propagate policy errors to the caller
(rather than silently degrading to identity on failure), use PolicyMemory:
use ;
let memory = new;