Skip to main content
A minimal guide to enabling conversational memory with sessions. When you use the same session_id across searches, Cognee remembers previous questions and answers, enabling contextually aware follow-up questions.

Before You Start

Code in Action

import asyncio
import cognee
from cognee import SearchType

async def main():
    # Prepare knowledge base
    await cognee.add([
        "Alice moved to Paris in 2010. She works as a software engineer.",
        "Bob lives in New York. He is a data scientist.",
        "Alice and Bob met at a conference in 2015."
    ])
    await cognee.cognify()

    # First search - starts a new session (default user is used when none is passed)
    result1 = await cognee.search(
        query_type=SearchType.GRAPH_COMPLETION,
        query_text="Where does Alice live?",
        session_id="conversation_1"
    )
    print("First answer:", result1[0])

    # Follow-up search - uses conversation history
    result2 = await cognee.search(
        query_type=SearchType.GRAPH_COMPLETION,
        query_text="What does she do for work?",
        session_id="conversation_1"  # Same session
    )
    print("Follow-up answer:", result2[0])
    # The LLM knows "she" refers to Alice from previous context

    # Different session - no memory of previous conversation
    result3 = await cognee.search(
        query_type=SearchType.GRAPH_COMPLETION,
        query_text="What does she do for work?",
        session_id="conversation_2"  # New session
    )
    print("New session answer:", result3[0])
    # This won't know who "she" refers to

asyncio.run(main())
This example works with either Redis or Filesystem adapter. Configure your chosen adapter in the Before you start section above.

What Just Happened

Step 1: Prepare Knowledge Base

await cognee.add([
    "Alice moved to Paris in 2010. She works as a software engineer.",
    "Bob lives in New York. He is a data scientist.",
    "Alice and Bob met at a conference in 2015."
])
await cognee.cognify()
Before you can search with sessions, you need to have data in your knowledge base. Use cognee.add() to ingest data and cognee.cognify() to build the knowledge graph.

Step 2: Use Session ID in Searches

result = await cognee.search(
    query_type=SearchType.GRAPH_COMPLETION,
    query_text="Where does Alice live?",
    session_id="conversation_1"
)
The session_id parameter creates or continues a conversation thread. All searches with the same session_id share conversation history.

Step 3: Follow-up Questions

result = await cognee.search(
    query_type=SearchType.GRAPH_COMPLETION,
    query_text="What does she do for work?",
    session_id="conversation_1"  # Same session
)
When you use the same session_id, Cognee automatically includes previous Q&A turns in the LLM prompt, enabling contextual follow-up questions.

Step 4: Multiple Sessions

# Session 1
await cognee.search(query_text="Question 1", session_id="session_1")
await cognee.search(query_text="Follow-up", session_id="session_1")

# Session 2 (independent)
await cognee.search(query_text="Question 1", session_id="session_2")
Each session_id maintains its own conversation history. Sessions are isolated from each other.

Advanced Usage

Use meaningful session IDs to organize conversations:
# User-specific sessions
await cognee.search(query_text="...", session_id=f"user_{user_id}_chat")

# Topic-specific sessions
await cognee.search(query_text="...", session_id="project_planning")
await cognee.search(query_text="...", session_id="bug_discussion")
Session IDs are arbitrary strings—use whatever naming scheme fits your application.
Sessions do not expire automatically. They persist until the cache is cleared. To clear sessions, use cognee.prune.prune_system(..., cache=True) or wipe your cache backend (e.g. Redis keys or the filesystem cache directory).
If you omit session_id and caching is enabled, Cognee uses the session ID default_session and still stores each search turn there. To scope conversations explicitly, pass a session_id.
Use cognee.session.get_session(session_id=..., last_n=N) to retrieve the last N Q&A entries. Entries are in chronological order (oldest first). Use entries[-1] for the most recent entry.
entries = await cognee.session.get_session(session_id="my_session", last_n=10)
newest = entries[-1]
print(newest.qa_id, newest.question, newest.answer)
Omitting session_id does not disable session storage: Cognee still writes to default_session when caching is on. To avoid storing any session data, set CACHING=false or ensure no cache backend is available. The system gracefully handles missing cache backends; searches run without conversational memory.
Sessions only affect these search types: GRAPH_COMPLETION, RAG_COMPLETION, TRIPLET_COMPLETION. Other modes (CHUNKS, SUMMARIES, etc.) do not use or write session history. Cognee includes up to the last 10 session entries when building conversation history. For multi-tenant or background jobs, pass an explicit user so the default user is not used.