---
title: Formal Modular RAG Architecture
url: https://www.emergentmind.com/topics/formal-modular-rag-architecture
type: topic
---

# Formal Modular RAG Architecture

A formal modular Retrieval-Augmented Generation (RAG) architecture refers to a systematic, composable, and interface-driven decomposition of a RAG system, such that its core functionalities—retrieval, context aggregation, and generative reasoning—are realized as interoperable modules with well-defined input/output types and documented interface specifications, enabling rigorous benchmarking, ablation, extensibility, and tailored instantiation for diverse application scenarios and knowledge sources. In the context of Graph-based Retrieval-Augmented Generation (GraphRAG), a formal modular architecture encompasses fine-grained module boundaries, precise pipeline composition, and parameterizable components, supporting both analysis and design-space exploration in large-scale reasoning tasks [2411.05844].

## 1. Formal Problem Statement and Pipeline Structure

A modular GraphRAG framework assumes as input a text-attributed knowledge graph $G = (V, E)$, where $V$ is a set of entities with textual descriptions and $E \subseteq V \times R \times V$ a set of labeled, directed edges for relations $R$. Given a natural-language query $q \in \Sigma^*$, a preprocessing frontend extracts a set of query entities/relations $\varepsilon_q = \{(v_i^{(q)}, e_j^{(q)})\}$ present in $G$.

