opncrafter

Microsoft GraphRAG: Solving the "Global" Problem

Vector Databases are amazing at finding needles. They are terrible at summarizing the haystack. GraphRAG fixes this.

1. The Limitation of Standard RAG

Scenario: You upload 10,000 incident reports from a factory.

Query: "What are the top 3 recurring themes in these failures?"

Vector Search (Fail)

It searches for chunks containing "recurring themes". It retrieves 5 random reports. It misses the big picture entirely.

GraphRAG (Success)

It has pre-read every document, built a community map of issues, and answers: "Hydraulic leaks, Sensor drift, and Operator error."

2. How GraphRAG Works (The Pipeline)

Microsoft's approach involves a heavy indexing phase.

  1. Entity Extraction: An LLM reads every chunk and extracts Entities (People, Places, Machines) and Relationships.
  2. Community Detection: Specifically, it uses the Leiden Algorithm to cluster related entities into hierarchical communities.
  3. Summarization: The LLM generates a summary for every community at every level of the hierarchy.

Query Time: Map-Reduce

When you ask a global question, GraphRAG doesn't search raw text.
It searches the Community Summaries. This allows it to "read" the entire dataset at a high level of abstraction.

3. Cost & Trade-offs

GraphRAG is not a magic bullet. It has significant costs.

MetricVector RAGGraphRAG
Index CostCheap ($0.05 / 1k docs)Expensive ($5.00 / 1k docs)
Query SpeedFast (ms)Slow (needs map-reduce)
Use CaseFact Logic retrievalGlobal Summarization

When to use it?

Use GraphRAG for Discovery use cases ("What does this dataset tell us?").
Use Vector RAG for Search use cases ("Where is the invoice for June?").

4. Getting Started with Microsoft GraphRAG

Microsoft released GraphRAG as an open-source Python package. Here's how to set it up from scratch:

Step 1: Installation

pip install graphrag

# Initialize a new GraphRAG project
mkdir my-graphrag && cd my-graphrag
python -m graphrag.index --init --root .

Step 2: Configure Your Data and Model

# .env file
GRAPHRAG_API_KEY=your-openai-key

# settings.yml (simplified)
llm:
  api_key: $GRAPHRAG_API_KEY
  type: openai_chat
  model: gpt-4o-mini  # Use mini for indexing (cost savings)
  model_supports_json: true

embeddings:
  llm:
    api_key: $GRAPHRAG_API_KEY
    type: openai_embedding
    model: text-embedding-3-small

input:
  type: file
  file_type: text
  base_dir: "input"  # Place your .txt files here

Step 3: Run the Indexing Pipeline

Place your documents in the input/ folder, then run the expensive but one-time indexing process:

# This is expensive! For 1000 docs, expect ~$5-20 in API costs
python -m graphrag.index --root .

# Output will be in ./output/ directory
# Includes: entities, relationships, community_reports, etc.

Step 4: Query the Knowledge Graph

# Global query - for "what is the big picture?" questions
python -m graphrag.query --root . --method global \
  "What are the primary themes across all incident reports?"

# Local query - for specific entity questions  
python -m graphrag.query --root . --method local \
  "What failures involved the hydraulic system in Building A?"

5. Hybrid Strategy: GraphRAG + Vector RAG

The most effective production systems combine both approaches. Use GraphRAG's global search for high-level queries and standard vector search for specific lookups. Here's an intelligent router pattern:

def smart_rag_router(query: str, llm) -> str:
    """Route queries to the right RAG approach"""
    
    classification_prompt = f"""Classify this query:
    Query: "{query}"
    
    Return GLOBAL if it asks for: themes, trends, summaries, comparisons, 
    patterns across many documents.
    
    Return LOCAL if it asks for: specific facts, named entities, 
    dates, numbers, or exact information.
    
    Return only: GLOBAL or LOCAL"""
    
    query_type = llm.complete(classification_prompt).strip()
    
    if query_type == "GLOBAL":
        return graphrag_global_query(query)
    else:
        return vector_rag_query(query)

6. Real-World Use Cases

Corporate Knowledge Management

