session_id across searches, Cognee remembers previous questions and answers, enabling contextually aware follow-up questions.
Before You Start
- Complete Quickstart to understand basic operations
- Ensure you have LLM Providers configured
- Read Sessions and Caching for conceptual overview
- Configure your cache adapter before using sessions. See Cache Adapters for Redis and Filesystem setup instructions.
Code in Action
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
cognee.add() to ingest data and cognee.cognify() to build the knowledge graph.
Step 2: Use Session ID in Searches
session_id parameter creates or continues a conversation thread. All searches with the same session_id share conversation history.
Step 3: Follow-up Questions
session_id, Cognee automatically includes previous Q&A turns in the LLM prompt, enabling contextual follow-up questions.
Step 4: Multiple Sessions
session_id maintains its own conversation history. Sessions are isolated from each other.
Advanced Usage
Custom Session IDs
Custom Session IDs
Use meaningful session IDs to organize conversations:Session IDs are arbitrary strings—use whatever naming scheme fits your application.
Session persistence and clearing
Session persistence and clearing
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).Default Session ID
Default Session ID
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.Inspect Session History
Inspect Session History
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.Disabling Sessions
Disabling Sessions
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 and search types
Sessions and search types
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.