Introduction
One of the biggest limitations of traditional AI applications is their inability to remember information across conversations. Most Large Language Models (LLMs) operate within a limited context window and forget previous interactions once a session ends.
For example, a user might tell an AI assistant:
My preferred programming language is C#.
Later, in a new conversation:
What programming language do I prefer?
Without memory, the AI cannot answer correctly.
This limitation becomes even more significant in enterprise applications, customer support systems, personal assistants, and autonomous AI agents that need to maintain context over days, weeks, or even months.
Long-term memory enables AI agents to store, retrieve, and use relevant information from previous interactions. By combining Semantic Kernel with PostgreSQL, developers can create AI agents that learn from past experiences and provide more personalized and context-aware responses.
In this article, you'll learn how long-term memory works, how Semantic Kernel supports memory-driven workflows, and how PostgreSQL can be used as a scalable memory store for AI agents.
Why AI Agents Need Long-Term Memory
Most AI systems operate like this:
User Question
|
v
LLM
|
v
Response
The model only sees information provided in the current prompt.
Once the conversation ends:
Context Lost
This creates several challenges:
Repeated questions
Lack of personalization
Poor user experience
Inconsistent responses
Limited reasoning across sessions
Long-term memory solves these issues.
Understanding AI Memory
Human memory consists of different types of information.
AI memory can be modeled similarly.
Short-Term Memory
Stores information during the current session.
Example:
Current Conversation
Long-Term Memory
Stores information across sessions.
Example:
User Preferences
Past Conversations
Learned Facts
Long-term memory allows agents to retain important information over time.
Types of Long-Term Memory
Different memory categories can be stored.
User Preferences
Example:
Preferred Language: C#
Preferred Cloud: Azure
Historical Conversations
Example:
Previous Support Requests
Business Knowledge
Example:
Company Policies
Internal Documentation
Agent Experiences
Example:
Past Decisions
Successful Workflows
These memory types improve agent performance.
Memory Architecture
A typical memory architecture looks like this:
User Interaction
|
v
Semantic Kernel
|
v
Memory Service
|
v
PostgreSQL
|
v
Memory Retrieval
The database stores information while Semantic Kernel orchestrates retrieval and usage.
Why PostgreSQL?
PostgreSQL is a popular choice for AI memory systems.
Benefits include:
Open source
Highly scalable
Reliable
Cloud-friendly
Supports vector storage
Strong indexing capabilities
It works well for both traditional data and AI-related workloads.
Memory Workflow
A typical workflow looks like this:
User Message
|
v
Extract Facts
|
v
Store Memory
|
v
Future Query
|
v
Retrieve Memory
|
v
Generate Response
The agent continuously learns from interactions.
Creating a Memory Model
Let's start with a simple memory entity.
public class MemoryRecord
{
public Guid Id { get; set; }
public string UserId { get; set; }
= string.Empty;
public string Content { get; set; }
= string.Empty;
public DateTime CreatedAt
{
get;
set;
}
}
This model stores user-related memories.
Creating the PostgreSQL Database
A simple memory table might look like this:
CREATE TABLE MemoryRecords
(
Id UUID PRIMARY KEY,
UserId TEXT,
Content TEXT,
CreatedAt TIMESTAMP
);
The table stores memory entries for future retrieval.
Configuring Entity Framework Core
Install PostgreSQL support.
dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL
This package enables PostgreSQL integration with .NET applications.
Creating the DbContext
Define a database context.
public class MemoryDbContext
: DbContext
{
public MemoryDbContext(
DbContextOptions<
MemoryDbContext> options)
: base(options)
{
}
public DbSet<MemoryRecord>
MemoryRecords => Set<MemoryRecord>();
}
The DbContext provides access to stored memories.
Registering the Database
Configure PostgreSQL in Program.cs.
builder.Services.AddDbContext<
MemoryDbContext>(
options =>
options.UseNpgsql(
configuration
.GetConnectionString(
"MemoryDatabase")));

Join the conversation! Your thoughts help the community grow.