Context is the most expensive and most valuable resource in AI. If your agent is reset every time you close the tab, it's not an assistant; it's a calculator.
In this guide, we'll walk through a robust architecture for contextual memory using n8n and Supabase.
1. The Trigger
Every workflow starts with a webhook or a chat trigger. We capture the user_id to fetch the correct memory partition.
2. Retrieval-Augmented Generation (RAG)
We don't just dump the history into the prompt. We use Semantic Search:
- Embeddings: OpenAI
text-embedding-3-small - Vector DB: Supabase
pgvector
// Generic vector search function in n8n
const memory = await db.query(
"SELECT content FROM memories WHERE user_id = $1 ORDER BY embedding <=> $2 LIMIT 5",
[userId, queryEmbedding],
);
3. The Feedback Loop
Every time the agent responds, we summarize the interaction and store it back. This creates a recursive loop of "learning."
"The difference between a tool and a system is the feedback loop."
Stay building.