Area 02 · RAG · Concept 17 of 20

GraphRAG

Build a graph of entities and their relations, and retrieve over it for connect-the-dots questions.

11–20 · Production levelPhase 2 · Weeks 7–10≈ 2 hours

The idea

Some questions need facts scattered across many documents: "Which suppliers are linked to projects that missed deadlines?" Plain chunk retrieval finds pieces but not the connections. GraphRAG extracts entities (people, companies, products) and relationships with an LLM, stores them as a graph, and retrieves by walking the graph.

Microsoft’s GraphRAG also clusters the graph into communities and writes summaries for each, which helps with global questions like "What are the main themes across these 500 reports?"

It is expensive to build (many LLM calls) and harder to keep current. Use it when relationship questions matter; start with normal RAG first.

Why it matters

It answers multi-hop and whole-corpus questions that vector search structurally cannot.

Picture it

Words to know

  • Knowledge graphNodes (entities) connected by typed edges (relations).
  • Multi-hop questionNeeds combining facts from several places.

Try it without code

Draw the graph

  1. Read 3 short news articles about one industry.
  2. Draw people and companies as boxes and relations as arrows.
  3. Write a question only the drawing can answer.

Use it in code

Extract triples with an LLM and query them with networkx.

python
import json, anthropic, networkx as nx
client = anthropic.Anthropic()

def triples(text):
    msg = client.messages.create(model="claude-haiku-5-5", max_tokens=500, temperature=0,
        messages=[{"role": "user", "content":
            f"Extract (subject, relation, object) triples as a JSON list of 3-item lists.\n{text}"}])
    return json.loads(msg.content[0].text)

G = nx.DiGraph()
for s, rel, o in triples("Acme supplies Project Atlas. Atlas missed its Q3 deadline. Maria leads Atlas."):
    G.add_edge(s, o, relation=rel)
print(list(G.edges(data=True)))
print(nx.descendants(G, "Acme"))
PRACTICE · TICK IT AFTER THIS

Build a small graph from 20 documents and answer 3 multi-hop questions by graph traversal plus an LLM summary.

Common mistakes

  • Duplicate entities ("Acme", "Acme Ltd") with no merging.
  • Building a graph when plain RAG would answer the questions.

Go deeper