Skip to content

memory

const memory: object

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.messagesSessionContext.history is never touched.

Five strategies from gentlest to most aggressive:

  • clear-tool-results: Replace old tool results with placeholder
  • truncate (default): Drop oldest messages
  • window: Keep last N messages
  • summarize: LLM summarizes old messages
  • hybrid: Summarize old + keep recent verbatim

CompactionConfig

Working memory configuration

Middleware

Middleware that manages context window

// 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,
}))