liveBook · Manning

LightRAG

As explained in Retrieval Augmented Generation, The Foundational Ideas

LightRAG is a retrieval-augmented generation architecture that combines graph-based query routing with retrieval over unstructured vector representations.

A contract archive may need to answer both “When does this supplier’s agreement renew?” and “What patterns recur across vendor disputes?” Vector search is useful for finding relevant chunks, but it can miss relationships or patterns spread across documents. LightRAG combines a graph of entities and relationships with vector retrieval, so specific names and broader themes can take different retrieval paths. section 12.1 section 12.4

The two paths depend on an offline indexing step:

  • Indexing extracts entities from chunks, gathers their mentions, and generates one descriptive profile per entity. The resulting graph consolidates information scattered across documents. section 12.4.1 section 12.4.3
  • At query time, one LLM call extracts both specific, low-level keywords and abstract, high-level keywords. The low-level path looks up entity profiles and immediate neighbors; the high-level path retrieves concept nodes or matching vector chunks. section 12.4.2
  • The paths can run together: a question about suppliers affected by a recall and the resulting exposure needs named entities as well as a broader risk theme. Their contexts are merged for answer generation. A critic can check that context and request another routing pass before an answer is written. section 12.4.2
Listing 12.5 Building deduplicated entity profiles.
from typing import Dict, List
import json
 
class LightRAGIndexer:
    """Build a deduplicated entity graph from text chunks."""
 
    def __init__(self, llm, graph: nx.DiGraph):
        """Bind to an LLM client and an empty knowledge graph."""
        self.llm = llm
        self.graph = graph
        self.mentions: Dict[str, List[str]] = {}  #A
 
    def index_chunk(self, chunk_id: str, text: str) -> None:
        """Extract entities and accumulate per-entity context."""
        prompt = (
            "Extract entities and short descriptions from this text. "
            "Return JSON: [{\"entity\": str, "
            "\"type\": str, \"desc\": str}].\n"
            f"Text: {text}"
        )
        items = json.loads(self.llm.complete(prompt))  #B
        for item in items:
            name = item["entity"]
            self.mentions.setdefault(name, []).append(item["desc"])  #C
            if name not in self.graph:
                self.graph.add_node(name, type=item["type"])
 
    def finalize_profiles(self) -> None:
        """Compress each entity's mentions into one profile."""
        for name, descriptions in self.mentions.items():
            joined = "\n".join(descriptions)
            profile_prompt = (
                f"Combine these mentions of {name} into a "
                f"short profile:\n{joined}"
            )
            self.graph.nodes[name]["profile"] = (  #D
                self.llm.complete(profile_prompt))
 

The indexing listing shows how extracted mentions accumulate into deduplicated entity profiles for later lookup. section 12.4.3

Listing 12.6 The dual router.
from typing import List, Dict, Literal
 
class DualPathRetriever:
    """Routes queries to entity inspection or theme aggregation."""
 
    def __init__(self, vector_store, graph_store, llm):
        """Initialize stores and the LLM client."""
        self.v_store = vector_store
        self.g_store = graph_store
        self.llm = llm
 
    def route_query(
        self, query: str, mode: Literal["mix", "low", "high"]
    ) -> str:
        """Execute dual-path retrieval and synthesis."""
        keywords = self._extract_keywords(query)  #A
        contexts = []
 
        if mode in ["low", "mix"]:
            for entity in keywords['low_level']:
                profile = self.g_store.get_profile(entity)  #B
                contexts.append(f"Entity: {entity}\n{profile}")
 
        if mode in ["high", "mix"]:
            for theme in keywords['high_level']:
                results = self.v_store.search(theme, top_k=3)  #C
                contexts.append(f"Theme: {theme}\n{results}")
 
        return self._synthesize_answer(query, contexts)
 

The router listing makes the low-level graph lookup and high-level vector lookup explicit; its answer-synthesis method is omitted. section 12.4.3

This design trades exhaustive coverage for cheaper retrieval. On a reported legal-dataset comparison, GraphRAG reads roughly 610,000 tokens of community reports for one question, while LightRAG’s keyword-extraction and lookup step uses under 100 tokens and one LLM call. That figure describes retrieval, not the later answer-generation call. LightRAG is a fit when an existing vector index answers many queries, but entity structure would help without the full cost of precomputed community summaries. section 12.4 section 12.4.3

A supplier hidden one step too far

In an eleven-node supplier graph, a query about suppliers hit by a 2023 recall does not put the correct supplier in LightRAG’s retrieved context. The supplier lies two hops from the named recall, while the low-level path expands only one hop. Its broader-theme path does not close the gap. Check retrieval coverage on such queries before relying on the cheaper path; a broad community-report search finds the supplier here, but reads more context. section 12.4.4

How Retrieval Augmented Generation, The Foundational Ideas covers it

How deep it goes
Works through entity-profile indexing and a Python dual-path router, but omits the router example’s answer-synthesis method.
Examples
Uses contract questions, Python indexing and routing listings, and a supplier-recall graph that exposes a missed two-hop answer.
What it assumes
Assumes familiarity with chunk embeddings and vector retrieval; defines graph traversal and related retrieval vocabulary before introducing the hybrid.
What you can do after
Build it from scratch with entity profiles and dual-path retrieval, then evaluate where one-hop expansion misses required context.
How it connects
Connects knowledge graphs, entity extraction, vector search, GraphRAG, and HippoRAG through retrieval-cost and coverage tradeoffs.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Retrieval Augmented Generation, The Foundational Ideas. The section links point to the parts of the book it draws on.