Python / Python Modern Generative AI and Agents Interview Questions
How do you add conversation memory to an LLM application with LangChain?
LLMs are stateless — each API call is independent and the model has no memory of previous exchanges. Maintaining conversation context requires explicitly including past messages in the current prompt. LangChain provides memory abstractions that manage this history, automatically appending it to the messages sent to the LLM.
The most practical pattern in modern LangChain is to pass MessagesPlaceholder in the prompt template and maintain a list of messages externally. For longer conversations, the history must be trimmed or summarised to stay within the context window — raw storage of all messages eventually exceeds token limits.
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
from langchain_core.output_parsers import StrOutputParser
llm = ChatOpenAI(model='gpt-4o-mini', temperature=0.7)
prompt = ChatPromptTemplate.from_messages([
SystemMessage(content='You are a helpful assistant.'),
MessagesPlaceholder(variable_name='history'), # slot for past messages
('human', '{input}'),
])
chain = prompt | llm | StrOutputParser()
# Maintain history externally
history = []
def chat(user_input: str) -> str:
response = chain.invoke({'input': user_input, 'history': history})
history.append(HumanMessage(content=user_input))
history.append(AIMessage(content=response))
return response
print(chat('My name is Alice.'))
print(chat('What is my name?')) # correctly recalls 'Alice'
# Trim history to last N messages to avoid context overflow
from langchain_core.messages import trim_messages
def chat_with_trim(user_input: str, max_tokens: int = 4000) -> str:
trimmed = trim_messages(
history,
max_tokens=max_tokens,
token_counter=llm,
strategy='last', # keep most recent messages
include_system=True,
)
response = chain.invoke({'input': user_input, 'history': trimmed})
history.append(HumanMessage(content=user_input))
history.append(AIMessage(content=response))
return response
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
