Scaling Conversational Memory: Implementing Epistemic Recall in Cronix
At Cronix, our AI-powered WhatsApp agent needed a way to remember context across disparate sessions without incurring expensive external API costs or bloating our infrastructure. We recently implemented an episodic memory layer that allows our agent to "remember" previous interactions, providing a more coherent user experience while maintaining strict multi-tenant isolation.
The Architecture of Recall
To ensure our memory layer remained maintainable and testable, we adopted a Hexagonal Architecture (Ports and Adapters) pattern. By defining clear interfaces, we decoupled the agent logic from the specific storage and embedding implementation.
We established three core abstractions:
interface IEmbedder {
embed(text: string): Promise<number[]>;
}
interface IEpisodicStore {
save(memory: MemoryEntry): Promise<void>;
recall(query: string, tenantId: string): Promise<MemoryEntry[]>;
}
interface IMemoryEngine {
remember(context: string): Promise<void>;
retrieve(query: string): Promise<string[]>;
}
By keeping these interfaces separate, we can swap out our Supabase-backed vector store or our embedding service without ever touching the core agent logic.
Database Strategy
We utilized pgvector within Supabase to handle the high-dimensional vector data. To optimize performance, we moved from an unindexed approach to using an HNSW index, which provided sub-50ms recall times.
CREATE TABLE ai_memories_v2 (
id UUID PRIMARY KEY,
business_id UUID NOT NULL,
embedding vector(384),
content TEXT,
expires_at TIMESTAMP WITH TIME ZONE
);
-- Indexing for fast vector similarity search
CREATE INDEX idx_memories_hnsw ON ai_memories_v2
USING hnsw (embedding vector_cosine_ops);
Crucially, our SQL functions perform tenant filtering before the vector scan occurs, ensuring that one client's data never leaks into another client's search space.
The Cross-Runtime Challenge
Because we operate in both Node.js (for our primary service) and Deno (within Supabase Edge Functions), maintaining memory parity was a priority. We implemented a shared memory schema and a robust suite of tests using Vitest. These tests verify byte-parity for our serialized objects, ensuring that data stored by the agent in one environment is correctly interpreted by the other.
The Takeaway
By leveraging vector similarity search instead of generic database queries, we transformed our WhatsApp agent from a stateless responder into a context-aware assistant. If you are building agentic systems, treat your memory layer as a swappable interface rather than a tightly coupled database dependency. Start by defining your IEmbedder and IEpisodicStore interfaces before writing any implementation code; it makes testing against mock data significantly easier.
Generated with Gitvlg.com