Skip to Content

Memory: Short-Term and Long-Term

When to use

Decide what the agent remembers between turns — within a session (short-term) and across sessions (long-term).

Analogy

A waiter remembers your order this visit; your favourite restaurant remembers you across visits. Both kinds of memory matter.

Data-flow diagram

   short-term:  message buffer (last N turns) + summarisation
   long-term:   vector store of facts + profile + episodic

   runner = Agent(
     short_term=BufferMemory(k=20),
     long_term=VectorMemory(index),
   )

Deep explanation

Short-term memory is the conversation buffer (or its summary) within one session. Long-term memory persists across sessions — facts extracted from chats, user profiles, episodic events. LangChain has BufferMemory and VectorStoreRetrieverMemory; LangGraph lets you checkpoint state. The right pattern: short-term buffer for context, long-term vector store for recall.

Examples

Example 1

from langchain.memory import ConversationBufferWindowMemory
mem = ConversationBufferWindowMemory(k=20, return_messages=True)
mem.save_context({'input':'hi'}, {'output':'hello'})
print(mem.load_memory_variables({}))

ConversationBufferWindowMemory keeps the last K turns — bounded context size, easy to reason about.

Example 2

# long-term: extract facts and embed
from langchain_openai import OpenAIEmbeddings
from langchain.vectorstores import FAISS
emb = OpenAIEmbeddings()
vdb = FAISS.from_texts(['user prefers JSON', 'user timezone: UTC'], embedding=emb)
print(vdb.similarity_search('what does the user like?').pop().page_content)

VectorStoreRetrieverMemory extracts long-term facts by embedding and retrieving similar prior facts.

Example 3

# combined: windowed short + long-term retrieval
def assemble_context(history):
    recent = history[-20:]
    facts  = vdb.similarity_search(history[-1].content, k=3)
    return {'recent': recent, 'facts': [f.page_content for f in facts]}

Assemble both — recent chat plus retrieved facts — into one prompt for the model.

Common mistake

Storing the entire raw message history as long-term memory. Cost balloons; retrieval quality drops as vectors become saturated.

Key takeaway

Short-term is recent turns. Long-term is extracted facts, profiles, summaries — written to a vector store and retrieved on relevance. Combine for full context.

Production Failure Playbook

Failure scenario 1: memory-bloat

Failure scenario 2: cross-user-leak