AnyLearn
All lessons
AIadvanced

GraphRAG: Knowledge-Graph Augmented Retrieval

Go beyond dense-vector search: learn how Microsoft GraphRAG extracts entities, builds a knowledge graph, clusters it with the Leiden algorithm, and serves both local and global queries with community summaries — delivering answers that classic RAG cannot.

Not signed in — your progress and quiz score won't be saved.
Lesson progress1 / 12

Why vanilla RAG hits a ceiling

Standard RAG works by embedding chunks of text, storing them in a vector index, and retrieving the top-k closest chunks at query time. For narrow, factual look-ups — "what is the capital of France?" — this is fine. It breaks down for global, sensemaking queries like "what are the main themes across this entire corpus?" or "how are these companies connected?"

The problem is structural: embedding similarity finds locally relevant passages but cannot synthesize information scattered across thousands of documents. A chunk about Company A's CEO and a chunk about Company B's acquisition sit at different addresses in embedding space; no single query pulls both into context simultaneously.

Microsoft Research's GraphRAG (Edge et al., 2024) replaces the flat chunk index with a knowledge graph + community hierarchy, enabling genuine cross-document reasoning without asking the LLM to read the whole corpus.

Full lesson text

All 12 steps on one page — for reading, reference, and search.

Show

1. Why vanilla RAG hits a ceiling

Standard RAG works by embedding chunks of text, storing them in a vector index, and retrieving the top-k closest chunks at query time. For narrow, factual look-ups — "what is the capital of France?" — this is fine. It breaks down for global, sensemaking queries like "what are the main themes across this entire corpus?" or "how are these companies connected?"

The problem is structural: embedding similarity finds locally relevant passages but cannot synthesize information scattered across thousands of documents. A chunk about Company A's CEO and a chunk about Company B's acquisition sit at different addresses in embedding space; no single query pulls both into context simultaneously.

Microsoft Research's GraphRAG (Edge et al., 2024) replaces the flat chunk index with a knowledge graph + community hierarchy, enabling genuine cross-document reasoning without asking the LLM to read the whole corpus.

2. GraphRAG pipeline at a glance

The pipeline has two distinct phases: indexing (top half) builds the graph and community summaries offline; querying (bottom half) routes to global or local search depending on query type.

flowchart TD
  A["Raw documents"] --> B["Entity & relationship extraction"]
  B --> C["Knowledge graph"]
  C --> D["Leiden clustering"]
  D --> E["Community summaries (LLM)"]
  E --> F["Community report store"]
  C --> G["Text-unit embeddings"]
  G --> H["Vector index"]
  F --> I["Global search"]
  H --> J["Local search"]
  I --> K["Final answer"]
  J --> K

3. Step 1 — Entity and relationship extraction

The indexer passes each text chunk to an LLM with a prompt that asks it to identify entities (people, organizations, events, concepts) and relationships between them, returning structured JSON.

A simplified extraction prompt result looks like:

{
  "entities": [
    {"name": "OpenAI", "type": "ORGANIZATION", "description": "AI research lab"},
    {"name": "Sam Altman", "type": "PERSON", "description": "CEO of OpenAI"}
  ],
  "relationships": [
    {"source": "Sam Altman", "target": "OpenAI", "description": "CEO of", "weight": 9}
  ]
}

The same entity can appear in hundreds of chunks. GraphRAG merges duplicate mentions by name-normalisation and co-reference, accumulating a richer description over occurrences. Relationship weights are summed across chunks, so edges with many supporting passages end up with higher weight — a signal of evidential strength, not just semantic similarity.

4. Step 2 — Building the knowledge graph

After extraction the merged entities become nodes and the relationships become weighted, directed edges. The result is a heterogeneous property graph stored (in the reference implementation) as a set of Parquet files — nodes.parquet, edges.parquet — alongside the raw text units.

Graph stats for a 1 M-token corpus are typically:

  • ~5 000–20 000 entity nodes
  • ~15 000–60 000 relationship edges
  • Average degree: 6–10 edges per node

This scale is still tractable for graph algorithms, but far too large to summarise naively. That's where clustering enters.

Heads up: Entity extraction quality is the biggest quality lever in the whole pipeline. Prompt-engineering the extraction step (adding domain-specific entity types, giving few-shot examples) yields more improvement than tuning downstream parameters.

5. Step 3 — Hierarchical Leiden clustering

GraphRAG partitions the knowledge graph using the Leiden algorithm (Traag et al., 2019), a community-detection method that optimises modularity faster and with better quality guarantees than the older Louvain algorithm.

