# hippoley/causalrag architecture

> Architecture diagram of the GitHub repository hippoley/causalrag, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hippoley/causalrag
- Repository: https://github.com/hippoley/causalrag

## Overview

1. **Overall shape**
   - This is a Python package that extends standard RAG with a causal-reasoning layer.
   - The central unit is `CausalRAGPipeline`, which connects ingestion, indexing, retrieval, reranking, prompt construction, and LLM generation.
   - Around that core are thin entry surfaces: a CLI, a FastAPI service, examples, and evaluation scripts.

2. **Primary data flow**
   - Input documents are first indexed.
   - Indexing builds two parallel artifacts: a semantic vector index and a causal graph.
   - At query time, the system retrieves candidate passages from both semantic and causal signals, reranks them, then feeds the selected context plus causal paths into the generator.
   - The final response includes the answer, supporting context, and causal paths.

3. **Causal graph subsystem**
   - Located under `causalrag/causal_graph/`.
   - `builder.py` is the extraction and graph-construction layer: it turns text into causal triples and stores them as a graph structure.
   - `retriever.py` uses that graph to find causal paths relevant to a query.
   - `explainer.py` is a utility layer for interpreting or visualizing the graph rather than serving the main retrieval path.

4. **Semantic and hybrid retrieval**
   - Located under `causalrag/retriever/`.
   - `vector_store.py` provides the semantic similarity index, with FAISS called out in the README as the main backend.
   - `bm25_retriever.py` adds keyword-style retrieval.
   - `hybrid.py` combines semantic retrieval with causal signals, making retrieval multi-signal instead of purely embedding-based.
   - This boundary matters because retrieval is not a single index lookup; it merges at least semantic and causal evidence.

5. **Reranking layer**
   - Located under `causalrag/reranker/`.
   - `base.py` defines the reranker interface.
   - `causal_path.py` implements reranking based on whether candidates align with causal paths or causal graph structure.
   - In the architecture, reranking sits between candidate retrieval and generation, acting as a relevance/coherence filter.

6. **Generation subsystem**
   - Located under `causalrag/generator/`.
   - `prompt_builder.py` assembles prompts using templates stored in `causalrag/templates/`.
   - `llm_interface.py` abstracts the model provider layer; the README indicates OpenAI-compatible APIs are the default integration point, with other providers possible.
   - This layer consumes reranked context and causal evidence and produces the answer.

7. **Orchestration and public API**
   - `causalrag/pipeline.py` is the orchestration boundary for end-to-end use.
   - It likely owns the main objects: graph builder, retriever, reranker, prompt builder, and LLM interface.
   - `causalrag/cli.py` exposes that pipeline for indexing, querying, serving, and evaluation from the command line.
   - `causalrag/interface/api.py` provides the server-facing FastAPI wrapper for programmatic access.

8. **Evaluation subsystem**
   - Located under `causalrag/evaluation/`.
   - `evaluator.py` runs metrics over pipeline outputs and references.
   - The bundled dataset in `data/evaluation/evaluation_dataset.json` shows that evaluation is first-class, not an afterthought.
   - The README emphasizes both generic RAG metrics and causal metrics, so evaluation covers answer quality plus causal consistency/completeness.

9. **Utilities and support code**
   - `causalrag/utils/io.py` handles file and dataset loading.
   - `causalrag/utils/logging.py` supports tracing/timing/diagnostics across pipeline stages.
   - These are cross-cutting helpers used by indexing, querying, CLI execution, and evaluation.

10. **Repository surfaces and operational modes**
   - The project can run as a library, a CLI tool, or an API service.
   - Examples in `examples/` show basic usage, causal extraction, and evaluation flows.
   - The `scripts/run_evaluation.py` script and `data/` directory suggest repeatable offline evaluation workflows.

11. **Tests and architectural confidence**
   - `tests/test_graph.py`, `tests/test_reranker.py`, and `tests/test_pipeline.py` map cleanly to the main subsystems.
   - That test split mirrors the architecture: graph construction, reranking logic, and end-to-end orchestration.
   - For a diagram, those test targets are good indicators of the package’s main boundaries.

