Adding Memory and Context


This entry is part 5 of 8 in the series Building Agentic AI

Why Memory Matters

Without memory, an AI agent treats every interaction like it’s the first. It forgets past conversations, decisions, and data. This can limit usefulness for complex, multi-step goals. By adding memory, your agent can:

  • Recall previous steps in a workflow.
  • Learn from past mistakes or successes.
  • Personalize responses based on prior interactions.

Types of Memory

  • Short-Term Memory: Remembers information for the duration of a single session or conversation. Useful for tasks that span several steps but end once completed.
  • Long-Term Memory: Stores knowledge across sessions so the agent can improve over time. Ideal for ongoing projects, personal assistants, or research agents.

Vector Databases

Long-term memory often relies on a vector database — a specialized tool for storing and retrieving “embeddings,” which are numerical representations of text. Popular vector databases include:

  • Pinecone: Cloud-based, easy to integrate with LangChain.
  • Weaviate: Open-source, supports semantic search and knowledge graphs.
  • FAISS: Open-source from Facebook, lightweight for local use.

Simple LangChain Example with Memory

from langchain.memory import ConversationBufferMemory
from langchain_openai import ChatOpenAI
from langchain.chains import ConversationChain

# Create the model
llm = ChatOpenAI(model="gpt-4o", temperature=0)

# Create memory
memory = ConversationBufferMemory()

# Create a conversation chain with memory
conversation = ConversationChain(
    llm=llm,
    memory=memory,
    verbose=True
)

# Example interaction
conversation.predict(input="My name is Alex.")
conversation.predict(input="What is my name?")

In this example, the agent remembers that you introduced yourself as Alex — something a stateless chatbot would forget immediately.

Memory in Real Projects

For my SDG ecosystem project, an agent with long-term memory could track which organizations have been researched, what gaps have been identified, and which follow-up actions are pending — all without me needing to repeat instructions each time.

Coming Next

In the next post, I’ll show how to connect your agents to the outside world through APIs, web scraping, and knowledge bases — giving them the “senses” they need to gather and act on fresh information.

Building Agentic AI

Building with LangChain Connecting Agents to the World

Leave a Reply