Add long-term memory to Agno agents with CortexDB.
Agno
CortexDB provides persistent long-term memory for Agno agents, enabling them to learn, remember, and build context across sessions and conversations.
Installation
pip install cortexdbai[agno]
Quick Start
from agno.agent import Agent
from cortexdb import Cortex
from cortexdb_agno import CortexDBMemory, CortexDBTools
client = Cortex(base_url="https://api.cortexdb.ai")
# Create a memory backend
memory = CortexDBMemory(client=client, tenant_id="my-app")
# Create agent tools
toolkit = CortexDBTools(client=client, tenant_id="my-app")
agent = Agent(
name="Research Assistant",
memory=memory,
tools=toolkit.get_tools(),
)
As a Memory Backend
Use CortexDBMemory as Agno's memory provider to give agents persistent recall:
from cortexdb import Cortex
from cortexdb_agno import CortexDBMemory
client = Cortex(base_url="https://api.cortexdb.ai")
memory = CortexDBMemory(
client=client,
tenant_id="my-app",
)
# Store memories
memory.add("The quarterly report deadline is March 15th.")
memory.add("Budget for Q2 was approved at $50,000.")
# Search for relevant context
results = memory.search("upcoming deadlines")
# Get formatted context for a prompt
context = memory.get_context("budget information", max_tokens=4096)
# Clear all memories
memory.clear()
Shared vs Per-Agent Memory
# Shared memory — all agents see the same memories
shared = CortexDBMemory(client=client, tenant_id="my-app-shared")
# Per-agent memory — isolated via tenant_id
researcher_mem = CortexDBMemory(client=client, tenant_id="my-app-researcher")
writer_mem = CortexDBMemory(client=client, tenant_id="my-app-writer")
As Agent Tools
Use CortexDBTools to give agents explicit store, search, and forget capabilities:
from agno.agent import Agent
from cortexdb import Cortex
from cortexdb_agno import CortexDBTools
client = Cortex(base_url="https://api.cortexdb.ai")
toolkit = CortexDBTools(client=client, tenant_id="my-app")
agent = Agent(
name="Knowledge Manager",
tools=toolkit.get_tools(),
instructions="Use CortexDB to store and retrieve important information.",
)
The toolkit provides three 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
Under the Hood
The integration wraps CortexDB's REST API. Here are the equivalent calls:
# remember() — store a memory
curl -X POST https://api.cortexdb.ai/v1/remember \
-H "Authorization: Bearer your-cortex-api-key" \
-H "Content-Type: application/json" \
-d '{
"content": "The quarterly report deadline is March 15th.",
"tenant_id": "my-app"
}'
# Returns: { "event_id": "evt_abc123" }
# recall() — retrieve relevant context
curl -X POST https://api.cortexdb.ai/v1/recall \
-H "Authorization: Bearer your-cortex-api-key" \
-H "Content-Type: application/json" \
-d '{
"query": "upcoming deadlines",
"tenant_id": "my-app"
}'
# Returns: { "context": "...", "confidence": 0.91, "latency_ms": 13 }
Configuration
| Parameter | Default | Description |
|---|---|---|
| client | Required | Initialized Cortex client instance |
| tenant_id | "default" | Tenant identifier for multi-tenant isolation |
Complete Example
from agno.agent import Agent
from cortexdb import Cortex
from cortexdb_agno import CortexDBMemory, CortexDBTools
client = Cortex(base_url="https://api.cortexdb.ai")
# Shared memory backend
memory = CortexDBMemory(client=client, tenant_id="engineering")
# Tools for the agent to manage its own memory
toolkit = CortexDBTools(client=client, tenant_id="engineering")
agent = Agent(
name="Standup Bot",
memory=memory,
tools=toolkit.get_tools(),
instructions=(
"You are a standup assistant. Use your memory to track what each "
"team member is working on. Store updates and recall previous "
"context when someone asks about project status."
),
)
# The agent can now:
# 1. Automatically recall relevant past context via memory
# 2. Explicitly store new information via the store tool
# 3. Search for specific information via the search tool
# 4. Remove outdated info via the forget tool