Build AI Agents with Long Term Memory Using LangChain v1 and Supabase
A step-by-step guide on how to use the latest version of LangChain to build AI agents and store conversation history in a Supabase PostgreSQL database
Introduction
Today we’re building an AI agent that remembers your entire conversation using LangChain and Supabase. Even if you close the program and come back later, it remembers everything. That’s persistent memory.
What You’ll Build
An agent that:
Remembers entire conversation history
References previous messages
Stores conversations in Supabase
Maintains context across sessions
Runs in a continuous loop
Prerequisites
Python 3.8+
Google API key for Gemini (get it here)
Supabase account (sign up here)
Step 1: Installing Dependencies
Create requirements.txt:
langchain==1.0.3
langchain-google-genai==3.0.1
python-dotenv==1.2.1
langgraph-checkpoint-postgres==2.0.12
psycopg2-binary==2.9.9
What are these packages?
langchain: Main library for agentslangchain-google-genai: Connects to Geminipython-dotenv: Secure credential storagelanggraph-checkpoint-postgres: Saves conversation history to PostgreSQLpsycopg2-binary: PostgreSQL database connector
Install with:
pip install -r requirements.txt
Step 2: Setting Up Supabase
Create a Supabase Project:
Go to supabase.com and sign up
Click “New Project”
Name it (e.g., “agent-memory”)
Create a strong database password (save it!)
Choose a region and click “Create”
Wait 2-3 minutes for setup ☕
Get Your Connection String:
Go to Project Settings (gear icon)
Click “Database” in left menu
Find “Connection string” section
Select “URI” tab
Copy the string:
postgresql://postgres:[YOUR-PASSWORD]@[PROJECT-REF].supabase.co:5432/postgresReplace
[YOUR-PASSWORD]with your actual password
Example: If your password is MySecret123 and project ref is abc123:
postgresql://postgres:MySecret123@abc123.supabase.co:5432/postgres
Step 3: Environment Variables
Create .env:
GOOGLE_API_KEY=your_google_api_key_here
SUPABASE_DB_URI=your_supabase_connection_string_here
Replace with your actual credentials. Never share this file!
Step 4: Understanding the Code
Create agent_with_memory.py:
Imports and Setup
from langchain_google_genai import ChatGoogleGenerativeAI
from dotenv import load_dotenv
from langchain.agents import create_agent
from langgraph.checkpoint.postgres import PostgresSaver
import os
load_dotenv()
DB_URI = os.getenv(”SUPABASE_DB_URI”)
What’s happening: We import necessary libraries and load our Supabase connection string from the .env file.
Tool Functions
Now, it’s time to write two functions -one for getting the location of the user and one for getting the weather. The agent will call these functions if it reasons it’s good to do so based on the user’s query.


