Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.cognee.ai/llms.txt

Use this file to discover all available pages before exploring further.

A minimal guide to shaping graph extraction with a custom LLM prompt. You’ll pass your prompt via custom_prompt to cognee.remember() to control entity types, relationship labels, and extraction rules.
For the built-in graph extraction prompts selected through GRAPH_PROMPT_PATH, see Cognify.
Before you start:
  • Complete Quickstart to understand basic operations
  • Ensure you have LLM Providers configured
  • Have some text or files to process

Code in Action

Step 1: Write a Custom Prompt

custom_prompt = """
Extract only people and cities as entities.
Connect people to cities with the relationship "lives_in".
Ignore all other entities.
"""
The custom prompt overrides the default system prompt used during entity/relationship extraction. It constrains node types, enforces relationship naming, and reduces noise.
custom_prompt is ignored when temporal_cognify=True.

Step 2: Remember with Your Custom Prompt

await cognee.forget(everything=True)
await cognee.remember(
    [
        "Alice moved to Paris in 2010, while Bob has always lived in New York.",
        "Andreas was born in Venice, but later settled in Lisbon.",
        "Diana and Tom were born and raised in Helsinki. Diana currently resides in Berlin, while Tom never moved.",
    ],
    custom_prompt=custom_prompt,
    self_improvement=False,
)
This resets the local state and then uses remember() to ingest the text and build the graph in one pass. The same approach works with multiple documents, files, or entire datasets.

Step 3: Ask Questions

res = await cognee.recall(
    query_type=SearchType.GRAPH_COMPLETION,
    query_text="Where does Alice live?",
)
Use cognee.recall(...) with SearchType.GRAPH_COMPLETION to get answers that leverage your custom extraction rules.

custom_prompt vs system_prompt: which prompt goes where

Cognee uses two distinct prompts at two different stages, and they are not interchangeable. Passing one where the other is expected will silently have no effect.
OperationPrompt parameterWhat it influencesStage
cognee.remember()custom_promptEntity & relationship extraction — remember() runs add() + cognify() under the hood, so this overrides the graph extraction prompt used during graph build.Graph build
cognee.recall()system_prompt (or system_prompt_path)Answer generation — instructs the LLM how to compose the final answer from retrieved context. Only applies to completion-style retrieval (GRAPH_COMPLETION, RAG_COMPLETION, TRIPLET_COMPLETION, etc.); ignored by only_context=True and by non-completion retrieval paths.Query time

Additional examples

Additional examples about Custom prompts are available on our github.

Full Example

import asyncio
import cognee
from cognee.api.v1.search import SearchType

custom_prompt = """
Extract only people and cities as entities.
Connect people to cities with the relationship "lives_in".
Ignore all other entities.
"""

async def main():
    await cognee.forget(everything=True)
    await cognee.remember(
        [
            "Alice moved to Paris in 2010, while Bob has always lived in New York.",
            "Andreas was born in Venice, but later settled in Lisbon.",
            "Diana and Tom were born and raised in Helsinki. Diana currently resides in Berlin, while Tom never moved.",
        ],
        custom_prompt=custom_prompt,
        self_improvement=False,
    )

    res = await cognee.recall(
        query_type=SearchType.GRAPH_COMPLETION,
        query_text="Where does Alice live?",
    )
    print(res)

if __name__ == "__main__":
    asyncio.run(main())
import asyncio
import cognee
from cognee.api.v1.search import SearchType

custom_prompt = """
Extract only people and cities as entities.
Connect people to cities with the relationship "lives_in".
Ignore all other entities.
"""

async def main():
    await cognee.add([
        "Alice moved to Paris in 2010, while Bob has always lived in New York.",
        "Andreas was born in Venice, but later settled in Lisbon.",
        "Diana and Tom were born and raised in Helsingy. Diana currently resides in Berlin, while Tom never moved."
    ])
    await cognee.cognify(custom_prompt=custom_prompt)

    res = await cognee.recall(
        query_type=SearchType.GRAPH_COMPLETION,
        query_text="Where does Alice live?",
    )
    print(res)

if __name__ == "__main__":
    asyncio.run(main())
This simple example uses a few strings for demonstration. In practice, you can add multiple documents, files, or entire datasets - the custom prompt processing works the same way across all your data.
If you are running Cognee as a server and want to infer a schema or generate a prompt through HTTP instead of writing it by hand, see the LLM Utility Endpoints examples in Deploy REST API Server.

Core Concepts

Understand knowledge graph fundamentals

Ontology Quickstart

Learn about ontology integration

API Reference

Explore API endpoints