Skip to content

03. Long-Term Memory

Category: Agent Memory Module: AI Agents Prerequisites: Agent Memory Overview, Short-Term Memory Difficulty: Intermediate

Note: Long-Term Memory (LTM) enables AI agents to retain important information across multiple conversations and sessions. Unlike Short-Term Memory, which exists only during the current interaction, Long-Term Memory persists over time, allowing agents to remember user preferences, historical interactions, domain knowledge, and previous experiences.


Overview

Imagine you use an AI assistant every day.

On Monday you say:

My preferred cloud platform is AWS.

On Friday you ask:

Recommend an architecture for my next project.

A normal LLM has forgotten Monday's conversation.

An AI agent with Long-Term Memory remembers your preference and recommends an AWS-based architecture.

Unlike Short-Term Memory, Long-Term Memory survives:

  • Session termination
  • Application restart
  • Server restart
  • User logout

It enables AI agents to become increasingly personalized and intelligent over time.


Why Long-Term Memory Matters

Without Long-Term Memory

Conversation 1


User:
I prefer Java.


Session Ends

──────────────────────────

Conversation 2


User:
Recommend a backend framework.


Agent:
What programming language do you use?

The agent asks the same questions repeatedly.


With Long-Term Memory

Conversation 1


Store User Preference


Database

──────────────────────────

Conversation 2


Retrieve User Preference


Recommend Spring Boot

Benefits

  • Personalized experiences
  • Persistent user preferences
  • Historical context
  • Reduced repetitive questions
  • Better recommendations
  • Continuous learning

High-Level Architecture

Unlike Short-Term Memory, Long-Term Memory is stored in persistent storage systems.

                        User
                     AI Agent
         ┌────────────────┼────────────────┐
         ▼                ▼                ▼
   User Profile      Episodic Memory   Semantic Memory
         │                │                │
         ▼                ▼                ▼
   PostgreSQL        MongoDB         Vector Database
                         LLM

Each storage system is optimized for a different type of memory.


Characteristics of Long-Term Memory

Feature Description
Lifetime Days, months, or years
Storage Persistent
Scope Multiple sessions
Speed Slower than Short-Term Memory
Purpose Store important information permanently

Unlike Short-Term Memory, Long-Term Memory is selective.

Only valuable information should be stored.


What Should Be Stored?

Long-Term Memory should contain information that remains useful over time.

User Preferences

Preferred Language


English

Preferred IDE


IntelliJ IDEA

Preferred Cloud


AWS

User Profile

Name

Company

Role

Location

Department

Previous Tasks

Generated Architecture


Completed


Saved


Can be reused later

Learned Facts

Customer always prefers PDF reports.


Store


Reuse later

Enterprise Knowledge

Policies

SOPs

Technical Documents

Architecture Decisions

Unlike RAG documents, this information can evolve based on user interactions.


What Should NOT Be Stored?

Not every interaction belongs in Long-Term Memory.

Avoid storing:

  • Temporary conversations
  • Intermediate reasoning
  • Tool execution logs
  • API responses
  • One-time requests
  • Sensitive information without encryption

Good memory is selective, not exhaustive.


Long-Term Memory Lifecycle

User Interaction
Extract Important Information
Validate
Store Permanently
Retrieve When Needed
Update

Unlike Short-Term Memory, Long-Term Memory is continuously refined rather than discarded.


How Long-Term Memory Works

User Request
Retrieve User Profile
Retrieve Previous Experiences
Retrieve Domain Knowledge
Combine Context
LLM
Update Memory

Every interaction can improve the agent's understanding of the user.


Implementation

Example 1 – Core Python

A simple implementation using a dictionary.

class LongTermMemory:

    def __init__(self):
        self.memory = {}

    def save(self, key, value):
        self.memory[key] = value

    def recall(self, key):
        return self.memory.get(key)


memory = LongTermMemory()

memory.save("preferred_cloud", "AWS")
memory.save("preferred_language", "Java")

print(memory.recall("preferred_cloud"))

Output

AWS

This demonstrates the basic idea of persistent key-value storage.


Example 2 – LangChain + Vector Store

Long-Term Memory can be implemented using a vector store for semantic retrieval.

from langchain.memory import VectorStoreRetrieverMemory

memory = VectorStoreRetrieverMemory(
    retriever=vector_store.as_retriever()
)

memory.save_context(
    {"input": "I prefer AWS for cloud deployments."},
    {"output": "Preference saved."}
)

print(memory.load_memory_variables({}))

Instead of searching by exact keys, the agent retrieves memories using semantic similarity.


Example 3 – Production Example (PostgreSQL)

Enterprise systems commonly store user profiles in relational databases.

import psycopg2

connection = psycopg2.connect(
    database="agent_db",
    user="postgres",
    password="password",
    host="localhost"
)

cursor = connection.cursor()

cursor.execute(
    """
    INSERT INTO user_preferences
    (user_id, preference_type, preference_value)
    VALUES (%s, %s, %s)
    """,
    (
        101,
        "preferred_cloud",
        "AWS"
    )
)

connection.commit()

cursor.close()
connection.close()

A production AI agent typically retrieves these preferences at the beginning of every new conversation to personalize its responses.


Enterprise Use Cases

Customer Support Agent

Stores customer preferences and historical interactions across multiple support sessions.