Leiden is run at multiple resolution levels, producing a hierarchy:

LevelApprox. communitiesGrain
0 (coarse)5–20macro-themes
150–200topic clusters
2 (fine)500–2000sub-topics

Each node belongs to exactly one community per level. The hierarchy lets the system answer queries at the right granularity — broad questions get routed to level-0 summaries; specific questions can go deeper.

In code, the reference implementation calls graspologic under the hood:

from graspologic.partition import hierarchical_leiden

communities = hierarchical_leiden(
    graph,          # networkx Graph with edge weights
    max_cluster_size=10,
    random_seed=42
)

6. Step 4 — Community summarization

For each community at each level, GraphRAG collects all the entities and relationships in that community, formats them as a structured context block, and asks an LLM to write a community report: a multi-paragraph summary covering key entities, their roles, notable relationships, and any claims or events.

The prompt structure (abridged) is:

You are given a knowledge graph community.
Entities: OpenAI (AI research lab), Sam Altman (CEO), ...
Relationships: Sam Altman --CEO of--> OpenAI (weight: 9), ...

Write a concise report covering the main themes,
key entities, and notable findings. Max 200 words.

With a 1 M-token corpus and ~500 level-2 communities, this phase makes ~500 LLM calls. That's the main indexing cost. The payoff: you now have a compressed, human-readable summary for every sub-graph — a semantic map of the corpus that no vector index can provide.

7. Global search — answering holistic questions

Global search targets queries that need synthesis across the whole corpus: "What are the recurring concerns about AI safety in these documents?"

The process:

  1. Collect all community reports at a chosen level (usually level 1 or 2).
  2. Shuffle and batch them into context windows.
  3. Send each batch to the LLM asking: "Here are community reports. What is relevant to the query? Return a rated partial answer."
  4. Aggregate rated partial answers, weighted by relevance score, into a final answer.

This is a map-reduce pattern over community summaries. It can process millions of tokens of source material because the summaries are already compressed. The critical parameter is response_type — setting it to "multiple paragraphs" or "single paragraph" shapes the aggregation.

Heads up: Global search is slow (5–30 s, many LLM calls) and expensive. Use it only for questions that genuinely require cross-document synthesis. Routing matters.

8. Local search — answering entity-centric questions

Local search targets questions about specific entities or events: "What did Sam Altman say about AGI timelines?"

It works more like augmented vector RAG:

  1. Embed the query and retrieve the top-k most relevant entities from the entity store.
  2. Follow graph edges to collect related entities, relationships, and co-membered community reports.
  3. Retrieve the original text units (chunks) linked to those entities.
  4. Pack entities + relationships + community summaries + raw text into the LLM context.

The graph traversal is what distinguishes this from vanilla RAG. If the query mentions Company A, local search also surfaces Company A's partners, subsidiaries, and the events they co-appear in — context that embedding search would miss unless those chunks happened to score high similarity.

Typical latency: 2–8 s, ~3–6 LLM calls.

9. Global vs Local search routing

Routing can be rule-based (keywords like "overall", "across all", "themes" → global) or LLM-classified. Microsoft's reference implementation defaults to local search and requires explicit configuration for global.

flowchart LR
  Q["User query"] --> R["Query router"]
  R --> G["Global search"]
  R --> L["Local search"]
  G --> CS["Community summaries (all levels)"]
  CS --> MR["Map-reduce aggregation"]
  MR --> A["Answer"]
  L --> EV["Entity vector lookup"]
  EV --> GT["Graph traversal"]
  GT --> TX["Text-unit retrieval"]
  TX --> A

10. Running GraphRAG in practice

The Microsoft reference implementation is a Python package:

pip install graphrag

# 1. Initialise a project
graphrag init --root ./my-project

# 2. Drop your .txt/.pdf files into ./my-project/input/

# 3. Run the indexing pipeline (builds graph + summaries)
graphrag index --root ./my-project

# 4. Query
graphrag query \
  --root ./my-project \
  --method global \
  --query "What are the main themes in these documents?"

Key config levers in settings.yaml:

  • entity_extraction.max_gleanings — how many extraction passes per chunk (default 1; raise to 2–3 for dense technical text)
  • community_reports.max_length — token budget per community report
  • leiden.max_cluster_size — trades community granularity vs. summary cost
  • local_search.text_unit_prop — fraction of context window reserved for raw text chunks vs. graph context

11. Cost, latency, and when to use it

GraphRAG is not a drop-in replacement for every RAG workload. The trade-offs are stark:

