Add persistent memory to Agno agents.

Agno Integration

Agno is a Python framework for building agents with toolkits. Register CortexDB recall + capture as toolkit functions.

Install

pip install cortexdbai agno

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.

Pattern

import os
from datetime import datetime, timezone
from uuid import uuid4
from agno.agent import Agent
from agno.tools import Toolkit
from agno.models.openai import OpenAIChat
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"


class CortexMemoryToolkit(Toolkit):
    def __init__(self):
        super().__init__(name="cortex_memory")
        self.register(self.recall_long_term)
        self.register(self.capture)

    def recall_long_term(self, query: str) -> str:
        """Recall prior context from long-term memory."""
        pack = client.recall(scope=SCOPE, view="holistic", query=query,
                             include=["beliefs", "facts", "episodes"],
                             budgets={"max_tokens": 3000})
        return pack["context_block"] or "(no relevant context)"

    def capture(self, text: str, role: str = "assistant") -> str:
        """Capture a turn into long-term memory."""
        client.experience(scope=SCOPE, text=text, role=role,
                          observed_at=datetime.now(timezone.utc).isoformat(),
                          idempotency_key=f"agno-{role}-{uuid4()}")
        return "captured"


agent = Agent(
    model=OpenAIChat(id="gpt-4o"),
    tools=[CortexMemoryToolkit()],
    instructions="Always recall_long_term before answering and capture after.",
)

See also