12. **Key architectural idea to diagram**
   - The distinctive feature is the two-track knowledge representation: semantic retrieval via vectors and causal reasoning via a graph.
   - The pipeline does not replace RAG; it augments it by inserting causal extraction, causal path retrieval, and causal reranking before generation.
   - The most important arrows in the diagram are: documents to vector index and causal graph; query to hybrid retrieval; candidates to reranker; reranked context and causal paths to LLM prompt construction; output answer back to API/CLI caller.

Main entry points: [`causalrag/pipeline.py`](https://github.com/hippoley/causalrag/blob/main/causalrag/pipeline.py) (CausalRAG), [`causalrag/retriever/hybrid.py`](https://github.com/hippoley/causalrag/blob/main/causalrag/retriever/hybrid.py) (Hybrid retriever), [`causalrag/generator/prompt_builder.py`](https://github.com/hippoley/causalrag/blob/main/causalrag/generator/prompt_builder.py) (Prompt builder), [`causalrag/causal_graph/builder.py`](https://github.com/hippoley/causalrag/blob/main/causalrag/causal_graph/builder.py) (Graph builder)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_orchestration["Orchestration"]
  node_pipeline["CausalRAG<br/>pipeline orchestrator<br/>[pipeline.py]"]
end

subgraph group_retrieval["Retrieval"]
  node_vector_store[("Vector index<br/>semantic retriever<br/>[vector_store.py]")]
  node_bm25[("BM25<br/>keyword retriever<br/>[bm25_retriever.py]")]
  node_hybrid["Hybrid retriever<br/>multi-signal retrieval<br/>[hybrid.py]"]
  node_reranker_base["Reranker API<br/>reranker interface<br/>[base.py]"]
  node_causal_reranker["Causal rerank<br/>[causal_path.py]"]
end

subgraph group_causal["Causal graph"]
  node_causal_builder["Graph builder<br/>causal extraction<br/>[builder.py]"]
  node_causal_retriever["Path retriever<br/>causal retrieval<br/>[retriever.py]"]
  node_explainer["Explainer<br/>graph utility<br/>[explainer.py]"]
end

subgraph group_generation["Generation"]
  node_prompt_builder["Prompt builder<br/>prompt assembly<br/>[prompt_builder.py]"]
  node_llm_interface["LLM interface<br/>model adapter<br/>[llm_interface.py]"]
  node_templates["Templates<br/>[causal_prompt.txt]"]
end

subgraph group_ops["Surfaces"]
  node_cli["CLI<br/>command surface<br/>[cli.py]"]
  node_api["API<br/>[api.py]"]
end

subgraph group_support["Support"]
  node_evaluator["Evaluator<br/>metrics runner<br/>[evaluator.py]"]
  node_io_utils["I/O utils<br/>data loading<br/>[io.py]"]
  node_logging_utils["Logging<br/>diagnostics<br/>[logging.py]"]
end

node_cli -->|"runs"| node_pipeline
node_api -->|"serves"| node_pipeline
node_pipeline -->|"indexes"| node_causal_builder
node_pipeline -->|"indexes"| node_vector_store
node_pipeline -->|"queries"| node_hybrid
node_hybrid -->|"uses"| node_vector_store
node_hybrid -->|"uses"| node_bm25
node_hybrid -->|"uses"| node_causal_retriever
node_causal_builder -->|"feeds"| node_causal_retriever
node_hybrid -->|"passes"| node_causal_reranker
node_causal_reranker -->|"implements"| node_reranker_base
node_pipeline -->|"assembles"| node_prompt_builder
node_prompt_builder -->|"uses"| node_templates
node_prompt_builder -->|"calls"| node_llm_interface
node_llm_interface -->|"returns"| node_pipeline
node_pipeline -->|"evaluates"| node_evaluator
node_evaluator -->|"loads"| node_io_utils
node_pipeline -->|"logs"| node_logging_utils
node_explainer -.->|"inspects"| node_causal_builder

click node_pipeline "https://github.com/hippoley/causalrag/blob/main/causalrag/pipeline.py"
click node_cli "https://github.com/hippoley/causalrag/blob/main/causalrag/cli.py"
click node_api "https://github.com/hippoley/causalrag/blob/main/causalrag/interface/api.py"
click node_vector_store "https://github.com/hippoley/causalrag/blob/main/causalrag/retriever/vector_store.py"
click node_bm25 "https://github.com/hippoley/causalrag/blob/main/causalrag/retriever/bm25_retriever.py"
click node_hybrid "https://github.com/hippoley/causalrag/blob/main/causalrag/retriever/hybrid.py"
click node_causal_builder "https://github.com/hippoley/causalrag/blob/main/causalrag/causal_graph/builder.py"
click node_causal_retriever "https://github.com/hippoley/causalrag/blob/main/causalrag/causal_graph/retriever.py"
click node_explainer "https://github.com/hippoley/causalrag/blob/main/causalrag/causal_graph/explainer.py"
click node_reranker_base "https://github.com/hippoley/causalrag/blob/main/causalrag/reranker/base.py"
click node_causal_reranker "https://github.com/hippoley/causalrag/blob/main/causalrag/reranker/causal_path.py"
click node_prompt_builder "https://github.com/hippoley/causalrag/blob/main/causalrag/generator/prompt_builder.py"
click node_llm_interface "https://github.com/hippoley/causalrag/blob/main/causalrag/generator/llm_interface.py"
click node_templates "https://github.com/hippoley/causalrag/blob/main/causalrag/templates/causal_prompt.txt"
click node_evaluator "https://github.com/hippoley/causalrag/blob/main/causalrag/evaluation/evaluator.py"
click node_io_utils "https://github.com/hippoley/causalrag/blob/main/causalrag/utils/io.py"
click node_logging_utils "https://github.com/hippoley/causalrag/blob/main/causalrag/utils/logging.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_pipeline toneBlue
class node_vector_store,node_bm25,node_hybrid,node_reranker_base,node_causal_reranker toneAmber
class node_causal_builder,node_causal_retriever,node_explainer toneMint
class node_prompt_builder,node_llm_interface,node_templates toneRose
class node_cli,node_api toneIndigo
class node_evaluator,node_io_utils,node_logging_utils toneTeal
```

## Components

### Orchestration

End-to-end pipeline and public entrypoints

- [CausalRAG](https://github.com/hippoley/causalrag/blob/main/causalrag/pipeline.py): pipeline orchestrator · Coordinates indexing, retrieval, reranking, and generation · `causalrag/pipeline.py`

### Retrieval

Semantic, keyword, and hybrid candidate retrieval

- [Vector index](https://github.com/hippoley/causalrag/blob/main/causalrag/retriever/vector_store.py): semantic retriever · FAISS-backed semantic similarity store · `causalrag/retriever/vector_store.py`
- [BM25](https://github.com/hippoley/causalrag/blob/main/causalrag/retriever/bm25_retriever.py): keyword retriever · Lexical retrieval for keyword-style matching · `causalrag/retriever/bm25_retriever.py`
- [Hybrid retriever](https://github.com/hippoley/causalrag/blob/main/causalrag/retriever/hybrid.py): multi-signal retrieval · Combines semantic and causal evidence for candidate selection · `causalrag/retriever/hybrid.py`
- [Reranker API](https://github.com/hippoley/causalrag/blob/main/causalrag/reranker/base.py): reranker interface · Defines the reranking contract · `causalrag/reranker/base.py`
- [Causal rerank](https://github.com/hippoley/causalrag/blob/main/causalrag/reranker/causal_path.py): causal reranker · Ranks candidates by alignment with causal paths and graph structure · `causalrag/reranker/causal_path.py`

### Causal graph

Causal extraction, path retrieval, and explanation

- [Graph builder](https://github.com/hippoley/causalrag/blob/main/causalrag/causal_graph/builder.py): causal extraction · Turns text into causal triples and stores the causal graph · `causalrag/causal_graph/builder.py`
- [Path retriever](https://github.com/hippoley/causalrag/blob/main/causalrag/causal_graph/retriever.py): causal retrieval · Finds causal paths relevant to a query · `causalrag/causal_graph/retriever.py`
- [Explainer](https://github.com/hippoley/causalrag/blob/main/causalrag/causal_graph/explainer.py): graph utility · Interprets or visualizes causal graph structure · `causalrag/causal_graph/explainer.py`

### Generation

Prompt assembly and LLM access

- [Prompt builder](https://github.com/hippoley/causalrag/blob/main/causalrag/generator/prompt_builder.py): prompt assembly · Builds prompts from templates and retrieved evidence · `causalrag/generator/prompt_builder.py`
- [LLM interface](https://github.com/hippoley/causalrag/blob/main/causalrag/generator/llm_interface.py): model adapter · Abstracts provider access, typically OpenAI-compatible · `causalrag/generator/llm_interface.py`
- [Templates](https://github.com/hippoley/causalrag/blob/main/causalrag/templates/causal_prompt.txt): prompt templates · Prompt text used by generation · `causalrag/templates/causal_prompt.txt`

### Surfaces

CLI and API exposure

- [CLI](https://github.com/hippoley/causalrag/blob/main/causalrag/cli.py): command surface · Command-line entrypoint for indexing, querying, serving, and evaluation · `causalrag/cli.py`
- [API](https://github.com/hippoley/causalrag/blob/main/causalrag/interface/api.py): FastAPI surface · Programmatic server wrapper around the pipeline · `causalrag/interface/api.py`

### Support

Evaluation and shared utilities

- [Evaluator](https://github.com/hippoley/causalrag/blob/main/causalrag/evaluation/evaluator.py): metrics runner · Evaluates answer quality and causal consistency · `causalrag/evaluation/evaluator.py`
- [I/O utils](https://github.com/hippoley/causalrag/blob/main/causalrag/utils/io.py): data loading · Loads files and datasets · `causalrag/utils/io.py`
- [Logging](https://github.com/hippoley/causalrag/blob/main/causalrag/utils/logging.py): diagnostics · Shared tracing and timing helpers · `causalrag/utils/logging.py`

## Connections

- CLI → CausalRAG: runs · CLI invokes the end-to-end pipeline
- API → CausalRAG: serves · API wraps the same orchestration boundary
- CausalRAG → Graph builder: indexes · Builds causal graph artifacts from documents
- CausalRAG → Vector index: indexes · Builds semantic index for retrieval
- CausalRAG → Hybrid retriever: queries · Uses hybrid candidate retrieval at query time
- Hybrid retriever → Vector index: uses · Pulls semantic candidates
- Hybrid retriever → BM25: uses · Pulls lexical candidates
- Hybrid retriever → Path retriever: uses · Adds causal path evidence
- Graph builder → Path retriever: feeds · Constructed graph supports causal path lookup
- Hybrid retriever → Causal rerank: passes · Candidate set is filtered by causal relevance
- Causal rerank → Reranker API: implements · Concrete reranker follows the interface
- CausalRAG → Prompt builder: assembles · Builds the final prompt from reranked evidence
- Prompt builder → Templates: uses · Prompt text comes from template files
- Prompt builder → LLM interface: calls · Submits prompt to the model adapter
- LLM interface → CausalRAG: returns · LLM output is folded into the pipeline response
- CausalRAG → Evaluator: evaluates · Outputs can be scored offline
- Evaluator → I/O utils: loads · Reads datasets and references
- CausalRAG → Logging: logs · Cross-cutting tracing across stages
- Explainer → Graph builder: inspects · Explainer operates on the causal graph representation
