Back
13 min read

RAG Implementation Guide: Add Memory to Your AI Agents

How to give your AI agents long-term memory using Retrieval Augmented Generation and Supabase pgvector.

What is RAG?

RAG (Retrieval Augmented Generation) lets AI agents access your documents, knowledge base, and data to give accurate, context-aware answers instead of making things up.

Without RAG: AI only knows what was in its training data
With RAG: AI retrieves relevant info from YOUR data before answering

Implementation with Supabase

Step 1: Enable pgvector Extension

-- In Supabase SQL Editor
CREATE EXTENSION vector;

-- Create table for embeddings
CREATE TABLE documents (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  content TEXT,
  embedding vector(1536),
  metadata JSONB
);

Step 2: Generate & Store Embeddings

// Using OpenAI embeddings
import { OpenAIEmbeddings } from "langchain/embeddings"
import { createClient } from "@supabase/supabase-js"

const embeddings = new OpenAIEmbeddings()
const vector = await embeddings.embedQuery(text)

await supabase.from('documents').insert({
  content: text,
  embedding: vector,
  metadata: { source: 'docs', title: '...' }
})

Step 3: Similarity Search

-- Create function for similarity search
CREATE FUNCTION match_documents (
  query_embedding vector(1536),
  match_threshold float DEFAULT 0.78,
  match_count int DEFAULT 5
)
RETURNS TABLE (
  id UUID,
  content TEXT,
  similarity float
)
LANGUAGE plpgsql
AS $$
BEGIN
  RETURN QUERY
  SELECT
    documents.id,
    documents.content,
    1 - (documents.embedding <=> query_embedding) AS similarity
  FROM documents
  WHERE 1 - (documents.embedding <=> query_embedding) > match_threshold
  ORDER BY similarity DESC
  LIMIT match_count;
END;
$$;

Step 4: Use in n8n Workflow

In your n8n AI agent workflow:

  1. Convert user question to embedding
  2. Search Supabase for similar documents
  3. Pass relevant docs to LLM as context
  4. LLM generates answer using your data

Real Project: Company Knowledge Bot

Built for 50-person company to answer policy questions

  • Documents: 200+ company docs, policies, FAQs
  • Accuracy: 95% correct answers
  • Time saved: 10 hours/week HR team
  • Cost: $15/mo (Supabase + OpenAI embeddings)

Best Practices

  • ✓ Chunk documents into 500-1000 tokens for better retrieval
  • ✓ Store metadata (source, date, category) for filtering
  • ✓ Set similarity threshold to avoid irrelevant results
  • ✓ Refresh embeddings when documents change
  • ✓ Monitor embedding costs (OpenAI: $0.0001 per 1K tokens)

Need RAG Implementation?

Expert RAG and AI agent development with Supabase pgvector and LangChain.

Build My AI Agent →