Add persistent memory to ControlFlow tasks and agents.

ControlFlow Integration

ControlFlow is a Prefect-native framework for agent workflows. Register CortexDB recall + capture as task-level tools.

Install

pip install cortexdbai controlflow

Pattern

import os
from datetime import datetime, timezone
from uuid import uuid4
import controlflow as cf
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"


@cf.tool
def recall_long_term(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)"


@cf.tool
def capture(text: str, role: str = "assistant") -> str:
    client.experience(scope=SCOPE, text=text, role=role,
                      observed_at=datetime.now(timezone.utc).isoformat(),
                      idempotency_key=f"cf-{role}-{uuid4()}")
    return "captured"


result = cf.run(
    "Answer the user's question about Acme's renewal",
    tools=[recall_long_term, capture],
)

See also