Persistent Memory for Semantic Kernel Agents
Free · Open source (MIT) · Works with LangChain, CrewAI, AutoGen · No signup
Semantic Kernel agents lose their memory when they restart. Your carefully crafted conversation history, user preferences, and contextual state vanish with each process restart. This breaks multi-session experiences and forces users to start from scratch. Here's how to add persistent memory that survives restarts across C# and Python Semantic Kernel applications.
The Memory Gap in Semantic Kernel
Microsoft Semantic Kernel excels at orchestrating LLM interactions, but it doesn't solve state persistence out of the box. When your agent process dies — whether from a crash, deployment, or simple restart — all memory disappears.
This creates jarring user experiences. A customer service bot forgets previous conversations. A personal assistant loses learned preferences. A code review agent can't remember past feedback patterns. The agent effectively develops amnesia with every restart.
The core issue is that Semantic Kernel's built-in memory is process-bound. Variables, conversation history, and custom state live in RAM. When the process exits, that memory is gone. You need a persistence layer that survives process boundaries while integrating cleanly with SK's architecture.
Traditional solutions like databases require schema design, connection management, and serialization logic. You end up building a custom persistence layer instead of focusing on your agent's core functionality.
The Fix: BotWire Memory
BotWire provides persistent key-value storage for agents with zero configuration. Install it and start persisting state in three lines:
pip install botwire
from botwire import Memory
# Persistent memory that survives restarts
memory = Memory("my-agent-namespace")
# Store state
memory.set("user_preference", "concise responses")
memory.set("conversation_count", 42)
# Retrieve across restarts
preference = memory.get("user_preference") # "concise responses"
count = memory.get("conversation_count") # 42
This memory persists across process restarts, machine reboots, and deployments. No database setup, no API keys, no configuration files.
How It Works
BotWire Memory acts as a persistent key-value store with a dead-simple API. Each namespace is isolated, so multiple agents or environments won't interfere with each other.
from botwire import Memory
import semantic_kernel as sk
class PersistentAgent:
def __init__(self, namespace: str):
self.memory = Memory(namespace)
self.kernel = sk.Kernel()
def remember_user_context(self, user_id: str, context: dict):
"""Store user context that survives restarts"""
self.memory.set(f"user:{user_id}:context", context)
def get_user_context(self, user_id: str) -> dict:
"""Retrieve user context from previous sessions"""
return self.memory.get(f"user:{user_id}:context", default={})
def increment_interaction_count(self):
"""Track interactions across restarts"""
current = self.memory.get("interaction_count", default=0)
self.memory.set("interaction_count", current + 1)
return current + 1
# Usage
agent = PersistentAgent("customer-service-bot")
# This state persists across restarts
agent.remember_user_context("user-123", {
"preferred_language": "spanish",
"last_issue": "billing",
"satisfaction_score": 8
})
The memory is immediately consistent — no eventual consistency delays. When you call set(), the data is stored and immediately available to get() calls, even from different processes or machines.
You can also manage the memory lifecycle with additional operations:
# List all keys (useful for debugging)
all_keys = memory.list_keys()
# Delete specific keys
memory.delete("temporary_state")
# Clear entire namespace (careful!)
memory.clear()
Common patterns include namespacing by user (f"user:{user_id}:state"), by session (f"session:{session_id}:history"), or by agent instance (f"agent:{agent_id}:config").
Integration with Semantic Kernel Workflows
For chat-based agents, BotWire integrates directly with conversation history management:
from botwire import BotWireChatHistory
import semantic_kernel as sk
# Persistent chat history across restarts
chat_history = BotWireChatHistory(session_id="user-42")
# Works with SK's chat completion
kernel = sk.Kernel()
chat_service = kernel.get_service(type=ChatCompletionServiceBase)
# History automatically persists
chat_history.add_user_message("What's the weather like?")
# Generate response (this would use your SK plugins)
response = await chat_service.get_chat_message_content(
chat_history=chat_history,
settings=sk.OpenAIChatPromptExecutionSettings()
)
chat_history.add_assistant_message(str(response))
# History survives process restart
# Next time you create BotWireChatHistory("user-42"),
# the conversation continues where it left off
This maintains conversation continuity across agent restarts, essential for production chatbots and assistants.
When NOT to Use BotWire
BotWire isn't the right choice for every use case:
• Vector search or embeddings: BotWire is key-value storage, not a vector database. Use Pinecone, Weaviate, or Qdrant for semantic search. • High-throughput applications: The free tier limits writes to 1000/day per namespace. For heavy usage, consider Redis or database solutions. • Sub-millisecond latency: Network calls add latency. For ultra-fast access, keep hot data in local memory and use BotWire for persistence only.
FAQ
Why not just use Redis or a database? BotWire requires zero setup — no Redis server, no database schemas, no connection strings. It's designed for developers who want persistence without infrastructure overhead. For production scale, you might graduate to dedicated solutions.
Is this actually free? Yes, the free tier is permanent: 1000 writes per day per namespace, 50MB storage per namespace, unlimited reads. No credit card, no trials, no surprise billing.
What about data privacy? BotWire is open source (MIT license) and self-hostable. You can run your own instance with the FastAPI + SQLite service from the GitHub repo. For convenience, the hosted version at botwire.dev handles the infrastructure.
Get Started
Add persistent memory to your Semantic Kernel agents in under a minute. No configuration, no database setup, no API keys required.
pip install botwire
Start building persistent agents at botwire.dev.