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 cortexdbai[camel]
Quick Start
from cortexdb import Cortex
from cortexdb_camel import CortexDBMemory, get_cortexdb_toolkit
client = Cortex(base_url="https://api.cortexdb.ai")
# 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="https://api.cortexdb.ai")
memory = CortexDBMemory(
client=client,
tenant_id="my-session",
)
# Write memory records
memory.write_record("The user prefers concise technical answers.")
memory.write_record("Project deadline is next Friday.")
# 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 tenant for isolation
assistant_memory = CortexDBMemory(
client=client, tenant_id="session-1-assistant",
)
user_proxy_memory = CortexDBMemory(
client=client, tenant_id="session-1-user-proxy",
)
# Or share memory across agents
shared_memory = CortexDBMemory(
client=client, tenant_id="session-1-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="https://api.cortexdb.ai")
tools = get_cortexdb_toolkit(client, tenant_id="my-session")
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="https://api.cortexdb.ai")
# Store
cortexdb_store(client, "The API key rotates every 90 days.", tenant_id="ops")
# Search
result = cortexdb_search(client, "API key rotation", tenant_id="ops")
print(result.context)
# 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 |
Under the Hood
The integration wrapper maps to CortexDB's REST API:
# memory.write_record("The user prefers concise answers.")
curl -X POST https://api.cortexdb.ai/v1/remember \
-H "Authorization: Bearer your-api-key" \
-H "Content-Type: application/json" \
-d '{
"content": "The user prefers concise answers.",
"tenant_id": "my-session"
}'
# Returns: { "event_id": "019d6359-d3cc-7671-9e4c-9151011fa016" }
# cortexdb_search(client, "API key rotation", tenant_id="ops")
curl -X POST https://api.cortexdb.ai/v1/recall \
-H "Authorization: Bearer your-api-key" \
-H "Content-Type: application/json" \
-d '{
"query": "API key rotation",
"tenant_id": "ops"
}'
# Returns: { "context": "...", "confidence": 0.92, "latency_ms": 12 }
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="https://api.cortexdb.ai")
# Memory backend for automatic context
memory = CortexDBMemory(
client=client,
tenant_id="customer-support",
)
# Tools for explicit memory management
tools = get_cortexdb_toolkit(
client=client,
tenant_id="customer-support",
)
# 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.",
)
# Later interaction — the agent can search for similar issues
result = memory.get_context("login timeout after password reset")
print(f"Retrieved context: {result.context}")
# 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?",
)
)