Skip to content

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.

Chunksfrom Module 3Extractentities + relationsGraphnodes, edges, claimsCommunitiesLeiden clusteringSummariesone per communityLocal question“who owns X?”Entity + 1 hopplus their chunksGlobal question“main themes?”Map-reduceover summariesIndexing (expensive: many LLM calls)Querying
Plain RAG can’t answer “what are the main themes across all 5,000 documents?”. A graph can, at a much higher indexing cost.

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.

Common misconception: “GraphRAG is better RAG.” It is a different tool for different questions, with a much higher indexing cost. For single-fact lookups, hybrid search is cheaper and usually better.

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.

  1. Extract. An LLM reads every chunk and lists entities (services, teams, vendors) and the relationships between them, with short descriptions.
  2. Merge. The same entity found in different chunks becomes one node, and its descriptions are summarised.
  3. Cluster. The Leiden algorithm splits the graph into communities of densely connected entities, at several levels: a few big communities, many small ones.
  4. Summarise. An LLM writes a report for each community: its members, key relationships and claims.
  5. 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.
  6. 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.

Tip: Start with a cheap middle ground: tag chunks with linked entities at ingestion, then expand retrieval to chunks that share entities with the top results.

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 code

Labs

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

Further reading

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

1. Which question suits GraphRAG’s global search?
2. What makes GraphRAG expensive?
3. “Acme Ltd” and “ACME” appear as two nodes. What step is missing?
4. You already have a service catalogue. Best graph source?
5. Safest way to run model-written Cypher?

Interview questions

  1. When is a knowledge graph worth adding to a RAG system?
  2. Explain how Microsoft GraphRAG answers a global question.
  3. How would you build a knowledge graph from 10,000 incident reports? What goes wrong?
  4. How do you keep a graph index fresh when documents change daily?
  5. 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).