Sliding window memory
The sliding window trimmer keeps an admin's MessageHistory from
growing unbounded. After every turn, it drops the oldest messages so
the window never exceeds the configured size. The system prompt is
always preserved.
MessageHistory lives in LogicGrid.Core.
Use it
using LogicGrid.Core.Admins;
using LogicGrid.Core.Llm;
using LogicGrid.Core.Memory;
var llm = LlmClientBase.Ollama("llama3.2");
var admin = new GroupChatAdmin<string, string>(
name: "Chat",
llmClient: llm,
agents: new IAgent[] { ... });
var trimmer = new SlidingWindowTrimmer(maxMessages: 20);
trimmer.AttachTo(admin);
// run normally — the trimmer is invisible
var result = await admin.RunAsync("...");
How it works
The trimmer subscribes to admin events. Whenever a message is added to
MessageHistory, it checks the count. If the count exceeds maxMessages,
it removes messages from the start of the history (keeping the system
prompt) until the count is back at the cap.
When to use it
✅ Use it when:
- You're running a long multi-turn conversation in a single admin run.
- Cost or context-window pressure matters.
- The conversation is recent-context-heavy (the LLM only needs to remember the last few turns).
❌ Don't use it when:
- Single-turn or short runs (no history to trim).
- The agent needs all earlier context to be correct. Use a longer window, or retrieve relevant turns from the full history with a RAG pipeline — see RAG over conversation history.
Configuration
var trimmer = new SlidingWindowTrimmer(
maxMessages: 30,
keepSystemPrompt: true, // default
keepFirstUserMessage: true // default — preserves the original task
);
keepFirstUserMessage is on by default because the original task is
often referenced later in the conversation, and dropping it leads to
context drift.
Per-agent vs per-admin
This trimmer applies to admin message history. Agent message history is
already scoped to a single RunAsync call — see Agents
for the why.
If you need durable, role-scoped memory across runs, see Semantic memory.