Add Memory to LangChain Agents for Context‑Aware Multi‑Turn Conversations
This guide walks through adding ConversationBufferMemory to a LangChain agent, covering tool creation, memory setup, agent initialization with OpenAI function calling, prompt inspection, configuration tweaks using agent_kwargs, and best‑practice considerations for maintaining context in multi‑turn AI conversations.
0 Introduction
When building complex AI applications, giving an agent memory is essential for maintaining context across multiple turns. This article explains how to add memory to a LangChain agent, covering the required components, code snippets, and best practices.
Core components of an agent with memory
LangChain agents with memory rely on three components: Tools, Memory, and a Large Language Model (LLM). Tools execute specific tasks, Memory stores and retrieves conversation history, and the LLM interprets inputs and generates responses.
1 Build tools for the agent
Define the tools the agent can use, such as a web‑search tool (SerpAPIWrapper) and a calculator tool (LLMMathChain).
# Build a search tool
search = SerpAPIWrapper()
# Build a calculator tool
llm_math_chain = LLMMathChain(llm=llm, verbose=True)
tools = [
Tool(name="Search", func=search.run,
description="useful for when you need to answer questions about current events or the current state of the world"),
Tool(name="Calculator", func=llm_math_chain.run,
description="useful for when you need to answer questions about math"),
]
print(tools)2 Add a memory component
Import and instantiate ConversationBufferMemory, specifying the key that stores the chat history and enabling return_messages=True so the agent receives message objects.
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory(
memory_key="chat_history",
return_messages=True
)3 Define the agent
Initialize the agent with the tools, LLM, and the memory component. Use AgentType.OPENAI_FUNCTIONS to enable OpenAI function calling.
from langchain.agents import AgentType, initialize_agent
agent_chain = initialize_agent(
tools,
llm,
agent=AgentType.OPENAI_FUNCTIONS,
verbose=True,
handle_parsing_errors=True,
memory=memory
) AgentType.OPENAI_FUNCTIONS: leverages OpenAI function calling. verbose=True: prints detailed execution logs. handle_parsing_errors=True: automatically handles parsing errors. memory=memory: injects the previously created memory component.
4 Inspect the default prompt template
Printing agent_chain.agent.prompt.messages reveals the system message, human prompt, and AI response templates.
print(agent_chain.agent.prompt.messages)
print(agent_chain.agent.prompt.messages[0])
print(agent_chain.agent.prompt.messages[1])
print(agent_chain.agent.prompt.messages[2])5 Optimize the agent configuration
Pass agent_kwargs with extra_prompt_messages placeholders for chat_history and agent_scratchpad. This ensures the agent can access prior conversation turns and its own reasoning steps.
agent_chain = initialize_agent(
tools,
llm,
agent=AgentType.OPENAI_FUNCTIONS,
verbose=True,
handle_parsing_errors=True,
agent_kwargs={
"extra_prompt_messages": [
MessagesPlaceholder(variable_name="chat_history"),
MessagesPlaceholder(variable_name="agent_scratchpad")
],
},
memory=memory
) agent_kwargs: customizes the prompt. chat_history: inserts stored dialogue. agent_scratchpad: inserts the agent’s intermediate reasoning.
6 Verify the updated prompt
Re‑print the prompt messages to confirm that the new placeholders appear.
print(agent_chain.agent.prompt.messages)
print(agent_chain.agent.prompt.messages[0])
print(agent_chain.agent.prompt.messages[1])
print(agent_chain.agent.prompt.messages[2])7 Conclusion
By following these steps, a LangChain agent gains memory, allowing it to retain context across multi‑turn interactions and improve performance on complex tasks. In production, developers should balance memory depth with latency, possibly implementing pruning or summarization strategies.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JavaEdge
First‑line development experience at multiple leading tech firms; now a software architect at a Shanghai state‑owned enterprise and founder of Programming Yanxuan. Nearly 300k followers online; expertise in distributed system design, AIGC application development, and quantitative finance investing.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
