CogneeMemoryStore
CogneeMemoryStore is a persistent memory store backed by Cognee's knowledge graph API. It is the shared data layer used by CogneeRetriever and CogneeWriter.
| Used by | CogneeRetriever, CogneeWriter |
| Optional init variables | search_type, top_k, dataset_name, session_id, self_improvement, timeout |
| API reference | Cognee |
| GitHub link | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/cognee |
| Package name | cognee-haystack |
Overview
CogneeMemoryStore wraps Cognee's V2 memory API:
add_memories→cognee.remembersearch_memories→cognee.recallimprove→cognee.improvedelete_all_memories→cognee.forget
Cognee supports two memory tiers. Set session_id to use the session cache — fast writes with no LLM extraction, session-aware recall. Leave session_id as None to write to the permanent knowledge graph, which uses LLM extraction during ingestion and supports richer graph-completion queries.
Cognee configuration (LLM provider, database, vector store) is read from environment variables. See the Cognee documentation for setup instructions.
Parameters
search_typeis optional and defaults to"GRAPH_COMPLETION". Controls which Cognee recall strategy is used. Other useful values include"CHUNKS"for raw retrieval and"SUMMARIES"for summarized graph nodes.top_kis optional and defaults to5. Sets the default maximum number of memories returned per search.dataset_nameis optional and defaults to"haystack_memory". Names the Cognee dataset backing this store.session_idis optional and defaults toNone. When set, reads and writes target the session-cache tier. WhenNone, the permanent knowledge graph is used.self_improvementis optional and defaults toTrue. WhenTrue, Cognee runs graph improvement inline after every write. Set toFalsewhen you wantimprove()to be the sole improvement trigger.timeoutis optional and defaults to300. Per-call timeout in seconds for any Cognee operation.
Installation
Install the Cognee integration:
Set your LLM API key (used by Cognee for graph extraction and queries):
Optionally, set a separate embedding API key (defaults to LLM_API_KEY when unset):
Usage
On its own
from haystack.dataclasses import ChatMessage
from haystack_integrations.memory_stores.cognee import CogneeMemoryStore
store = CogneeMemoryStore(search_type="GRAPH_COMPLETION", top_k=5)
store.add_memories(
messages=[ChatMessage.from_user("Alice enjoys hiking and outdoor activities.")],
user_id="a1b2c3d4-e5f6-7890-abcd-ef1234567890",
)
memories = store.search_memories(
query="What does Alice like?",
user_id="a1b2c3d4-e5f6-7890-abcd-ef1234567890",
)
print([msg.text for msg in memories])
Session tier vs permanent graph
Use session_id to control which memory tier is targeted. A single store can serve both tiers — the writer's session_id overrides the store's session_id per call.
from haystack.dataclasses import ChatMessage
from haystack_integrations.memory_stores.cognee import CogneeMemoryStore
store = CogneeMemoryStore(dataset_name="my_agent_memory", self_improvement=False)
# Write long-lived facts to the permanent graph (no session_id).
store.add_memories(
messages=[ChatMessage.from_user("Alice is a senior data scientist at Acme Corp.")],
)
# Write transient session context to the session cache.
store.add_memories(
messages=[ChatMessage.from_user("Alice is currently debugging a vector store issue.")],
session_id="alice_session_1",
)
# Promote the session cache into the permanent graph.
store.improve(session_id="alice_session_1")
Delete all memories
# Delete only this store's dataset (session cache is unaffected).
store.delete_all_memories()
# To wipe everything including the session cache, call cognee directly:
import asyncio
import cognee
asyncio.run(cognee.forget(everything=True))