LightRAG Architecture Diagram: How the Codebase Works
Last analyzed
Project Overview
LightRAG is a Retrieval-Augmented Generation (RAG) system designed to provide accurate, grounded responses from large language models (LLMs) by leveraging proprietary and domain-specific knowledge. It achieves this through a dual-layer approach: a knowledge graph for structured information and conceptual relationships, and vector databases for semantic similarity searches over document chunks, entities, and relationships. LightRAG enables administrators to ingest and manage documents, curate the knowledge graph, and finely tune RAG parameters, while providing end-users with intuitive web interfaces (an administrative console and a simplified workspace) to query the system and receive contextually rich LLM-generated answers. It solves the critical problem of LLM 'hallucinations' by ensuring responses are anchored to a verifiable and extensible knowledge base.
- Category
- library
- Difficulty
- intermediate
- Tech Stack
- Python
- Author
- HKUDS
- Tags
- rag, graph-rag, knowledge-graph, python
LightRAG is part of our RAG architecture collection, where similar codebases are broken down side by side. Or browse the full architecture gallery.
How LightRAG Works
LightRAG is a Retrieval-Augmented Generation (RAG) system designed to provide accurate, grounded responses from large language models (LLMs) by leveraging proprietary and domain-specific knowledge. It achieves this through a dual-layer approach: a knowledge graph for structured information and conceptual relationships, and vector databases for semantic similarity searches over document chunks, entities, and relationships. LightRAG enables administrators to ingest and manage documents, curate the knowledge graph, and finely tune RAG parameters, while providing end-users with intuitive web interfaces (an administrative console and a simplified workspace) to query the system and receive contextually rich LLM-generated answers. It solves the critical problem of LLM 'hallucinations' by ensuring responses are anchored to a verifiable and extensible knowledge base.
Data Flow
Data primarily flows in a client-server architecture. On the frontend, user input from `RetrievalView.tsx` or `DocumentManager.tsx` is captured, potentially managed by Zustand stores like `lightrag_webui/src/stores/querySettings.ts` or `lightrag_webui/src/stores/graph.ts`. This data is then serialized (e.g., to JSON) and sent as HTTP requests to the FastAPI backend via the `lightrag_webui/src/api/lightrag.ts` client. The backend, handled by `lightrag/api/routers/` modules (e.g., `query_routes.py`, `document_routes.py`, `graph_routes.py`), processes these requests. This involves deserializing the input, interacting with core RAG components like document parsers (`ir_builder.py` modules), embedding models, vector databases, and the knowledge graph store. Persistent data (documents, graph structure, embeddings) is stored in various backend services (e.g., file system, database, vector stores). Processed results or status updates are then serialized back into HTTP responses and sent to the frontend. The frontend deserializes these responses and updates its local state (e.g., `retrievalHistory.ts`) and UI components (e.g., `RetrievalView.tsx` message list, `GraphViewer.tsx` visualization). Offline tools like `rebuild_vdb.py` interact directly with the backend data stores without involving the API or frontend.
Get a free architecture diagram of your own codebase
Every diagram on this page was generated automatically — no setup, no annotations. Free architecture diagram generator — try your own codebase — paste a GitHub URL or drop a ZIP, and the diagrams, module map and execution flows come back in about a minute. First project is free.
Key Modules & Components
- Knowledge Base Ingestion & Processing: This module manages the entire lifecycle of documents and data used by LightRAG. It provides the backend APIs and frontend interfaces for uploading, parsing, and transforming raw documents into a structured Intermediate Representation (IRDoc), extracting entities and relationships, and generating semantic embeddings. These processed components are then stored in the system's knowledge graph and specialized vector databases. It also includes utilities for maintaining the integrity of these knowledge stores, such as rebuilding vector databases.
Key files: lightrag/api/routers/document_routes.py, lightrag/parser/docx/ir_builder.py, lightrag/parser/markdown/ir_builder.py - Retrieval-Augmented Generation (RAG) Engine: This is LightRAG's core intelligence module, responsible for orchestrating the process of generating accurate, grounded responses to user queries. It receives natural language queries, performs sophisticated multi-stage retrieval from the knowledge graph and vector databases, contextual re-ranking, and constructs enriched prompts. These prompts are then sent to Large Language Models (LLMs) to generate the final, contextually relevant answers. The module also exposes an API compatible with Ollama, facilitating broader integration.
Key files: lightrag/api/routers/query_routes.py, lightrag/api/routers/ollama_api.py, lightrag_webui/src/features/RetrievalView.tsx - Knowledge Graph Visualization & Exploration: This module empowers administrators and users to visually interact with and explore LightRAG's structured knowledge graph. It includes backend APIs for retrieving graph data and a sophisticated frontend component that renders entities as nodes and relationships as edges. Users can intuitively navigate, search, apply layout algorithms, and inspect detailed properties of the graph, offering profound insights into the system's conceptual understanding of its knowledge base.
Key files: lightrag/api/routers/graph_routes.py, lightrag_webui/src/features/GraphViewer.tsx, lightrag_webui/src/hooks/useLightragGraph.tsx - Administrative UI Customization: This module provides the administrative capabilities for personalizing and localizing the LightRAG web interface. It includes a backend API for serving dynamic UI assets and configuration data, allowing administrators to easily modify welcome messages, brand logos, login blurbs, and other aesthetic elements without requiring a frontend redeployment. This ensures a tailored and branded user experience.
Key files: lightrag/api/routers/ui_customization_routes.py - Application Platform & Deployment: This module encompasses the foundational infrastructure for configuring, building, and deploying the LightRAG application. It includes core backend configuration settings, Gunicorn server setup for production, Docker containerization definitions, and project dependency manifests. Its purpose is to ensure the application's reliable setup, execution, and scalability across different environments.
Key files: lightrag/api/config.py, lightrag/api/gunicorn_config.py, Dockerfile - Web Application Core & API Integration: This module forms the foundational layer for the LightRAG web user interfaces (both administrative and workspace). It includes the primary entry points for the React applications, global routing mechanisms, and the unified API client that mediates all communication with the LightRAG backend. This module ensures the frontend's structural integrity, navigation, and reliable data exchange with the server.
Key files: lightrag_webui/src/main.tsx, lightrag_webui/src/App.tsx, lightrag_webui/src/workspace-main.tsx
Source repository: https://github.com/HKUDS/LightRAG
Related Architecture Deep Dives
- GraphRAG Architecture Diagram — Python
- PageIndex Architecture Diagram — Python
- Langchain-Chatchat Architecture Diagram — Python
- Python-qrcode Architecture Diagram — Python
- Changedetection.io Architecture Diagram — Docker, Python
Explore the full interactive analysis of LightRAG on Revibe Codes — architecture diagrams, module flow, execution paths, and code-level insights.