Run your first Cognee example to see AI memory in action.

Basic Usage

This minimal example shows how to add content, process it, and perform a search:
import cognee
import asyncio

async def main():

    # Create a clean slate for cognee -- reset data and system state
    await cognee.prune.prune_data()
    await cognee.prune.prune_system(metadata=True)
    
    # Add sample content
    text = "Frodo carried the One Ring to Mordor."
    await cognee.add(text)
    
    # Process with LLMs to build the knowledge graph
    await cognee.cognify()
    
    # Search the knowledge graph
    results = await cognee.search(
        query_text="What did Frodo do?"
    )
    
    # Print
    for result in results:
        print(result)

if __name__ == '__main__':
    asyncio.run(main())

What just happened

The code demonstrates Cognee’s three core operations:
  • .add — Adds documents to Cognee so they can be cognified. In this case, we added a single document (the string “Frodo carried the One Ring to Mordor”).
  • .cognify — This is where the cognification happens. All documents are chunked, entities are extracted, relationships are made, and summaries are generated. In this case, we can expect entities like Frodo, One Ring, and Mordor.
  • .search — Queries the knowledge graph using vector similarity and graph traversal to find relevant information and return contextual results.

Next Steps