Add persistent memory to AG2 agents (successor to AutoGen).

AG2 Integration

AG2 (the successor to Microsoft AutoGen) uses the same agent model. Wire CortexDB the same way: recall context before the LLM call, capture after.

Install

pip install cortexdbai[ag2]

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.ag2 import CortexDBAgent

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

# CortexDBAgent is an AG2 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 ag2 import ConversableAgent, 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 recall(query: str) -> str:
    pack = client.recall(scope=SCOPE, view="holistic", query=query,
                         include=["events", "beliefs", "facts", "episodes"],
                         budgets={"max_tokens": 3000})
    return pack.get("context_block", "")


def capture(text: str, role: str = "user") -> None:
    client.experience(scope=SCOPE, text=text, role=role,
                      observed_at=datetime.now(timezone.utc).isoformat(),
                      idempotency_key=f"{role}-{uuid4()}")


assistant = ConversableAgent(
    name="assistant",
    system_message="You are a helpful assistant.",
    llm_config={"model": "gpt-4o"},
)

# Register pre/post hooks so every turn is captured
assistant.register_hook(
    "process_message_before_send",
    lambda agent, messages, sender, config: messages,  # custom logic
)
assistant.register_hook(
    "process_last_received_message",
    lambda agent, msg: (capture(msg["content"], role="user"), msg)[1],
)

# Inject recall context as the agent boots
user = UserProxyAgent("user", human_input_mode="ALWAYS")
user.initiate_chat(
    assistant,
    message=f"PRIOR CONTEXT:\n{recall('what do we know')}\n\nHi!",
)

Group chat with shared memory

For a multi-agent crew, give every agent the same SCOPE — recall reads the same pack regardless of which agent calls it.

See also