Memory Matters
Without memory, your agent is a goldfish — it forgets everything between sessions. With memory, it learns from every interaction and gets better over time. Here is how to implement both types.
Two Types of Memory
Agents need two kinds of memory, just like humans. Short-term memory holds the current conversation. Long-term memory persists across sessions.
The conversation history. Each message and tool result from the current session. Lost when the session ends.
messages array you send to Claude on every turn.Persistent storage — a database, vector store, or file. Survives across sessions, users, and restarts.
Short-Term Memory in Code
Short-term memory is simply the conversation history you pass to the API. Each turn adds to the array:
messages = []
# Turn 1: User asks a question
messages.append({"role": "user", "content": "What is my account status?"})
# Turn 1: Agent responds + uses a tool
response = client.messages.create(messages=messages, ...)
messages.append({"role": "assistant", "content": response.content})
# Turn 2: User follows up — agent remembers Turn 1
messages.append({"role": "user", "content": "Can you upgrade it?"})
# Claude now sees the full history — it knows "it" = the account
When the messages array gets too long, it overflows the context window. When the session ends, the array is gone. The agent starts the next session knowing nothing about what happened before.
Long-Term Memory in Code
Long-term memory uses a database to persist knowledge across sessions. Here is a complete implementation using Supabase:
supabase = create_client(url, key)
def save_memory(key, value, category="general"):
"""Save a fact to long-term memory"""
supabase.table("agent_memory").upsert({
"key": key,
"value": value,
"category": category,
"updated_at": "now()"
}).execute()
def recall_memory(key):
"""Retrieve a fact from long-term memory"""
result = supabase.table("agent_memory").select("value")
.eq("key", key).execute()
return result.data[0]["value"] if result.data else None
def search_memory(category):
"""Search all memories in a category"""
result = supabase.table("agent_memory").select("*")
.eq("category", category)
.order("updated_at", desc=True).execute()
return result.data
def agent_with_memory(user_message):
# Boot: load relevant memories into context
past_interactions = search_memory("customer_issues")
user_prefs = recall_memory("user.preferences")
# Include memories in the system prompt
context = f"""You have the following memories:
Past issues: {past_interactions}
User preferences: {user_prefs}"""
response = client.messages.create(
system=SYSTEM_PROMPT + "\n" + context,
...
)
# After resolving: save what was learned
save_memory(
"resolution.login_failure",
"Cache clearing does not fix login issues. Root cause is usually expired OAuth token.",
category="customer_issues"
)
This lesson is for Pro members
Unlock all 520+ lessons across 52 courses with Academy Pro.
Already a member? Sign in to access your lessons.