Example:

  • Preferred communication language
  • Purchased products
  • Previous support tickets
  • Frequently reported issues
Customer


Support Agent


Long-Term Memory


Customer Profile Database


LLM


Personalized Response

Software Engineering Assistant

Remembers developer preferences and previous projects.

Examples

  • Preferred programming language
  • Favorite framework
  • Coding standards
  • Previous architecture decisions
  • Frequently used libraries

Instead of asking the same questions every session, the assistant immediately adapts to the developer.


Enterprise Knowledge Assistant

Stores organization-specific knowledge that evolves over time.

Examples

  • Frequently accessed documents
  • Department preferences
  • Project history
  • Organizational terminology
  • Business workflows

Financial Assistant

Maintains persistent financial information.

Examples

  • Investment preferences
  • Risk profile
  • Budget goals
  • Loan history
  • Preferred financial products

Healthcare Assistant

Stores patient information securely for future consultations.

Examples

  • Allergies
  • Chronic conditions
  • Previous consultations
  • Preferred hospital
  • Long-term treatment plans

Sensitive healthcare information should always be encrypted and protected according to regulatory requirements.


Production Insight

Enterprise AI agents rarely implement Long-Term Memory as a single database.

Instead, different categories of memory are stored in specialized systems.

                      AI Agent
       ┌──────────────────┼──────────────────┐
       ▼                  ▼                  ▼
 User Profile      Historical Events    Semantic Knowledge
       │                  │                  │
       ▼                  ▼                  ▼
 PostgreSQL         MongoDB          Vector Database
                     Object Storage

A common enterprise architecture is:

Memory Category Recommended Storage
User Profile PostgreSQL
Historical Events MongoDB
Semantic Knowledge Vector Database
Large Files Amazon S3 / Azure Blob / GCS

Separating memory by purpose improves scalability, retrieval accuracy, and maintainability.


Architecture Decision

Scenario Recommended Storage
User Profile PostgreSQL
User Preferences PostgreSQL / MongoDB
Enterprise Knowledge Vector Database
Historical Interactions MongoDB
Large Documents Object Storage
Workflow Metadata Relational Database

Advantages

  • Personalizes AI interactions across sessions
  • Eliminates repetitive questions
  • Improves recommendations
  • Learns user behavior over time
  • Enables adaptive decision making
  • Supports enterprise personalization
  • Preserves historical context

Limitations

  • Additional storage infrastructure
  • Slower retrieval compared to Short-Term Memory
  • Requires memory validation
  • Privacy and compliance concerns
  • Higher maintenance cost
  • Risk of storing outdated information

Best Practices

  • Store only meaningful information.
  • Separate user profiles from semantic knowledge.
  • Encrypt sensitive data.
  • Implement memory versioning.
  • Regularly clean obsolete information.
  • Validate memories before storing.
  • Apply access control to sensitive records.
  • Continuously monitor retrieval quality.

Common Mistakes

❌ Saving every conversation permanently

❌ Using Long-Term Memory as a conversation log

❌ Storing temporary workflow state

❌ Ignoring GDPR or privacy regulations

❌ Mixing structured and unstructured memory

❌ Never updating outdated information


Framework Comparison

Framework Long-Term Memory Support
LangChain VectorStoreRetrieverMemory, Custom Memory
LangGraph Persistent State + External Storage
LlamaIndex Memory Modules + Vector Stores
CrewAI Persistent Agent Memory
OpenAI Agents SDK External Memory Integration

Interview Questions

What is Long-Term Memory in an AI Agent?

How does Long-Term Memory differ from Short-Term Memory?

What information should be stored permanently?

Why is PostgreSQL commonly used for user profiles?

Why are vector databases useful for semantic memory?

What challenges arise when Long-Term Memory becomes outdated?

How can enterprise AI systems protect sensitive Long-Term Memory?

Why should Long-Term Memory be selective rather than exhaustive?


Quick Revision

                    User
                 AI Agent
        ┌─────────────┼─────────────┐
        ▼             ▼             ▼
  User Profile   Historical Data   Knowledge
        │             │             │
        ▼             ▼             ▼
 PostgreSQL      MongoDB      Vector Database
                     LLM
            Personalized Response

Key Takeaways

  • Long-Term Memory enables AI agents to retain important information across multiple sessions.
  • It stores persistent information such as user profiles, preferences, historical interactions, and semantic knowledge.
  • Enterprise AI systems typically distribute Long-Term Memory across relational databases, document databases, vector databases, and object storage.
  • Long-Term Memory should be selective, continuously updated, and protected with appropriate security and privacy controls.
  • When combined with Short-Term Memory, Long-Term Memory enables AI agents to deliver personalized, context-aware, and intelligent user experiences.

References

  • LangChain Documentation – Memory
  • LangGraph Documentation – Persistent State
  • LlamaIndex Documentation – Memory Components
  • OpenAI Agents SDK Documentation
  • CrewAI Documentation

Next Note

04-working-memory.md

In the next note, we'll explore Working Memory, the temporary reasoning workspace used by AI agents during planning, tool execution, and decision-making. You'll learn how it differs from Short-Term and Long-Term Memory, how modern agent frameworks implement it, and why it is essential for complex multi-step workflows.

Enterprise AI Engineering Handbook
Building Production-Grade Enterprise AI Systems — One Chapter at a Time.