Giving Agents Memory and Conversation State
Add short-term and long-term memory to LangChain agents so they remember context across turns and produce coherent multi-step conversations.
Giving Agents Memory and Conversation State is a free AI Agents with LangChain & Autonomous Workflows lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents with LangChain & Autonomous Workflows learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Agents Need Memory
LLMs are stateless: each call knows nothing about the last unless you tell it. Without memory, an agent forgets your name the instant you say it.
The Context Window
Memory ultimately means stuffing prior info into the context window. That window is finite, so the real challenge is deciding what to keep and what to drop.
Buffer Memory
Buffer memory stores the whole conversation and replays it every turn. Accurate, but it grows without bound and eventually overflows the context window.
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
memory.save_context({'input': 'Hi, I am Lena'}, {'output': 'Hello Lena!'})Windowed Memory
Windowed memory keeps only the last N exchanges. It bounds size and forgets older context — perfect when only recent turns matter.
from langchain.memory import ConversationBufferWindowMemory
memory = ConversationBufferWindowMemory(k=4)Summary Memory
Summary memory periodically condenses older turns with the LLM, keeping the gist plus recent messages — preserving meaning in a small footprint.
from langchain.memory import ConversationSummaryMemory
memory = ConversationSummaryMemory(llm=llm)Short-Term vs Long-Term
Two flavors serve different needs: short-term memory holds the current chat in the prompt, while long-term persists facts across sessions in a store.
Long-Term Memory with Vectors
For knowledge that must survive sessions, store messages as embeddings in a vector store and retrieve the most relevant ones by similarity instead of replaying everything.
from langchain.memory import VectorStoreRetrieverMemory
memory = VectorStoreRetrieverMemory(retriever=vectorstore.as_retriever())Wiring Memory into a Chain
Wire memory into a conversation chain. Each call loads prior context, runs the model, and saves the new exchange automatically.
from langchain.chains import ConversationChain
chain = ConversationChain(llm=llm, memory=memory)
print(chain.predict(input='What is my name?'))Session and User Scoping
Real apps serve many users at once. Scope memory by session or user id so separate conversations never leak into each other.
store = {}
def get_memory(session_id):
if session_id not in store:
store[session_id] = ConversationBufferMemory()
return store[session_id]Cost and Privacy Tradeoffs
More memory means more tokens — higher cost and latency. Long-term stores may hold sensitive data, so mind retention limits and what you're allowed to keep.
Choosing a Memory Strategy
Choosing a strategy: buffer or window for short chats, summary for long ones, vector for cross-session facts — always scoped per user and mindful of cost.
Quick Check
You've met several memory types — which fits when? Time to put it to the test.
Recap
Recap: memory feeds prior context into a finite window. Buffer, window, and summary trade accuracy for size, vector stores enable long-term recall — always scope and watch cost.
Frequently asked questions
Is the “Giving Agents Memory and Conversation State” lesson free?
Yes — the full text of “Giving Agents Memory and Conversation State” is free to read here on the web, and the AI Agents with LangChain & Autonomous Workflows course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents with LangChain & Autonomous Workflows course, upgrade to CoddyKit PRO.
What will I learn in “Giving Agents Memory and Conversation State”?
Add short-term and long-term memory to LangChain agents so they remember context across turns and produce coherent multi-step conversations. You practise AI Agents with LangChain & Autonomous Workflows with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Agents with LangChain & Autonomous Workflows?
No prior experience is required. AI Agents with LangChain & Autonomous Workflows on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Giving Agents Memory and Conversation State” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Agents with LangChain & Autonomous Workflows lesson?
Yes. Every AI Agents with LangChain & Autonomous Workflows lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Understanding AI Agents & LLMs
- LangChain Core Components Explained
- Building Your First Simple Agent
- Giving Agents Memory and Conversation State