Courses / RAG Systems / Module 11
Module 11: GraphRAG and knowledge graphs
10 h · theory 3 · hands-on 4 · project 2 · revision 1
You will be able to
- Build a knowledge graph from documents
- Answer global questions with community summaries
- Compare GraphRAG with plain RAG on cost and quality
- Decide when a graph is worth it
Capstone increment: Optional: a graph index for cross-document questions, compared with the hybrid retriever.
Job-description skills: Knowledge graphs / GraphRAG · Lab tools: graphrag, lightrag-hku, neo4j
1. Questions plain RAG can’t answer
Top-k retrieval finds a few chunks similar to the question. It struggles with two kinds of question: global ones that need the whole corpus (“what are the main recurring causes across all incident reports?”) and multi-hop ones that need facts chained across documents (“which customer-facing services break if this database fails, and who owns them?”).
Knowledge graphs answer both: entities and relationships extracted once, organised into communities with summaries for global questions, and traversed for multi-hop ones.
2. Build the graph
An LLM reads each chunk and extracts entities (services, teams, people, products), relationships (depends on, owns, caused) and sometimes claims. Schema-guided extraction, where you name the entity and relation types you care about, gives cleaner graphs than open extraction. Entity resolution matters: “Acme Ltd”, “ACME” and “Acme Limited” must become one node.
Often the best graph is the one you already have. A CMDB, a service catalogue, an org chart or a product database can become a graph without any extraction, and is more accurate than anything an LLM extracts from prose.
3. Communities and summaries: Microsoft GraphRAG
Microsoft’s GraphRAG (Edge and others, 2024) clusters the graph into a hierarchy of communities with the Leiden algorithm and has an LLM write a report for each community. Global search answers by mapping the question over community reports and reducing the partial answers; local search starts from entities that match the question and pulls in their neighbours and source chunks.
The cost is indexing: extraction and summarisation take many LLM calls per chunk, far more than embedding. Microsoft’s LazyGraphRAG and HKU’s LightRAG cut that cost, and LightRAG supports incremental updates, which matters when documents change often.
Deep dive
Deep dive: how GraphRAG answers a global question
Plain RAG retrieves the few chunks most similar to the question. A question like “what are the main risks across these 5,000 incident reports?” has no single similar chunk: the answer is spread across all of them. GraphRAG prepares for questions like this at indexing time.
- Extract. An LLM reads every chunk and lists entities (services, teams, vendors) and the relationships between them, with short descriptions.
- Merge. The same entity found in different chunks becomes one node, and its descriptions are summarised.
- Cluster. The Leiden algorithm splits the graph into communities of densely connected entities, at several levels: a few big communities, many small ones.
- Summarise. An LLM writes a report for each community: its members, key relationships and claims.
- Map. At query time, every community report at the chosen level gets the question and writes a partial answer with a helpfulness score from 0 to 100.
- Reduce. Partial answers below a threshold are dropped, the best are packed into the context, and the LLM writes the final answer.
Worked example: what indexing and a global query cost, roughly
5,000 documents × 4 chunks = 20,000 chunks extraction: 1–2 LLM calls per chunk ≈ 20,000–40,000 calls community reports: one call each → hundreds to thousands of calls one global query maps over every report at its level → hundreds of calls budget before you build; LazyGraphRAG and LightRAG exist mainly to cut these costs
4. Compare honestly
Build two question sets: global questions and local fact lookups. Run your hybrid retriever and the graph approach on both, judged the same way (Module 8). Record indexing cost and time, cost per query, latency, and how an update to one document propagates. Graphs often win clearly on global questions and lose or tie on local ones.
A graph is worth it when your corpus is rich in entities and relationships and users ask about connections, patterns and themes. Record the comparison and the decision in an ADR either way; “we measured and chose not to” is a strong interview answer.
5. Structured questions: traversal and text-to-Cypher
For multi-hop questions over a real graph database such as Neo4j, let the model write a Cypher query from the question and the schema, then run it. Guard it like any generated code: a read-only database user, query timeouts, a row limit, and validation that the query only touches allowed labels.
For known question types (blast radius, ownership, dependency chains), write the traversal yourself and expose it as a tool. Deterministic code beats generated queries wherever the question shape is predictable.
Hands-on: coding window
The coding window and the full VS Code lab are available after you sign in.
Sign in to codeLabs
Lab 11.1: Index with GraphRAG or LightRAG
Run Microsoft GraphRAG or LightRAG over 50 capstone documents with a small model. Log the number of LLM calls, tokens, cost and time.
Expected: An index plus a cost report per 1,000 chunks.
Lab 11.2: Head-to-head
Write 10 global and 10 local questions. Answer them with the graph approach and the hybrid retriever, and judge both with your calibrated judge.
Expected: A table by question type with quality, cost and latency.
Lab 11.3: Decide (optional capstone increment)
If the graph wins on a question type your users ask, add it behind the router (Module 7) for that type. Either way, write docs/adr/0006-graphrag.md with the numbers.
Expected: An ADR with a measured decision, and a route if adopted.
Go deeper
Videos
- GraphRAG vs. Traditional RAG: Higher Accuracy & Insight with LLM · IBM Technology · ~4 min. A four-minute overview of what a graph adds over plain RAG.
- Graph RAG Simply Explained · codebasics · ~14 min. GraphRAG explained simply with a worked example of entities, relations and communities.
- LightRAG - A simple and fast RAG that beats GraphRAG? (paper explained) · AI Bites · ~9 min. The LightRAG paper explained: dual-level retrieval and cheaper updates than GraphRAG.
- GraphRAG: Building a Smarter AI System (full walkthrough) · Thu Vu · ~36 min. A full GraphRAG walk-through on real data, including indexing cost and query modes.
Further reading
- Paper From Local to Global: A Graph RAG Approach to Query-Focused Summarization · Edge et al., Microsoft Research, 2024. The GraphRAG paper: extraction, Leiden communities, community reports and map-reduce answering.
- Docs GraphRAG documentation · Microsoft. The indexing pipeline, prompt tuning and the local, global and DRIFT query modes.
- Paper LightRAG: Simple and Fast Retrieval-Augmented Generation · Guo et al., 2024. A cheaper graph-plus-vector design and its comparison with GraphRAG.
- Article What is GraphRAG? · Neo4j. The knowledge-graph view: traversal, Cypher and when a graph database earns its place.
GitHub repositories
- microsoft/graphrag: The indexing pipeline config and the prompts for entity extraction and community reports; read the global search code.
- HKUDS/LightRAG: How it combines graph and vector retrieval, and how incremental inserts work.
- neo4j/neo4j-graphrag-python: Knowledge-graph construction from text and the Text2Cypher retriever.
Checkpoint quiz
Interview questions
- When is a knowledge graph worth adding to a RAG system?
- Explain how Microsoft GraphRAG answers a global question.
- How would you build a knowledge graph from 10,000 incident reports? What goes wrong?
- How do you keep a graph index fresh when documents change daily?
- How would you safely let a model query a graph database?
Ask the professor
Sign in to ask questions about this module.
Discussion
Community discussion appears here once giscus is configured (see README).