Memory means carrying useful information from earlier turns or earlier workflow steps. In chat apps, memory may include recent messages. In business workflows, state may include selected documents, user role, tool results, approvals, or partial outputs.
The key is to store the right information in the right place. Do not blindly append the entire conversation forever. Summarize, trim, retrieve, and separate durable user profile data from temporary chat context.
Short-term memory keeps the current conversation coherent. Long-term memory stores durable facts or preferences. Workflow state tracks intermediate steps in a multi-step process.
Memory can leak sensitive information, increase token cost, and preserve incorrect assumptions. Treat memory as data with privacy, retention, and correction rules.
This pattern keeps chat history outside the model and injects it only when invoking the chain.
from langchain_core.chat_history import InMemoryChatMessageHistory
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_openai import ChatOpenAI
store = {}
def get_history(session_id: str):
if session_id not in store:
store[session_id] = InMemoryChatMessageHistory()
return store[session_id]
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful programming mentor."),
MessagesPlaceholder(variable_name="history"),
("human", "{question}"),
])
chain = prompt | ChatOpenAI(model="gpt-4o-mini", temperature=0.2)
chat = RunnableWithMessageHistory(
chain,
get_history,
input_messages_key="question",
history_messages_key="history",
)
config = {"configurable": {"session_id": "user-123"}}
print(chat.invoke({"question": "What is RAG?"}, config=config).content)
print(chat.invoke({"question": "Give me one use case for it."}, config=config).content)
messages = [
{'role': 'user', 'content': 'first'},
{'role': 'assistant', 'content': 'answer one'},
{'role': 'user', 'content': 'second'},
{'role': 'assistant', 'content': 'answer two'},
{'role': 'user', 'content': 'latest question'},
]
window = messages[-3:]
print([message['content'] for message in window])
['second', 'answer two', 'latest question']
No. Memory usually stores conversation or user state. RAG retrieves external knowledge from documents or databases.
Yes. Old, irrelevant, or incorrect memory can distract the model and produce poor answers.
Keep only what the product needs, summarize older context, and apply a clear retention policy.
Finish the concept here, then reinforce it with hands-on coding, interview prep, or a tool that matches the topic.
Explore 500+ free tutorials across 20+ languages and frameworks.
Fresh tutorials, interview guides, and coding practice in your inbox.