# nano-graphrag

> A simple, easy-to-hack GraphRAG implementation in about 1100 lines

- Category: Retrieval & Knowledge (RAG) / RAG Frameworks & Platforms
- Language: Python
- Repository: https://github.com/gusye1234/nano-graphrag
- Canonical page: https://ai-tldr.dev/tools/nano-graphrag/

## In short

nano-graphrag is a compact Python reimplementation of GraphRAG, the graph-based retrieval-augmented generation approach. It keeps the core functionality of the original Microsoft GraphRAG while shrinking the codebase to about 1100 lines (excluding tests and prompts), so you can actually read and change it. It sits in the RAG Frameworks & Platforms category of the Retrieval & Knowledge (RAG) tools and is open-source, written in Python.

## Overview

nano-graphrag is a compact Python reimplementation of GraphRAG, the graph-based retrieval-augmented generation approach. It keeps the core functionality of the original Microsoft GraphRAG while shrinking the codebase to about 1100 lines (excluding tests and prompts), so you can actually read and change it.

It is aimed at developers who want a graph-based retrieval codebase they can study and modify, rather than a large framework that is hard to follow. The project is asynchronous and fully typed, and it supports both global and local query modes plus a naive RAG fallback.

As a RAG framework, it fits the case where plain vector retrieval is not enough and you want entities and communities extracted into a graph. It is portable: you can swap in different LLMs, embedding models, vector databases, and graph storage backends.

## What it does

- Around 1100 lines of code (excluding tests and prompts), built to be read and hacked
- Global and local GraphRAG search modes, plus an optional naive RAG mode
- Incremental and batch insert with md5-hash de-duplication so repeated content is not recomputed
- Async API: every NAME(...) method has a matching aNAME(...) async version
- Pluggable components — OpenAI, Amazon Bedrock, DeepSeek and ollama LLMs; OpenAI, Bedrock and sentence-transformers embeddings; nano-vectordb, hnswlib, milvus-lite and faiss vector stores; networkx graph storage
- Reloads all context automatically when you re-initialize GraphRAG from the same working_dir

## Getting started

Install the package, set your OpenAI API key, then insert some text and query it.

1. Install

   Install from PyPI, or from source by cloning the repo and running an editable install.

```bash
pip install nano-graphrag
```

2. Set your OpenAI API key

   nano-graphrag reads the OpenAI key from the environment.

```bash
export OPENAI_API_KEY="sk-..."
```

3. Insert text and query

   Create a GraphRAG with a working directory, insert a document, then run a global or local search. The same working_dir reloads context automatically next time.

```python
from nano_graphrag import GraphRAG, QueryParam

graph_func = GraphRAG(working_dir="./dickens")

with open("./book.txt") as f:
    graph_func.insert(f.read())

# Global graphrag search
print(graph_func.query("What are the top themes in this story?"))

# Local graphrag search
print(graph_func.query("What are the top themes in this story?", param=QueryParam(mode="local")))
```

## When to use it

- Studying or experimenting with how GraphRAG works without wading through a large official codebase
- Building a graph-based RAG pipeline over a document collection where entity and community structure matters
- Prototyping with alternative backends — swapping in ollama, faiss, milvus-lite, or sentence-transformers in place of OpenAI defaults
- Incrementally ingesting a growing corpus and re-querying it with global or local search modes
