Use CortexDB as durable long-term memory alongside Letta (formerly MemGPT) agents.

Letta Integration

Letta gives agents working memory and a virtual context window; CortexDB gives them durable, queryable long-term memory across all sessions. Wire CortexDB recall into Letta's archival storage interface.

Install

pip install cortexdbai[letta]

Archival storage

import os
from cortexdb import Cortex
from cortexdb.integrations.letta import CortexDBArchivalStorage

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

archival = CortexDBArchivalStorage(client=client, scope="org:acme/user:alice")
archival.insert("The quarterly review is the last Friday of each quarter.")
results = archival.search("quarterly review schedule")

CortexDBStorageConnector(client=client, scope=..., table_name="passages") backs Letta's pluggable storage system.

Import existing Letta archives

CortexDB ships a native importer — point it at a Letta export and the archival blocks land as experiences:

import requests

requests.post(
    "https://api-v1.cortexdb.ai/v1/import/letta",
    headers={
        "Authorization": f"Bearer {os.environ['CORTEX_TOKEN']}",
        "X-Cortex-Actor":  "user:alice",
    },
    json={
        "scope": "org:acme/user:alice",
        "rows":  letta_export["archival_memory"],
    },
)

See Import for the full set of importers.

Prefer manual control?

Register a tool that queries CortexDB so the Letta agent can pull cross-session knowledge on demand:

import os
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_long_term(query: str) -> str:
    """Pull long-term context from CortexDB."""
    pack = client.recall(scope=SCOPE, view="holistic", query=query,
                         include=["events", "beliefs", "facts", "episodes"],
                         budgets={"max_tokens": 2000})
    return pack.get("context_block", "") or "(no relevant prior context)"

Register that as a tool with the Letta SDK, and the agent learns to call it whenever it needs prior context.

Capture turns

Letta's step API exposes the latest user + assistant messages. Capture both into CortexDB:

from datetime import datetime, timezone
from uuid import uuid4

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

See also