The pipeline supports the following formal sequence:
1. **Entity/Relation Extraction**: $\varepsilon_q = \mathrm{Extract}(q) \subset V \times R$
2. **Reasoning Chain Retrieval**: $R = \{P_i\} = \mathrm{Retrieve}(G, \varepsilon_q)$, where each $P$ is a multi-hop path.
3. **Augmented Prompt Construction**: $q' = q \cup \mathrm{Format}(R)$
4. **Answer Generation**: $a = \mathrm{LLM}(q')$

Compactly, $a = \mathrm{Generate}(\mathrm{AugPrompt}(q, \mathrm{Retrieve}(G, \mathrm{Extract}(q))))$.

## 2. Modular Decomposition of Retrieval

The retrieval phase is decomposed into three sequential modules, each with strictly defined interface contracts:

### 2.1 Subgraph-Extraction (SE)
- **Inputs**: Full graph $G$, query entity set $\{v_i^{(q)}\} \subset V$, parameters $\mathrm{max\_ent} \in \mathbb{N}$, $\mathrm{coupling\_flag} \in \{\mathrm{true}, \mathrm{false}\}$.
- **Outputs**: Query-specific subgraph $g_q = (V_q, E_q)$, $|V_q| \leq \mathrm{max\_ent}$.
- **Algorithm**: Personalized PageRank (PPR) from seed nodes, with possible semantic reranking via $S(v; \varepsilon_q)$ if coupled with a neural/LLM scorer.

**Interface**:
```python
def SE_PPR(G, seeds, λ, max_ent): ...
```

### 2.2 Path-Filtering (PF)
- **Inputs**: Subgraph $g_q$, seeds $\varepsilon_q$, method $\in$ {SPF, CPF, IPF}, $beam\_width$, scoring function $S_{\mathrm{path}}(P)$.
- **Outputs**: Candidate paths $R = \{P_i\}$.
- **Algorithms**: Shortest-Path (Dijkstra, SPF), Complete Path Filtering (CPF, BFS enumeration), Iterative/Beam Search (IPF).

**Interface**:
```python
def IPF(g_q, seeds, beam_width, S_path): ...
```

### 2.3 Path-Refinement (PR)
- **Inputs**: Candidates $R = \{P_i\}$, query $q$, scoring function $S_{\mathrm{ref}}(P_i, q)$, $top_k$.
- **Outputs**: Refined paths $\hat{R}$ (top-$k$).
- **Algorithm**: Score and select top-$k$ candidates.

**Interface**:
```python
score_list = [(P_i, S_ref(P_i, q)) for P_i in R]
hat_R = top_k(P_i) by score
return hat_R
```

### Module Interfaces Summary

| Module | Input Types                    | Output Types                   | Core Algorithm            |
|--------|-------------------------------|--------------------------------|---------------------------|
| SE     | $G, \varepsilon_q, ...$       | $g_q = (V_q, E_q)$             | PPR, semantic rerank      |
| PF     | $g_q, \varepsilon_q, ...$     | $R = \{P_i\}$                  | SPF, CPF, IPF (beam)      |
| PR     | $R, q, ...$                   | $\hat{R}$ (top $k$ paths)      | Scoring + selection       |

## 3. Systematic Taxonomy of Existing Techniques

Existing GraphRAG techniques can be mapped as valid module choices:

- **SE**: Purely structural (PPR, RWR), lexical (BM25), neural (Sentence-Transformer, DPR), LLM rerank (Llama/GPT), fine-tuned KG-coupled models.
- **PF**: Standard SPF/CPF (as in classical KBQA), beam-search + BM25/NN-based scorer, LLM-based scoring, fine-tuned in-domain LLMs.
- **PR**: Random, BM25 of path text, Sentence-Transformer rerankers, LLM re-ranking, LoRA-fine-tuned discriminators.

This mapping reveals a design space where each module is independently instantiable, provided interface consistency.

## 4. Assembly and Instantiation of New GraphRAG Pipelines

A concrete GraphRAG instance is specified by selecting one method per module, with budgetary/compatibility constraints:
1. **SE Module**: {$\mathrm{PPR}$, $\mathrm{RWR}$, $\mathrm{PPR+BM25}$, $\mathrm{PPR+ST}$, $\mathrm{PPR+LLM_{ft}}$}
2. **PF Module**: {$\mathrm{SPF}$, $\mathrm{CPF}$, $\mathrm{BS+BM25}$, $\mathrm{BS+ST}$, $\mathrm{BS+LLM}$}
3. **PR Module**: {$\mathrm{Random}$, $\mathrm{BM25}$, $\mathrm{ST}$, $\mathrm{LLM}$}

Parameters must be tuned to keep subgraph and candidate set sizes within hardware constraints.

Guidelines:
- Avoid double fine-tuning across SE and PF to prevent overspecialization.
- Non-NN pipelines (structural SE + basic PF/PR) are computationally frugal; LLM-powered pipelines offer accuracy at higher cost.

## 5. Evaluation Metrics and Multi-Objective Tradeoffs

Key evaluation metrics for modular GraphRAG architectures:
- **Reasoning Quality** $Q(I)$: F1 or exact-match (HR@1) on generation output.
- **Retrieval Quality** $Q_R(I)$: F1 versus ground-truth reasoning chains.
- **End-to-End Quality** $Q_{E2E}(I) = \alpha Q_R + (1-\alpha) Q_{\mathrm{Gen}}$.
- **Runtime** $T(I)$: $T_{SE} + T_{PF} + T_{PR} + T_{gen}$ (seconds).
- **Token Cost** $C_{\mathrm{tok}}(I)$: aggregate LLM tokens processed.
- **GPU Cost** $C_{\mathrm{GPU}}(I)$: LLM latency × GPU power.

Comprehensive optimization:
$$
\max F(I) = Q_{E2E}(I) - \lambda_1 T(I) - \lambda_2 C_{\mathrm{tok}}(I) - \lambda_3 C_{\mathrm{GPU}}(I) \\
\text{subject to } T(I) \leq T_{max},\; C_{\mathrm{tok}}(I) \leq C_{max}.
$$

## 6. Empirical Design Principles for Modular GraphRAG

Empirical analysis in LEGO-GraphRAG provides the following guidance:
- **SE**: PPR maximizes recall; adding Sentence-Transformer reranking improves precision at low cost. Vanilla LLM reranking is more effective but incurs ~5× runtime overhead.
- **PF**: SPF and CPF are efficient; CPF provides richer context but is noisier. Beam search with ST reranking is optimal for F1/runtime; fine-tuning offers marginal gain. LLM beam search only helps with large, well-prompted models.
- **PR**: BM25 is fast but low quality; ST rerankers add 3–5 F1 points; LLM re-ranking is best but doubles runtime.
- **Prompt Engineering**: Increasing path count up to ~16 boosts F1, after which returns diminish. Few-shot prompts show inconsistent effects; zero-shot is robust.
- **Overall Pipelines**: PPR→SPF→ST for throughput; PPR+LLM_ft→BS+ST→LLM for accuracy [2411.05844].

The modular decomposition, taxonomy, and instantiation protocol in LEGO-GraphRAG enable systematic design, reproducibility, and principled experimentation in building advanced RAG systems grounded in structured knowledge graphs.

Source: https://www.emergentmind.com/topics/formal-modular-rag-architecture