A minimal guide to enabling conversational memory with sessions. When you use the sameDocumentation Index
Fetch the complete documentation index at: https://docs.cognee.ai/llms.txt
Use this file to discover all available pages before exploring further.
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).Sessions can also be persisted into the knowledge graph via the session persistence pipeline, which converts cached Q&A history into searchable graph nodes.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 Returns
cognee.session.get_session() to retrieve stored Q&A entries for a session. Entries are returned in chronological order (oldest first). Use entries[-1] for the most recent entry. If the session does not exist or the cache backend is unavailable, this call returns an empty list instead of raising an error.Identifier of the session to retrieve. It must match the
session_id previously passed to cognee.search().Maximum number of most-recent entries to return. When
None, all stored entries are returned.User that owns the session. When
None, Cognee resolves it from the current session context or falls back to the default user.List[SessionQAEntry], which may be empty.Upstream context and the include_context flag
Upstream context and the include_context flag
Every Q&A entry stored in the session cache contains a
context field. Depending on how the completion was generated, this field may be empty or may contain a stored summary of the retrieved context for that turn. You can inspect it programmatically when reading session history.The include_context flag:get_session_manager() is an internal, lower-level API rather than the usual SDK entry point. Prefer cognee.session.get_session() unless you specifically need formatted history control such as include_context.SessionManager.get_session() and SessionManager.format_entries() both accept include_context: bool (default True). When True, a CONTEXT: line is included for each entry in the formatted history string; when False, it is omitted.This flag is not exposed on cognee.search() or cognee.session.get_session(). If you need it, use the lower-level SessionManager directly.Additional information:Example: reading context from past entries
Example: reading context from past entries
Does the LLM automatically see context from previous turns?
Does the LLM automatically see context from previous turns?
No. When Cognee builds conversation history for the LLM during
cognee.search(), it uses include_context=False internally — previous questions and answers are included in the prompt, but the stored context from those earlier turns is omitted. Fresh graph context is retrieved for the current query only. This keeps prompts compact and avoids re-sending large context blobs.Using SessionManager directly
Using SessionManager directly
If your use-case requires the LLM to see stored context from a prior turn — for example to trace provenance or build a richer prompt — use the lower-level
SessionManager to retrieve formatted history with include_context=True, then pass that history to your own LLM call.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.Token usage tracking and cost estimation
Token usage tracking and cost estimation
Cognee can record approximate LLM usage per session for session-scoped completion flows. This is most relevant when you use recall in a conversational pattern and want to inspect prompt/completion volume over time.What is trackedWhen an LLM completion is generated inside an active session scope, Cognee accumulates these values on the session record:
tokens_in— estimated prompt tokenstokens_out— estimated completion tokenscost_usd— estimated spend using Cognee’s built-in pricing table
- Session tracking depends on caching being available.
- If you omit
session_id, Cognee still usesdefault_sessionrather than disabling session usage tracking. - The tracked counts reflect session-scoped completion calls, not every LLM-capable step in the broader ingestion pipeline.
parent_user_id matches the requesting user’s id, and sessions visible through dataset read permissions.Sessions remain stored under the user that actually created them, so the user_id on each session_records row is unchanged. Aggregate totals such as stats and cost-by-model therefore include usage from child agents when those sessions are visible to the parent. See Users for how to create agent users with parent_user_id.Reading usage via the HTTP APIGET /api/v1/sessionslists sessions visible to the callerGET /api/v1/sessions/{session_id}returns per-session fields such astokens_in,tokens_out, andcost_usdGET /api/v1/sessions/stats?range=30dreturns aggregate totals for24h,7d,30d, orallGET /api/v1/sessions/cost-by-model?range=30dbreaks usage down by model
Sessions and Caching
Understand how sessions work conceptually
Search Basics
Learn about search parameters and types
Setup Configuration
Configure cache adapters and providers