memory
constmemory:object
Type Declaration
Section titled “Type Declaration”compaction
Section titled “compaction”compaction: (
config?) =>Middleware=memoryCompaction
Context window management with compaction strategies.
Creates a memory.compaction() middleware that manages the context window.
Automatically compacts messages before each LLM call when the token count
exceeds the configured limit. Only modifies ModelContext.messages —
SessionContext.history is never touched.
Five strategies from gentlest to most aggressive:
clear-tool-results: Replace old tool results with placeholdertruncate(default): Drop oldest messageswindow: Keep last N messagessummarize: LLM summarizes old messageshybrid: Summarize old + keep recent verbatim
Parameters
Section titled “Parameters”config?
Section titled “config?”Working memory configuration
Returns
Section titled “Returns”Middleware that manages context window
Example
Section titled “Example”// Simple truncation (default, zero cost)agent.use(memory.compaction({ maxTokens: 8192 }))
// Hybrid (best quality, one LLM call)agent.use(memory.compaction({ maxTokens: 8192, strategy: "hybrid", summaryModel: "anthropic/claude-haiku-4-5", keepRecentMessages: 10,}))