Skip to main content
A minimal comparison of the same async function in three modes: without memory, with a fixed retrieval query, and with a query derived from a function argument. Before you start:
  • Complete Quickstart or have Cognee installed and configured
  • Ensure you have LLM Providers configured
  • Be familiar with the add()cognify() workflow

Shared Setup

All three variants use the same dataset and the same LLM helper. Memory is built once with add() and cognify() before any of the three functions are called. LLMGateway.acreate_structured_output() automatically picks up whatever memory the decorator retrieved and prepends it to the text input — no extra code needed inside the function.
import asyncio

import cognee
from cognee.infrastructure.llm.LLMGateway import LLMGateway

DATASET_NAME = "agent_memory_demo"


async def setup_memory() -> None:
    await cognee.add(
        (
            "Internal product note: the private codename for the first supported "
            "`cognee.agent_memory` release is Maple Panda."
        ),
        dataset_name=DATASET_NAME,
    )
    await cognee.cognify(datasets=[DATASET_NAME])


async def ask_llm(question: str) -> str:
    return await LLMGateway.acreate_structured_output(
        text_input=question,
        system_prompt="Answer briefly.",
        response_model=str,
    )

Compare the Three Modes

async def answer_without_memory() -> str:
    return await ask_llm("What animal does the internal codename refer to?")


async def main() -> None:
    await setup_memory()
    answer = await answer_without_memory()
    print(answer)


if __name__ == "__main__":
    asyncio.run(main())
The question goes to the LLM with no retrieved context. The model answers from training data alone and will not know the internal codename.
The decorator does not create memory — it only retrieves it. The example works because the fact was stored first with add() and processed with cognify().

Agent Memory Decorator

Concept overview and parameter reference

Low-Level LLM

LLMGateway for direct model calls

Cognify

Build memory before using the decorator

Datasets

Scope memory retrieval to the right dataset