Add long-term memory to CAMEL-AI agents with CortexDB.
CAMEL-AI
CortexDB provides persistent long-term memory for CAMEL-AI agents, enabling multi-agent conversations and role-playing sessions to retain context across interactions.
Installation
pip install cortexdb-camel
Quick Start
from cortexdb import Cortex
from cortexdb_camel import CortexDBMemory, get_cortexdb_toolkit
client = Cortex(base_url="http://localhost:3141")
# As a memory backend
memory = CortexDBMemory(client=client, tenant_id="my-session")
# As agent tools
tools = get_cortexdb_toolkit(client, tenant_id="my-session")
As a Memory Backend
Use CortexDBMemory as CAMEL's memory provider to give agents persistent recall across conversations:
from cortexdb import Cortex
from cortexdb_camel import CortexDBMemory
client = Cortex(base_url="http://localhost:3141")
memory = CortexDBMemory(
client=client,
tenant_id="my-session",
namespace="roleplay",
top_k=10,
)
# Write memory records
memory.write_record("The user prefers concise technical answers.")
memory.write_record("Project deadline is next Friday.", metadata={"priority": "high"})
# Retrieve relevant context as structured records
records = memory.get_context("project deadlines")
# Retrieve as formatted text for prompt injection
context_text = memory.get_context_text("user preferences", max_tokens=4096)
# Clear all memories
memory.clear()
Per-Agent Memory
# Each agent gets its own memory namespace
assistant_memory = CortexDBMemory(
client=client, tenant_id="session-1", namespace="assistant",
)
user_proxy_memory = CortexDBMemory(
client=client, tenant_id="session-1", namespace="user-proxy",
)
# Or share memory across agents
shared_memory = CortexDBMemory(
client=client, tenant_id="session-1", namespace="shared",
)
As Agent Tools
Use get_cortexdb_toolkit to create CAMEL FunctionTool instances that agents can invoke:
from camel.agents import ChatAgent
from cortexdb import Cortex
from cortexdb_camel import get_cortexdb_toolkit
client = Cortex(base_url="http://localhost:3141")
tools = get_cortexdb_toolkit(client, tenant_id="my-session", namespace="tools")
agent = ChatAgent(
system_message="You are a helpful assistant with long-term memory.",
tools=tools,
)
The toolkit provides three function tools:
- cortexdb_search — Search for relevant memories using natural language
- cortexdb_store — Save important information for future retrieval
- cortexdb_forget — Remove memories for privacy or data correction
Standalone Functions
You can also use the underlying functions directly:
from cortexdb import Cortex
from cortexdb_camel import cortexdb_search, cortexdb_store, cortexdb_forget
client = Cortex(base_url="http://localhost:3141")
# Store
cortexdb_store(client, "The API key rotates every 90 days.", tenant_id="ops")
# Search
results = cortexdb_search(client, "API key rotation", tenant_id="ops")
# Forget
cortexdb_forget(client, "old API key policy", "Policy updated", tenant_id="ops")
Configuration
| Parameter | Default | Description |
|---|---|---|
| client | Required | Initialized Cortex client instance |
| tenant_id | "default" | Tenant identifier for multi-tenant isolation |
| namespace | None | Namespace for organizing memories |
| top_k | 5 | Maximum results per context retrieval (memory only) |
Complete Example
from camel.agents import ChatAgent
from camel.messages import BaseMessage
from cortexdb import Cortex
from cortexdb_camel import CortexDBMemory, get_cortexdb_toolkit
client = Cortex(base_url="http://localhost:3141")
# Memory backend for automatic context
memory = CortexDBMemory(
client=client,
tenant_id="customer-support",
namespace="tickets",
)
# Tools for explicit memory management
tools = get_cortexdb_toolkit(
client=client,
tenant_id="customer-support",
namespace="tickets",
)
# Build a support agent with persistent memory
agent = ChatAgent(
system_message=(
"You are a customer support agent. Use your memory tools to store "
"ticket details and search for similar past issues. Always check "
"memory before answering a question."
),
tools=tools,
)
# First interaction — store a new ticket
memory.write_record(
"Ticket #1234: User reports login timeout after password reset. "
"Resolved by clearing session cache.",
metadata={"ticket_id": "1234", "status": "resolved"},
)
# Later interaction — the agent can search for similar issues
context = memory.get_context("login timeout after password reset")
print(f"Found {len(context)} related records")
# Agent can also use tools in conversation
response = agent.step(
BaseMessage.make_user_message(
role_name="User",
content="A customer is reporting login timeouts. Have we seen this before?",
)
)