Add persistent memory to Microsoft AutoGen agents.
AutoGen Integration
AutoGen agents are stateful within a conversation but stateless across them. Wrap an AssistantAgent with CortexDB recall/capture hooks so multi-turn knowledge survives restarts.
Install
pip install cortexdbai autogen-agentchat
LLM provider note. This example uses OpenAI for the agent's chat model, but CortexDB itself is LLM-agnostic. The only model CortexDB invokes internally is the one used by
POST /v1/answerandPOST /v1/understanding/synthesize(Claude Opus 4.6 by default, configurable). Your agent's chat model is independent — swapOpenAIChat/gpt-4ofor Anthropic, Gemini, Mistral, Groq, or any local model. CortexDB does not care.
Pattern
import os
from datetime import datetime, timezone
from uuid import uuid4
from autogen import AssistantAgent, UserProxyAgent
from cortexdb.v1 import V1Client
client = V1Client(
api_url="https://api-v1.cortexdb.ai",
actor="user:alice",
bearer=os.environ["CORTEX_TOKEN"],
)
SCOPE = "org:acme/user:alice"
def cortex_system_prompt(base: str, query: str) -> str:
pack = client.recall(scope=SCOPE, view="holistic", query=query,
include=["beliefs", "facts", "episodes"],
budgets={"max_tokens": 3000})
if pack["context_block"]:
return f"{base}\n\nPRIOR CONTEXT:\n{pack['context_block']}"
return base
def capture(text: str, role: str) -> None:
client.experience(scope=SCOPE, text=text, role=role,
observed_at=datetime.now(timezone.utc).isoformat(),
idempotency_key=f"{role}-{uuid4()}")
# Use a UserProxy callback hook to intercept turns
class MemoryProxy(UserProxyAgent):
def receive(self, message, sender, *a, **kw):
capture(message["content"] if isinstance(message, dict) else str(message),
role="assistant" if sender.name == "assistant" else "user")
return super().receive(message, sender, *a, **kw)
assistant = AssistantAgent(
name="assistant",
system_message=cortex_system_prompt("You are a helpful AI assistant.", "what do we know?"),
llm_config={"model": "gpt-4o"},
)
user = MemoryProxy(name="user", human_input_mode="ALWAYS")
user.initiate_chat(assistant, message="What do you remember about our project?")
See also
- AG2 (AutoGen) — the successor project
- Python SDK
- Scopes