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]

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/answer and POST /v1/understanding/synthesize (Claude Opus 4.6 by default, configurable). Your agent's chat model is independent — swap OpenAIChat / gpt-4o for Anthropic, Gemini, Mistral, Groq, or any local model. CortexDB does not care.

Memory-backed agent

import os
from cortexdb import Cortex
from cortexdb.integrations.autogen import CortexDBAgent

client = Cortex(
    api_url="https://api-v1.cortexdb.ai",
    actor="user:alice",
    bearer=os.environ["CORTEX_TOKEN"],
)

# CortexDBAgent is a ConversableAgent that auto-recalls relevant context
# before each reply and stores every turn back into CortexDB.
agent = CortexDBAgent(
    name="memory_assistant",
    cortex_client=client,
    scope="org:acme/user:alice",
    llm_config=llm_config,
    system_message="You are a helpful assistant with long-term memory.",
)

Prefer explicit tools? register_cortexdb_tools(agent, executor, client, scope=...) registers cortexdb_search / store / forget on an existing agent + executor pair.

Prefer manual control?

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=["events", "beliefs", "facts", "episodes"],
                         budgets={"max_tokens": 3000})
    if pack.get("context_block", ""):
        return f"{base}\n\nPRIOR CONTEXT:\n{pack.get('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