How to build a GraphRAG application: a practical implementation guide

Vector search misses thematic relationships across large document collections. Here is a practical engineering guide to building a GraphRAG system from scratch, from entity extraction to community summarization.

JobsDart Editorial4 min read

Key takeaways

  • GraphRAG requires a two-phase architecture: an offline knowledge-graph extraction pipeline and an online dual-mode retrieval engine.
  • Entity and relationship extraction uses structured JSON prompting with gleaning passes to catch overlooked connections.
  • Hierarchical community detection (like the Leiden algorithm) clusters densely connected entities into multi-level topical groups.
  • Community summaries pre-compute answers to broad, corpus-wide questions before the user ever types a query.
  • Query processing routes dynamically: local point queries traverse direct graph edges, while global thematic queries inspect community summaries.

The two-phase architecture of a GraphRAG system

Building a GraphRAG system requires shifting your mental model from simple vector indexing to automated knowledge engineering. Unlike traditional RAG, which processes documents independently, GraphRAG maps the connections between all documents in your collection.

A complete GraphRAG implementation consists of two distinct operational phases: the Offline Graph Extraction & Clustering Pipeline, and the Online Retrieval & Synthesis Engine.

During the offline phase, your system reads source text, extracts entities and relationships, builds a graph network, and generates hierarchical community summaries. During the online phase, the system uses this pre-computed graph to answer both local entity lookups and global thematic questions.

Phase 1: Entity extraction and gleaning passes

The foundation of GraphRAG is accurate entity and relationship extraction. You split your raw text into chunks (typically 600 to 1,000 tokens) and pass each chunk to an LLM with a specialized extraction prompt.

The prompt instructs the model to identify named entities (e.g. `Organization`, `Person`, `Location`, `Technology`, `Event`) and the specific directed relationships connecting them (e.g. `ACQUIRED`, `INVESTIGATED`, `DEPENDS_ON`).

Crucially, a single extraction pass often misses subtle relationships. Production systems implement "gleaning passes": asking the model a follow-up question ("Are there any additional entities or relationships in this text that you missed?") until the model returns an empty list. This increases relationship yield by up to 35%.

Graph extraction data model schema
Data ElementAttributes StoredExample Instance
Entity (Node)Name, Type, Description, Source ChunksStripe | Organization | Online payment infrastructure provider
Relationship (Edge)Source, Target, Description, Weight, Source ChunksStripe -> Paystack | ACQUIRED | Stripe acquired Paystack for $200M in 2020
Community (Cluster)Level, Member Entities, SummaryFintech M&A | Cluster of payment providers and African expansion deals

Phase 2: Community detection and hierarchical summarization

Once nodes and edges are extracted from all documents, the raw graph typically contains thousands of interconnected points. Querying this massive graph directly at inference time would overwhelm model context windows.

GraphRAG solves this by applying community detection algorithms — specifically the Leiden algorithm — to partition the graph into a hierarchy of clusters. Closely related entities are grouped into Level-0 micro-communities, which roll up into Level-1 meso-communities, and finally Level-2 macro-communities.

For each detected community, an LLM generates a pre-computed executive summary: describing the overarching narrative, key actors, controversial claims, and unresolved questions within that cluster. These community summaries are what allow GraphRAG to answer global questions ("What were the main regulatory controversies across all portfolio companies?") in seconds.

Frequently asked questions

How do you build a GraphRAG application?

To build GraphRAG: 1) Parse documents and extract entities and relationships using an LLM, 2) Store them as a knowledge graph, 3) Run community detection (like Leiden) to cluster related entities, 4) Generate pre-computed summaries for each cluster, and 5) Route user queries to either local graph traversals or global community summaries.

What is entity gleaning in GraphRAG?

Gleaning is a technique where an LLM is prompted multiple times on the same text passage to discover secondary entities and subtle relationships that were overlooked during the initial extraction pass.

What is the Leiden algorithm used for in GraphRAG?

The Leiden algorithm is a community detection algorithm that partitions a large graph network into hierarchical clusters of densely connected nodes, enabling structured summarization at different abstraction levels.

What is the difference between Local Search and Global Search in GraphRAG?

Local Search answers specific questions about individual entities by inspecting their immediate graph neighbors. Global Search answers broad thematic questions by querying pre-computed community summaries across the entire dataset.

Can you build GraphRAG using PostgreSQL?

Yes. PostgreSQL can easily store graph nodes, directed edges, and community summaries in standard relational tables with pgvector, avoiding the operational overhead of a separate graph database.

Further reading

Check this against your own resume

Scan your CV against a real job description, or build a parse-safe one from scratch. Your first scan costs nothing.

Keep reading

All career guides