AI / LangGraph LangChain Interview questions II
How do you implement conversation memory?
The recommended modern approach for conversation memory uses RunnableWithMessageHistory which wraps an LCEL chain and automatically loads and saves message history per session ID from a configurable store — without any manual history tracking in application code.
from langchain_core.chat_history import BaseChatMessageHistory from langchain_community.chat_message_histories import ChatMessageHistory from langchain_core.runnables.history import RunnableWithMessageHistory from langchain_openai import ChatOpenAI from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder # In-memory store (swap for Redis, DynamoDB etc. in production) store = {} def get_session_history(session_id: str) -> BaseChatMessageHistory: if session_id not in store: store[session_id] = ChatMessageHistory() return store[session_id] prompt = ChatPromptTemplate.from_messages([ ("system", "You are a helpful assistant."), MessagesPlaceholder("history"), ("human", "{input}"), ]) chain = prompt | ChatOpenAI() chain_with_memory = RunnableWithMessageHistory( chain, get_session_history, input_messages_key="input", history_messages_key="history", ) # session_id identifies the conversation thread chain_with_memory.invoke( {"input": "My name is Alice."}, config={"configurable": {"session_id": "alice-123"}}, ) chain_with_memory.invoke( {"input": "What is my name?"}, config={"configurable": {"session_id": "alice-123"}}, )
More Related questions...