Large enterprises with thousands of internal documents—meeting notes, project reports, strategy documents—struggle with "I know we discussed this, but I can't find where." Standard vector search fails for meta-level questions like "What topics does the engineering team prioritize?" GraphRAG builds a community map of topics, people, and decisions across all documents, enabling queries that answer these organizational intelligence questions.

Scientific Literature Analysis

Research institutions use GraphRAG to analyze thousands of academic papers. A pharmaceutical company can ask "What are the most common drug interaction mechanisms studied in the last 5 years?" Standard RAG retrieves individual paper snippets—GraphRAG analyzes the entire corpus and provides a synthesized answer about patterns across the literature. This is invaluable for drug discovery and clinical research.

Legal Contract Analysis

Law firms analyzing complex M&A deals have hundreds of contracts, each with cross-references and dependencies. GraphRAG extracts entities (parties, obligations, dates, conditions) and relationships (guarantees, indemnities, change-of-control clauses) into a knowledge graph. Questions like "Which contracts are affected if Company X changes ownership?" can be answered by traversing the relationship graph—impossible with vector search alone.

Customer Feedback Intelligence

Product teams upload thousands of support tickets, reviews, and survey responses. Standard RAG finds individual mentions of specific issues. GraphRAG builds community clusters of related complaints, enabling questions like "What product areas generate the most negative sentiment, and how do they relate to each other?" This provides strategic insight rather than just tactical lookup.

Troubleshooting GraphRAG

Issue: Indexing costs too much

Fix: Use gpt-4o-mini for entity extraction (not GPT-4o). It costs 15x less and produces comparable quality for extraction tasks. Reserve GPT-4o only for the community summarization step which requires deeper reasoning.

Issue: Queries return generic answers

Fix: The quality of your answers is directly proportional to the quality of your input documents. Ensure documents are clean text (no garbled OCR), in English (multilingual support is limited), and sufficiently long (at least 500 words each). Very short documents don't provide enough context for entity extraction.

Issue: Missing relationships in the graph

Fix: Adjust the entity_extraction.max_gleanings parameter in settings.yml. Increasing it from 1 to 2-3 causes the LLM to do multiple passes over each document, catching more subtle relationships at higher cost.

Frequently Asked Questions

How is GraphRAG different from a traditional Knowledge Graph?

Traditional Knowledge Graphs are manually curated (a human defines the schema and populates it). GraphRAG uses LLMs to automatically extract entities and relationships from unstructured text, creating a knowledge graph with zero manual work. The tradeoff is higher cost and occasional extraction errors, but the automation makes it practical for large corpora.

Can GraphRAG answer specific factual questions?

Yes, using "local search" mode which searches specific entities and their immediate relationships rather than community summaries. However, for pure fact retrieval, standard vector RAG is faster and cheaper. GraphRAG's true advantage is global/thematic queries.

How often should I re-index when documents change?

GraphRAG doesn't yet support incremental indexing—any new documents require a full re-index. For frequently updated corpora, this makes GraphRAG expensive to maintain. Workaround: use vector RAG for new documents (last 30 days) and GraphRAG for the stable archive (older than 30 days).

What's the difference between Global and Local search?

Global search uses community summaries to answer broad, thematic questions—it "reads" the entire dataset's structure. Local search uses individual entities and their connections to answer specific questions about named people, places, or events. Use global for "What are the themes?" and local for "Tell me about Entity X."

Next Steps

  • Start with a Small Dataset: Begin with 20-50 documents to understand the indexing cost and quality before committing to a large corpus.
  • Compare Query Quality: Run the same 10 questions through standard vector RAG and GraphRAG. The difference for global questions will be striking.
  • Build the Hybrid Router: Implement the smart router pattern above to automatically route queries to the best approach.
  • Monitor Indexing Costs: Set up OpenAI usage alerts before running large indexing jobs to prevent unexpected bills.

Continue Reading

👨‍💻
Written by

Vivek

AI Engineer

Full-stack AI engineer with 4+ years building LLM-powered products, autonomous agents, and RAG pipelines. I've shipped AI features to production for startups and worked hands-on with GPT-4o, LangChain, LlamaIndex, and the Vercel AI SDK. I started OpnCrafter to share everything I wish I had when learning — no fluff, just working code and real-world context.

GPT-4oLangChainNext.jsVector DBsRAGVercel AI SDK