Skip to content

Memory Manager

The Memory Manager provides agents with persistent context across conversations through three distinct memory types, each with pluggable backends and configurable retention strategies. It lives in astromesh/core/memory.py.

┌─────────────────────────────────────────────────────┐
│ MemoryManager │
│ │
│ ┌───────────────┐ ┌──────────────┐ ┌─────────────┐ │
│ │ Conversational│ │ Semantic │ │ Episodic │ │
│ │ │ │ │ │ │ │
│ │ Chat history │ │ Vector │ │ Event logs │ │
│ │ (turns) │ │ embeddings │ │ (actions) │ │
│ │ │ │ (similarity) │ │ │ │
│ └───────┬───────┘ └──────┬───────┘ └──────┬──────┘ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌─────────┐ ┌───────────┐ ┌──────────┐ │
│ │ Backend │ │ Backend │ │ Backend │ │
│ └─────────┘ └───────────┘ └──────────┘ │
└─────────────────────────────────────────────────────┘
Memory TypeWhat It StoresAccess PatternPurpose
ConversationalUser and assistant message turns for the current sessionSequential (recent history)Maintain conversation continuity within a session
SemanticVector embeddings of past interactions and documentsSimilarity search (nearest neighbor)Recall relevant past information across sessions
EpisodicStructured event logs (tool calls, decisions, outcomes)Filtered query (by type, time, agent)Track what the agent did and why, audit trail

Each memory type supports multiple storage backends:

BackendIdentifierDescription
In-memorymemoryDictionary-based, lost on restart. Good for development
RedisredisPersistent across restarts, TTL support, shared across instances
PostgreSQLpostgresFull durability, SQL queryable, suitable for production
BackendIdentifierDescription
In-memorymemorySimple cosine similarity, no persistence
ChromaDBchromaEmbedded vector database, file-based persistence
PostgreSQL + pgvectorpgvectorProduction vector store with SQL integration
BackendIdentifierDescription
In-memorymemoryList-based, lost on restart
PostgreSQLpostgresDurable event log with timestamp indexing

Strategies control how conversational memory is trimmed to fit within the model’s context window.

StrategyBehaviorConfiguration
sliding_windowKeep only the last N turns. Oldest turns are droppedwindow_size: 20
summaryWhen history exceeds the threshold, compress older turns into a summary using the LLMsummary_threshold: 30
token_budgetKeep as many recent turns as fit within a token limit. Counts tokens and drops oldest turns firstmax_tokens: 8000
spec:
memory:
conversational:
backend: redis
strategy: sliding_window
window_size: 20
semantic:
backend: chroma
collection: "agent_memory"
episodic:
backend: postgres

build_context(agent_name, session_id, query)

Section titled “build_context(agent_name, session_id, query)”

Assembles context from all configured memory types into a single context object for prompt rendering.

async def build_context(
agent_name: str,
session_id: str,
query: str,
) -> MemoryContext

Steps:

  1. Load conversational history for the session from the backend
  2. Apply the configured strategy (sliding_window, summary, or token_budget) to trim history
  3. If semantic memory is enabled, embed the current query and retrieve top-k similar past entries
  4. If episodic memory is enabled, retrieve recent events relevant to the agent/session
  5. Return a MemoryContext combining all three

MemoryContext fields:

FieldTypeDescription
historylist[Message]Trimmed conversational turns
semantic_resultslist[SemanticResult]Relevant past interactions (text + similarity score)
episodeslist[Episode]Recent event log entries

persist_turn(agent_name, session_id, user_message, assistant_message)

Section titled “persist_turn(agent_name, session_id, user_message, assistant_message)”

Stores a completed user/assistant exchange in all configured memory backends.

async def persist_turn(
agent_name: str,
session_id: str,
user_message: str,
assistant_message: str,
metadata: dict | None = None,
) -> None

Steps:

  1. Append user and assistant messages to conversational memory
  2. If semantic memory is enabled, embed the exchange and store the vector
  3. If episodic memory is enabled, log a turn_completed event with metadata (tokens used, tools called, latency)

Delete all conversational history for a session.

async def clear_history(
agent_name: str,
session_id: str,
) -> None

Full memory configuration example:

spec:
memory:
conversational:
backend: redis
strategy: token_budget
max_tokens: 8000
redis:
url: "redis://localhost:6379/0"
ttl: 86400
semantic:
backend: chroma
collection: "my_agent_memory"
top_k: 5
chroma:
path: "./data/chroma"
episodic:
backend: postgres
postgres:
dsn: "postgresql://user:pass@localhost:5432/astromesh"
FieldRequiredDefaultDescription
conversational.backendNomemoryStorage backend for chat history
conversational.strategyNosliding_windowRetention strategy
conversational.window_sizeNo20Turns to keep (sliding_window strategy)
conversational.summary_thresholdNo30Turn count that triggers summarization (summary strategy)
conversational.max_tokensNo8000Token budget limit (token_budget strategy)
semantic.backendNoBackend for vector memory. Omit to disable semantic memory
semantic.collectionNo{agent_name}_memoryVector store collection name
semantic.top_kNo5Number of similar results to retrieve
episodic.backendNoBackend for event logs. Omit to disable episodic memory