Adding Memory to AutoGen Agents
Free · Open source (MIT) · Works with LangChain, CrewAI, AutoGen · No signup
Your AutoGen agents lose all memory when they restart. Conversations, learned preferences, and accumulated context vanish between sessions. This breaks multi-session workflows where agents should remember previous interactions. Here's a one-class fix that adds persistent memory to any AutoGen agent without changing your existing code structure.
The Problem: AutoGen Agents Start Fresh Every Time
AutoGen's conversational agents are stateless by design. When your Python process ends, all conversation history disappears. The ConversableAgent class stores messages in memory only—there's no built-in persistence layer.
This creates real problems:
- Multi-session conversations break: Users expect agents to remember previous discussions
- Learned preferences vanish: An agent that learns a user's coding style forgets everything on restart
- Context switching fails: Agents can't reference earlier work or decisions across sessions
- Development friction: Every code change requiring a restart wipes your agent's accumulated knowledge
Here's what happens in practice:
# Session 1
agent = ConversableAgent(name="assistant")
agent.receive("My name is Sarah and I prefer TypeScript")
# Agent learns and responds appropriately
# Process restarts - Session 2
agent = ConversableAgent(name="assistant")
agent.receive("What's my preferred language?")
# Agent has no memory of Sarah or TypeScript preference
The Fix: Add Persistent Memory in 3 Lines
Install BotWire Memory and wrap your agent's memory operations:
pip install botwire
from autogen import ConversableAgent
from botwire import Memory
class PersistentAgent(ConversableAgent):
def __init__(self, name, **kwargs):
super().__init__(name=name, **kwargs)
self.memory = Memory(f"autogen-{name}")
def receive(self, message, sender=None, request_reply=None, silent=False):
# Load conversation history
history = self.memory.get("conversation_history") or []
# Add to AutoGen's internal chat history
result = super().receive(message, sender, request_reply, silent)
# Persist the updated conversation
history.append({"sender": sender.name if sender else "user", "message": message})
self.memory.set("conversation_history", history)
return result
# Usage - memory survives restarts
agent = PersistentAgent("assistant")
agent.receive("Remember: I'm working on a Flask API")
How It Works: Key-Value Persistence Across Sessions
BotWire Memory provides a simple key-value store that persists across process restarts. Each agent gets its own namespace using the autogen-{name} pattern.
The memory operations are straightforward:
memory.set(key, value)stores any JSON-serializable datamemory.get(key)retrieves data or returnsNoneif missing- Data persists automatically to BotWire's backend at
https://botwire.dev
Handling Agent State and Preferences
Beyond conversation history, you can persist any agent state:
class StatefulAgent(ConversableAgent):
def __init__(self, name, **kwargs):
super().__init__(name=name, **kwargs)
self.memory = Memory(f"autogen-{name}")
# Restore previous preferences
self.user_preferences = self.memory.get("preferences") or {}
self.learned_patterns = self.memory.get("patterns") or []
def learn_preference(self, key, value):
self.user_preferences[key] = value
self.memory.set("preferences", self.user_preferences)
def add_pattern(self, pattern):
self.learned_patterns.append(pattern)
self.memory.set("patterns", self.learned_patterns)
def get_context(self):
"""Get full agent context for decision making"""
return {
"preferences": self.user_preferences,
"patterns": self.learned_patterns,
"conversation_count": self.memory.get("message_count") or 0
}
Cross-Process Agent Coordination
Multiple agent instances can share memory using the same namespace. This enables agent hand-offs and collaborative workflows:
# Process A - Agent learns something
agent_a = PersistentAgent("researcher")
agent_a.memory.set("research_findings", {"topic": "FastAPI", "key_points": [...]})
# Process B - Different agent accesses same findings
agent_b = PersistentAgent("writer")
shared_memory = Memory("autogen-researcher") # Same namespace
findings = shared_memory.get("research_findings")
Memory operations have no TTL by default—data persists indefinitely. You can clean up with memory.set(key, None) to delete specific keys.
AutoGen GroupChat Integration
For multi-agent conversations, persist the entire group's state:
from autogen import GroupChat, GroupChatManager
from botwire import Memory
class PersistentGroupChat(GroupChat):
def __init__(self, agents, **kwargs):
super().__init__(agents=agents, **kwargs)
self.group_memory = Memory("group-chat-session")
# Restore previous messages
stored_messages = self.group_memory.get("messages") or []
self.messages = stored_messages
def append(self, message):
super().append(message)
# Persist after each message
self.group_memory.set("messages", self.messages)
self.group_memory.set("speaker_history", [msg["name"] for msg in self.messages])
# Multi-agent persistence
agents = [ConversableAgent("coder"), ConversableAgent("reviewer")]
chat = PersistentGroupChat(agents=agents)
manager = GroupChatManager(groupchat=chat)
When NOT to Use BotWire Memory
- Vector similarity search: BotWire is key-value only. Use Pinecone/Weaviate for embedding-based retrieval
- High-throughput applications: 1000 writes/day free tier won't handle production chatbots with thousands of users
- Sub-millisecond latency requirements: HTTP round-trips add ~50-200ms overhead vs in-memory storage
FAQ
Why not Redis or a local database? Redis requires setup, configuration, and infrastructure management. BotWire works immediately with zero configuration—just import and use. Perfect for development and small-scale production.
Is this actually free? Yes, 1000 writes per day per namespace, forever. 50MB storage per namespace. Unlimited reads. No credit card required.
What about data privacy? Data is stored on BotWire's servers. For sensitive applications, self-host the open-source version (it's a single FastAPI + SQLite service) or use local alternatives.
Add Memory to Your Agents Now
Stop rebuilding agent context on every restart. pip install botwire gives you persistent memory in three lines of code. Full documentation and self-hosting guide at botwire.dev.