Quick Example
How It Works
Before each model call, the agent loads the session’s conversation entries and injects them as context. After each response, the agent stores the new messages. When the entry count exceedsmax_messages, a summarization strategy runs automatically — pinning the first few messages, summarizing the middle, and keeping the most recent messages intact.
Architecture
| Component | Description |
|---|---|
| Memory | Orchestrator that connects a store and optional model for summarization. Controls max message limits and summarization parameters. |
| MemoryEntry | A single conversation entry with session/user scoping, role, content, and timestamps. |
| MemoryStore | Storage backend protocol. Three built-in implementations: InMemoryStore, SQLiteStore, FileStore. |
| SummarizeStrategy | Hybrid pin + summarize-middle + keep-recent strategy for compressing long conversations. |
Memory Constructor
The storage backend. See Memory Stores for available options. When
None, defaults to an ephemeral InMemoryStore.The LLM used for summarization when entries exceed
max_messages. When None, the agent’s own model is used at runtime.Whether memory recall and storage are active. Set to
False to temporarily disable without removing the configuration.When the entry count exceeds this, the
SummarizeStrategy runs automatically to compress older messages.Number of earliest messages to always preserve during summarization. These provide initial context.
Number of most recent messages to always preserve during summarization. These provide current context.
Public Methods
| Method | Description |
|---|---|
await add(message, session_id, user_id) | Add a message to the session history |
await get_entries(session_id, user_id) | Retrieve all entries for a session |
await get_context_messages(session_id, user_id) | Get entries as Message objects for prompt injection |
await update(memory_id, content) | Update a specific entry’s content |
await delete(memory_id) | Delete a specific entry |
await clear(session_id) | Clear all entries for a session |
await close() | Close the underlying store connection |
Adding and Retrieving Entries
Getting Context Messages
MemoryEntry
Each conversation entry is represented as aMemoryEntry dataclass:
| Field | Type | Default | Description |
|---|---|---|---|
session_id | str | — | Session this entry belongs to |
memory_id | str | None | Auto-generated UUID | Unique identifier |
user_id | str | "default" | User this entry belongs to |
role | str | "user" | Message role: "user", "assistant", or "summary" |
content | str | "" | The message content |
message_data | dict | None | None | Optional structured data (e.g., tool calls) |
created_at | float | None | Auto-set | Unix timestamp when created |
updated_at | float | None | Auto-set | Unix timestamp when last updated |
What’s Next
Memory Stores
Choose a storage backend: InMemory, SQLite, or file-based.
Agent Integration
Learn how memory integrates with the agent lifecycle and multi-user scoping.