AI Shortcuts

AI Shortcuts

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

Ardit Sulce's avatar
Ardit Sulce
Nov 27, 2025
∙ Paid

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 agents

  • langchain-google-genai: Connects to Gemini

  • python-dotenv: Secure credential storage

  • langgraph-checkpoint-postgres: Saves conversation history to PostgreSQL

  • psycopg2-binary: PostgreSQL database connector

Install with:

pip install -r requirements.txt

Step 2: Setting Up Supabase

Create a Supabase Project:

  1. Go to supabase.com and sign up

  2. Click “New Project”

  3. Name it (e.g., “agent-memory”)

  4. Create a strong database password (save it!)

  5. Choose a region and click “Create”

Wait 2-3 minutes for setup ☕

Get Your Connection String:

  1. Go to Project Settings (gear icon)

  2. Click “Database” in left menu

  3. Find “Connection string” section

  4. Select “URI” tab

  5. Copy the string: postgresql://postgres:[YOUR-PASSWORD]@[PROJECT-REF].supabase.co:5432/postgres

  6. Replace [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.

This post is for paid subscribers

Already a paid subscriber? Sign in
© 2025 Ardit Sulce
Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture