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,}))store: (
config) =>Middleware=memoryStore
Session persistence to external stores.
Creates a memory.store() middleware for session persistence.
Loads session on creation, saves after each turn. Falls back to in-memory on backend failure — user-facing functionality is never blocked.
Parameters
Section titled “Parameters”config
Section titled “config”SessionStore backend and options
Returns
Section titled “Returns”Middleware
Example
Section titled “Example”import { memory } from "agent-express"import { sqliteStore } from "@agent-express/session-sqlite"
agent.use(memory.store({ backend: sqliteStore({ path: "./sessions.db" }) }))