DimensionVector RAGGraphRAG
Index build costLow ($0.05/MB)High ($2–10/MB)
Query latency~500 ms2–30 s
Cross-doc synthesisPoorExcellent
Entity-centric Q&AMediocreVery good
Factual look-upGoodGood
Incremental updatesEasy (re-embed)Hard (re-extract + re-cluster)

GraphRAG earns its cost when:

  • The corpus is stable (reports, research papers, legal documents) not a live feed.
  • Queries are thematic or relational rather than point look-ups.
  • You need explainable provenance — the community report links back to source entities and text units.

For most production chatbots over a knowledge base, a hybrid is optimal: use local GraphRAG search for entity questions, fall back to vector search for narrow factual queries.

12. Gotchas and production hardening

Things that bite teams in production:

  • Extraction hallucinations: LLMs sometimes invent entity names or misattribute relationships. Validate with a stricter extraction prompt or post-process with a second pass that checks entity names against a dictionary.
  • Entity deduplication failures: "OpenAI", "Open AI", and "OPENAI" end up as three separate nodes unless you normalise before merge. The reference implementation uses case-folding; you may need fuzzy matching for messy real-world text.
  • Community report token bloat: A very dense community (high-degree hub node) can generate a context window overflow. Cap with max_input_tokens and filter to top-N entities by degree.
  • Re-indexing cost: Even a small corpus update triggers full re-extraction for changed chunks and potentially re-clustering. Plan for incremental indexing (only re-run changed chunks) if content updates frequently.
  • Prompt injection via document content: If documents are untrusted, the extraction prompt can be hijacked. Wrap document content in XML delimiters and instruct the model to treat anything inside as data, not instructions.

Check your understanding

The lesson ends with a 5-question quiz. Take it in the player above to see your score.

  1. Which query type in GraphRAG uses a map-reduce pattern over community summaries to answer thematic questions?
    • Local search
    • Global search
    • Hybrid search
    • Vector search
  2. What does the Leiden algorithm do in the GraphRAG pipeline?
    • Embeds entity names into a vector space
    • Extracts named entities from text chunks
    • Partitions the knowledge graph into hierarchical communities
    • Generates community summaries using an LLM
  3. In the GraphRAG entity extraction phase, relationship weights are primarily derived from:
    • The cosine similarity between entity embeddings
    • The number of text chunks that support the relationship
    • The LLM's confidence score on each extraction call
    • The token length of the entities involved
  4. Compared to standard vector RAG, GraphRAG local search retrieves additional context by:
    • Increasing the top-k chunk count from the vector index
    • Running BM25 keyword search alongside embeddings
    • Traversing graph edges from matched entities to related nodes and text units
    • Re-ranking chunks with a cross-encoder after vector retrieval
  5. Which of the following is the most accurate reason GraphRAG re-indexing is expensive after a corpus update?
    • Vector embeddings must be regenerated for all existing chunks
    • Entity extraction and potentially full re-clustering must be rerun
    • Community report prompts are cached and cannot be invalidated
    • The Leiden algorithm requires a sorted adjacency matrix to start

Related lessons

AI
intermediate

RAG Evaluation in Production: Metrics, Tools, and Cadence

Learn how to systematically evaluate Retrieval-Augmented Generation systems in production using RAGAS, TruLens, and Phoenix — covering golden sets, retrieval drift, embedding drift, and cost-aware eval scheduling.

12 steps·~18 min·audio
AI
advanced

Loop engineering: verification, orchestration, and anti-patterns

Make loops trustworthy. Adversarial verification panels, sub-agent orchestration, loop-until-dry for unbounded discovery, loop-until-budget for paid depth, multi-modal sweeps with diverse prompts, eval-driven cap selection, and the five anti-patterns — silent caps, infinite plans, drift, premature termination, thrashing — that ship to prod more than they should.

9 steps·~14 min
AI
intermediate

Loop engineering in production: state, errors, and budgets

Once an agent loop runs past a few iterations the failure surface shifts: state bloats, tool calls fail in three distinct ways, retries multiply if you stack them wrong, and unbounded budgets rack up four-figure bills overnight. The state, errors, and budgets you need to make the loop survive contact with reality.

8 steps·~12 min
AI
beginner

Loop engineering basics: the agent control loop

How LLM agents actually run: the iterative prompt-action-observation loop, the ReAct shape, the smallest tool-calling loop in twelve lines, why you always stack three termination layers, what the model sees on iteration N, and when a single prompt is the better answer.

8 steps·~12 min