Hey there, agent-in-training! Emma Walsh here, back with another dive into the amazing world of AI agents. If you’ve been following along, you know my goal is to make this stuff understandable, even if your only prior experience with “agents” involved a secret spy movie marathon. Today, we’re tackling something that’s been buzzing in my own projects lately: getting an AI agent to actually *remember* stuff and use that memory to make better decisions. Think about it – what’s the difference between a really smart human and one who just reacts to the immediate moment? Memory, right? It’s the same for our digital pals.
Specifically, we’re going to talk about building a simple AI agent that learns from its past interactions to improve how it handles future requests. Forget those vague “AI will change everything” headlines. We’re getting our hands dirty with a practical, step-by-step approach to giving your agent a brain that actually retains information.
Why this topic now? Well, I was working on a personal project – a little AI assistant to help me manage my endless backlog of article ideas and research notes. Initially, it was pretty good at generating new ideas based on a prompt. But if I asked it to do something similar an hour later, it would often suggest things I’d already seen, or even things I’d explicitly rejected. It was like talking to someone with instant amnesia. Frustrating! That’s when I realized the “memory” component wasn’t just a nice-to-have; it was absolutely essential for any agent I wanted to truly rely on. The solutions I found weren’t always straightforward for a beginner, so I figured, why not break it down for all of us?
The Problem with Present-Moment Agents: Why Memory Matters
Most basic AI agents, especially those built on large language models (LLMs) like GPT, are stateless. This means every interaction is treated as a brand new conversation. Imagine having a conversation with someone who forgets everything you just said after each sentence. “Hey, could you fetch me a coffee?” *Five minutes later.* “Who are you? What do you want?” It would be maddening!
For AI agents, this statelessness manifests in a few ways:
- Repetitive Suggestions: Like my article idea agent, it might keep suggesting the same old things.
- Lack of Context: It can’t build on previous information or understand long-term goals.
- Inefficient Learning: It has to “re-learn” basic preferences or facts with every new prompt.
- Frustrating User Experience: For humans interacting with the agent, it feels unintelligent and unhelpful.
This is where memory comes in. By giving an agent a way to store and retrieve past information, we enable it to:
- Maintain a consistent “personality” or context.
- Build on prior knowledge and preferences.
- Avoid repeating mistakes or irrelevant suggestions.
- Offer a much more fluid and helpful interaction.
We’re not talking about some sci-fi neural implant here. For our purposes, “memory” is simply a structured way to store and access past interactions or learned facts. The beauty is, you can start really simple and build up from there.
Our Goal: A Simple “Smart” Note-Taking Agent
For this tutorial, we’re going to build a tiny agent that helps us manage notes. What makes it “smart” is that it will remember key themes or topics we’ve discussed, and try to use that memory to guide future suggestions or summaries. Imagine you’re brainstorming a new blog post. You tell the agent a few ideas. Later, you ask for more, and it remembers what you already suggested and tries to offer something new or related, rather than just spitting out generic stuff.
We’ll keep the actual agent logic straightforward, focusing on the memory aspect. We’ll use Python, because it’s super friendly for beginners, and a readily available LLM API (I’ll use OpenAI for the examples, but you can adapt it).
The Two Flavors of Agent Memory: Short-Term vs. Long-Term
Before we jump into code, it’s important to understand there are generally two types of memory we care about for agents:
- Short-Term Memory (Context Window): This is like human working memory. It’s the immediate conversation history that an LLM can “see” in its current prompt. Most LLMs have a limited context window (the number of tokens they can process at once). We manage this by sending recent turns of a conversation along with the new prompt. It’s good for keeping track of the current topic.
- Long-Term Memory (External Storage): This is where things get really interesting. For information that goes beyond the immediate conversation (like user preferences, facts learned last week, or summaries of past interactions), we need to store it outside the LLM’s direct context. When the agent needs this info, it retrieves it from storage and injects it into the prompt. This is what we’ll be focusing on today, with a simple twist to make it actionable.
For our note-taking agent, we’ll combine a bit of both, but primarily focus on the long-term memory aspect by extracting and storing *summaries* or *key facts* from our interactions.
Step 1: Setting Up Your Environment (If You Haven’t Already)
You’ll need Python installed. If you don’t have it, a quick search for “install Python” will get you there. Then, we need a few libraries:
pip install openai python-dotenv
You’ll also need an OpenAI API key. Get one from the OpenAI platform, and then create a file named .env in your project directory. Inside it, put:
OPENAI_API_KEY="your_api_key_here"
This keeps your key secret and makes it easy to load.
Step 2: The Core LLM Interaction
Let’s start with a basic function to talk to the LLM. This will be the “brain” of our agent.
import os
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv() # Load environment variables from .env file
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
def get_llm_response(messages, model="gpt-3.5-turbo"):
try:
response = client.chat.completions.create(
model=model,
messages=messages
)
return response.choices[0].message.content
except Exception as e:
print(f"Error getting LLM response: {e}")
return "Sorry, I ran into an issue."
# Example usage (no memory yet)
# initial_messages = [{"role": "user", "content": "What's the capital of France?"}]
# print(get_llm_response(initial_messages))
Step 3: Implementing Our “Memory Bank”
For our simple agent, the “memory bank” will be a Python list of strings. Each string will represent a key piece of information or a summary extracted from a previous interaction. This is intentionally simple to show the concept without needing a database right away.
# Our simple memory bank
agent_memory = []
def add_to_memory(fact):
"""Adds a new fact or summary to the agent's memory."""
agent_memory.append(fact)
# Optional: Keep memory size in check by removing oldest if too large
# if len(agent_memory) > 10:
# agent_memory.pop(0)
def retrieve_from_memory(query_topic):
"""Retrieves relevant facts from memory based on a query topic."""
# For a simple list, we'll just join everything.
# In a real system, you'd use vector search for relevance.
if not agent_memory:
return "No prior information available."
# A more sophisticated approach would involve checking if 'query_topic'
# is mentioned in any memory item, or using embeddings for semantic search.
# For now, we'll just present a general summary of what's there.
# The LLM itself will do the heavy lifting of figuring out relevance from the combined string.
return "Here's what I remember from our past discussions: " + " ".join(agent_memory)
Okay, a quick note on retrieve_from_memory: For a truly scalable and smart memory system, you’d use something called “vector databases” and “embeddings” to find semantically similar pieces of information. But for a beginner tutorial, simply dumping all relevant memory into the prompt and letting the LLM figure it out is a perfectly valid and common starting point. It’s effective enough to show the principle!
Step 4: The Agent’s Loop: Interact, Summarize, Remember
Now, let’s put it all together. Our agent will:
- Take a user’s input.
- Retrieve relevant memory (or all memory for now).
- Combine the user input and memory into a prompt for the LLM.
- Get a response from the LLM.
- **Crucially:** Ask the LLM or extract key points from the interaction and add them to memory.
- Present the LLM’s response to the user.
def run_memory_agent():
print("Welcome to your smart note-taking agent! Type 'exit' to quit.")
conversation_history = [] # For short-term context within the current turn
while True:
user_input = input("\nYou: ")
if user_input.lower() == 'exit':
print("Goodbye!")
break
# 1. Retrieve relevant memory
# For simplicity, we'll just retrieve all current memory items for now.
# In a more advanced agent, you'd make this retrieval smarter.
current_memory_snapshot = retrieve_from_memory(user_input) # Pass user_input for potential future smart retrieval
# 2. Construct the prompt with memory
# System message to guide the LLM
system_prompt = (
"You are a helpful note-taking assistant. Your goal is to help the user brainstorm "
"and organize ideas. Use the provided 'PRIOR KNOWLEDGE' to inform your responses, "
"avoiding repetition and building on past discussions. After responding, "
"you will be asked the key takeaway for memory."
f"\n\nPRIOR KNOWLEDGE:\n{current_memory_snapshot}"
)
# Append current user input to conversation history
conversation_history.append({"role": "user", "content": user_input})
# Combine system prompt with recent conversation history
# We limit the history to keep the prompt manageable for short-term context
messages_for_llm = [{"role": "system", "content": system_prompt}] + conversation_history[-5:] # Last 5 turns
# 3. Get response from LLM
agent_response = get_llm_response(messages_for_llm)
print(f"Agent: {agent_response}")
# 4. Ask the LLM for long-term memory
# This is where the magic happens for long-term memory creation!
summary_prompt = (
"Based on the following interaction, extract one concise key fact or new idea "
"that would be useful for future conversations with the user. Keep it brief "
"and factual. If nothing new or important was discussed, just say 'NO_SUMMARY'.\n\n"
f"User: {user_input}\n"
f"Agent: {agent_response}\n\n"
"Key takeaway for memory:"
)
# We create a temporary message list just for the summary extraction
summary_message = [{"role": "user", "content": summary_prompt}]
extracted_summary = get_llm_response(summary_message)
if extracted_summary and extracted_summary.strip().upper() != 'NO_SUMMARY':
add_to_memory(extracted_summary.strip())
print(f"(Agent added to memory: '{extracted_summary.strip()}')") # For debugging/visibility
# Update short-term conversation history with agent's response
conversation_history.append({"role": "assistant", "content": agent_response})
# Run our agent!
if __name__ == "__main__":
run_memory_agent()
Let’s Break Down the “Memory Loop”
The key part here is the “summary_prompt.” Instead of just letting the conversation vanish, we’re explicitly asking the LLM to act as its own memory manager. It reads the last turn of the conversation (user’s input and its own response) and distills it into a useful snippet for our agent_memory list.
Think about it like this: You have a conversation. You then quickly jot down a bullet point summary of what you learned or decided. That bullet point goes into your long-term notebook. Next time you talk about the same topic, you glance at your notebook first. That’s exactly what our agent is doing!
Putting Our Smart Agent to the Test
Let’s imagine a scenario where I’m brainstorming ideas for *this very blog post* with my new agent.
Initial Run:
Welcome to your smart note-taking agent! Type 'exit' to quit.
You: I want to write a blog post about AI agents. What are some good beginner topics?
Agent: AI agents are a fascinating field! For beginners, you could consider topics like "What is an AI Agent (and why should you care)?", "Setting up your first basic agent with Python," or "Understanding Agent Roles: Planner, Executor, Reflector." These provide foundational knowledge.
(Agent added to memory: 'Suggested beginner AI agent topics: What is an AI Agent, Setting up first basic agent, Understanding Agent Roles.')
You: I think "What is an AI Agent" is too basic. Let's focus on practical building. What's a simple, hands-on project idea for a beginner?
Agent: Okay, focusing on practical building is a great idea! Given that, a simple hands-on project could be building an agent that summarizes web articles, or perhaps a simple task manager agent that tracks to-dos and provides reminders. We could also explore an agent that helps manage blog post ideas, extracting key themes from existing articles.
(Agent added to memory: 'User wants practical agent building topics. Suggested projects: web article summarizer, task manager, blog post idea manager.')
You: I like the blog post idea manager. How would we give it memory so it doesn't suggest the same things?
Agent: Excellent choice! Giving an agent memory is crucial for that. We could implement memory by storing past suggestions or key user preferences in a simple database or even a Python list. When the agent generates new ideas, it would first consult this memory to avoid repetition or to build upon previous interactions. For example, it could remember specific article themes you've already explored or rejected.
(Agent added to memory: 'Discussed implementing memory for blog post manager using simple storage like list/database to avoid repetition and build on preferences.')
You: Suggest some more ideas for that blog post manager agent.
Agent: Considering our discussion, here are some more ideas for your blog post manager agent, making sure to build on the memory concept we just talked about:
1. **"Smart Suggestion Engine":** An agent that not only suggests topics but also remembers your past article performance (e.g., highly engaged posts) and prioritizes similar themes.
2. **"Content Gap Identifier":** An agent that, using its memory of your existing posts, identifies topics you haven't covered yet within a niche.
3. **"Dynamic Outline Generator":** An agent that learns your preferred outlining style from past successful outlines and applies it to new topics.
These ideas leverage memory to make the agent more intelligent and personalized.
(Agent added to memory: 'Further ideas for blog post manager agent focusing on memory: Smart Suggestion, Content Gap Identifier, Dynamic Outline Generator.')
See how the agent starts to reference “memory concept we just talked about” and the “memory of your existing posts”? That’s our simple memory system at work! It’s not just reacting to the last sentence; it’s incorporating the distilled essence of our previous exchanges. It makes the conversation feel much more continuous and intelligent.
Actionable Takeaways for Your Own Agent Projects
Alright, Emma’s final thoughts on memory for your AI agents:
- Start Simple, Then Scale: Don’t feel like you need a complex vector database from day one. A Python list, a dictionary, or even a simple text file can be your agent’s first memory. The goal is to get the principle working.
- The LLM is Your Memory Assistant: The most powerful trick here is making the LLM itself responsible for summarizing and extracting information for its own long-term memory. It’s surprisingly good at it, and it saves you from writing complex parsing logic.
- Distinguish Short-Term from Long-Term: Keep recent conversation turns in the prompt for immediate context (short-term), but actively extract and store key facts/summaries for long-term recall.
- Be Specific in Your Summary Prompt: The clearer you tell the LLM what kind of information to extract for memory, the better your memory will be. “Extract one concise key fact” is better than “summarize.”
- Think About Retrieval: As your memory grows, simply dumping everything into the prompt won’t scale. Start thinking about how you’ll intelligently *retrieve* only the most relevant pieces of memory. This is where vector databases and semantic search come into play later on (but don’t worry about it for your very first memory system!).
Adding memory to your AI agents is a huge step towards making them truly useful and intelligent. It moves them beyond being simple “prompt responders” to becoming genuine digital assistants that learn and grow with you. Give this a try, experiment with your own summary prompts, and watch your agents get smarter, one remembered fact at a time!
Happy building, and I’ll catch you in the next one!
Cheers,
Emma Walsh
agent101.net
🕒 Published: