Persistent Memory for Haystack Agents
Free · Open source (MIT) · Works with LangChain, CrewAI, AutoGen · No signup
Your Haystack agents work perfectly... until they restart. Every pipeline run starts with a blank slate, losing all the context, user preferences, and learned patterns that made your agent intelligent. The memory gets wiped, conversations reset, and your users notice the agent "forgot" everything.
The Problem: Stateless Agents in a Stateful World
Haystack pipelines are designed to be stateless by default. Each execution starts fresh, which is great for reliability and scaling, but terrible for agents that need to remember things between runs.
When your agent restarts—whether from a crash, deployment, or just running in serverless environments—it loses:
- User conversation history and context
- Learned preferences and patterns
- Previous decisions and reasoning
- Custom settings per user/session
This creates a frustrating experience where agents appear to have amnesia. A user might spend time teaching the agent their preferences, only to have it completely forgotten on the next interaction. Without persistent memory, your Haystack agents can't build meaningful, continuous relationships with users.
The core issue is that Python variables and in-memory state disappear when processes end. Haystack doesn't include built-in persistence for agent state—it focuses on the pipeline execution, leaving memory management to you.
The Fix: Persistent Key-Value Memory
BotWire gives your Haystack agents persistent memory that survives restarts, deployments, and crashes. Install it and add memory in three lines:
pip install botwire
from botwire import Memory
from haystack import Pipeline
from haystack.components.generators.openai import OpenAIGenerator
# Create persistent memory for this agent
memory = Memory("my-agent")
# Your existing pipeline
pipeline = Pipeline()
pipeline.add_component("llm", OpenAIGenerator())
# Before running: load previous context
previous_context = memory.get("conversation_context") or ""
# After running: save new state
memory.set("conversation_context", "User prefers technical details")
memory.set("user_preferences", {"format": "json", "detail_level": "high"})
Your agent now remembers everything between runs. The memory persists across processes, deployments, and even different machines.
How It Works
BotWire provides a simple key-value store specifically designed for agent memory. Each namespace is isolated, so multiple agents or users don't interfere with each other.
Basic Operations
from botwire import Memory
# Create memory for a specific user/session
user_memory = Memory("user-123")
# Store any JSON-serializable data
user_memory.set("preferences", {
"language": "python",
"examples": True,
"format": "markdown"
})
# Retrieve with optional defaults
prefs = user_memory.get("preferences", {})
language = user_memory.get("language", "english")
# List all stored keys
all_keys = user_memory.list_keys()
print(f"Stored: {all_keys}")
Managing Memory Lifecycle
Memory doesn't accumulate forever. You can set TTL (time-to-live) for automatic cleanup:
# Expire after 7 days of inactivity
memory.set("temp_data", value, ttl=604800)
# Clean up explicitly
memory.delete("old_conversation")
# Check what's stored
if memory.exists("user_context"):
context = memory.get("user_context")
This is crucial for production agents that handle many users—old sessions automatically clean up, preventing memory bloat.
Cross-Process Persistence
The real power shows when your agent runs across different processes or containers:
# Process 1: Agent learns something
agent_memory = Memory("agent-production")
agent_memory.set("learned_patterns", ["user_asks_for_examples", "prefers_code_first"])
# Process 2 (after restart): Agent remembers
agent_memory = Memory("agent-production") # Same namespace
patterns = agent_memory.get("learned_patterns", [])
# patterns = ["user_asks_for_examples", "prefers_code_first"]
Integration with Haystack Pipelines
Here's how to add persistent memory to a typical Haystack agent pipeline:
from botwire import Memory
from haystack import Pipeline
from haystack.components.generators.openai import OpenAIGenerator
from haystack.components.builders import PromptBuilder
class PersistentAgent:
def __init__(self, user_id: str):
self.memory = Memory(f"user-{user_id}")
# Build pipeline
self.pipeline = Pipeline()
self.pipeline.add_component("prompt", PromptBuilder(
template="Context: {{context}}\nUser: {{query}}\nAssistant:"
))
self.pipeline.add_component("llm", OpenAIGenerator())
self.pipeline.connect("prompt", "llm")
def chat(self, query: str) -> str:
# Load conversation history
context = self.memory.get("context", "")
# Run pipeline
result = self.pipeline.run({
"prompt": {"context": context, "query": query}
})
response = result["llm"]["replies"][0]
# Update memory with new context
new_context = f"{context}\nUser: {query}\nAssistant: {response}"
self.memory.set("context", new_context[-2000:]) # Keep last 2000 chars
return response
# Usage
agent = PersistentAgent("user-123")
response = agent.chat("What's my favorite programming language?")
When NOT to Use BotWire
BotWire isn't the right tool for every use case:
• Vector search or embeddings: Use Pinecone, Weaviate, or Qdrant for semantic similarity searches • High-throughput scenarios: 1000 writes/day limit makes it unsuitable for heavy logging or analytics • Sub-millisecond latency requirements: HTTP API adds ~50-200ms latency vs in-memory solutions
FAQ
Why not just use Redis or a database? You could, but then you're managing infrastructure, connections, schemas, and deployment complexity. BotWire works immediately with zero setup—no servers, no credentials, no configuration.
Is the free tier actually free forever? Yes. 1000 writes/day per namespace, 50MB storage per namespace, unlimited reads. No credit card required, no trials that expire. Heavy users can self-host the open-source version.
What about data privacy and security? Data is stored on BotWire's servers unless you self-host. For sensitive applications, the MIT-licensed code runs as a single FastAPI + SQLite service you can deploy anywhere.
Get Started
Add persistent memory to your Haystack agents in under a minute. Your agents will finally remember what matters between runs.
pip install botwire
Full documentation and self-hosting guide at https://botwire.dev.