# fbr65/rag-router architecture

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

- Interactive diagram: https://gitdiagram.com/fbr65/rag-router
- Repository: https://github.com/fbr65/rag-router

## Overview

rag-router is a configurable Python RAG-routing library. Its principal workflow accepts a question, separates the skip decision from knowledge-base selection, retrieves from selected RAGs, then checks whether passages answer the question and reports a final state. It also supports document indexing, pluggable Laya/SLM/hybrid routes, and auto calibration. Relationships shown are limited to excerpted code and README statements; unsampled subsystems use exact enclosing directories where supported.

Main entry points: [`src/rag_router/pipeline.py`](https://github.com/fbr65/rag-router/blob/master/src/rag_router/pipeline.py) (RagRouter pipeline), [`src/rag_router/decision/route.py`](https://github.com/fbr65/rag-router/blob/master/src/rag_router/decision/route.py) (Laya / SLM / hybrid routes), [`src/rag_router/backends/lancedb.py`](https://github.com/fbr65/rag-router/blob/master/src/rag_router/backends/lancedb.py) (LanceDB retrieval and indexing), [`src/rag_router/router.py`](https://github.com/fbr65/rag-router/blob/master/src/rag_router/router.py) (Routing engine)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Public workflow"]
  node_pipeline["RagRouter pipeline<br/>[pipeline.py]"]
end

subgraph group_routing["Decision and calibration"]
  node_decision_engine["Routing engine<br/>[router.py]"]
  node_decision_routes["Laya / SLM / hybrid routes<br/>[route.py]"]
  node_legacy_decision["Legacy decision backend"]
  node_calibration["Auto calibration<br/>[calibration.py]"]
  node_calibration_cache["Calibration cache and locking<br/>[locking.py]"]
end

subgraph group_retrieval["Knowledge retrieval"]
  node_embedder["Embedding providers<br/>[embedding.py]"]
  node_backend_protocol["Backend contract and hits<br/>[base.py]"]
  node_lancedb[("LanceDB retrieval and indexing<br/>[lancedb.py]")]
end

subgraph group_checking["Answer verification"]
  node_answer_checker["Laya answer checker<br/>[laya.py]"]
  node_llm_checker["SLM answer checker<br/>[llm.py]"]
  node_answer_contract["Answer-check result contract<br/>[base.py]"]
end

subgraph group_config["Configuration"]
  node_config_loader["YAML configuration<br/>[config.py]"]
  node_rag_config["RAG definitions<br/>[config.py]"]
end

node_caller(("Library / CLI caller"))
node_laya_service["Laya model"]
node_slm_service["OpenAI-compatible model endpoint"]
node_lancedb_store[("LanceDB tables")]
node_json_documents["JSON document input"]

node_caller -->|"calls"| node_pipeline
node_pipeline -->|"loads settings"| node_config_loader
node_pipeline -->|"routes question"| node_decision_engine
node_pipeline -->|"constructs route"| node_decision_routes
node_pipeline -->|"derives thresholds"| node_calibration
node_pipeline -->|"locks calibration"| node_calibration_cache
node_decision_engine -->|"asks skip / choose"| node_decision_routes
node_pipeline -->|"searches selected RAGs"| node_lancedb
node_pipeline -->|"checks passages"| node_answer_checker
node_pipeline -->|"checks passages"| node_llm_checker
node_pipeline -->|"creates embedder"| node_embedder
node_pipeline -->|"builds backends"| node_rag_config
node_lancedb -->|"embeds text"| node_embedder
node_lancedb -->|"reads / writes"| node_lancedb_store
node_pipeline -->|"indexes documents"| node_lancedb
node_json_documents -->|"provides JSON"| node_pipeline
node_answer_checker -->|"returns result"| node_answer_contract
node_llm_checker -->|"returns result"| node_answer_contract
node_decision_routes -.->|"predicts"| node_laya_service
node_decision_routes -->|"requests logprobs"| node_slm_service
node_answer_checker -->|"predicts"| node_laya_service
node_llm_checker -->|"requests completion"| node_slm_service
node_decision_engine -.->|"supports legacy backend"| node_legacy_decision
node_calibration -->|"measures routes"| node_decision_routes

click node_pipeline "https://github.com/fbr65/rag-router/blob/master/src/rag_router/pipeline.py"
click node_config_loader "https://github.com/fbr65/rag-router/blob/master/src/rag_router/config.py"
click node_decision_engine "https://github.com/fbr65/rag-router/blob/master/src/rag_router/router.py"
click node_decision_routes "https://github.com/fbr65/rag-router/blob/master/src/rag_router/decision/route.py"
click node_legacy_decision "https://github.com/fbr65/rag-router/tree/master/src/rag_router/decision"
click node_calibration "https://github.com/fbr65/rag-router/blob/master/src/rag_router/calibration.py"
click node_calibration_cache "https://github.com/fbr65/rag-router/blob/master/src/rag_router/locking.py"
click node_rag_config "https://github.com/fbr65/rag-router/blob/master/src/rag_router/config.py"
click node_embedder "https://github.com/fbr65/rag-router/blob/master/src/rag_router/embedding.py"
click node_backend_protocol "https://github.com/fbr65/rag-router/blob/master/src/rag_router/backends/base.py"
click node_lancedb "https://github.com/fbr65/rag-router/blob/master/src/rag_router/backends/lancedb.py"
click node_answer_checker "https://github.com/fbr65/rag-router/blob/master/src/rag_router/checking/laya.py"
click node_llm_checker "https://github.com/fbr65/rag-router/blob/master/src/rag_router/checking/llm.py"
click node_answer_contract "https://github.com/fbr65/rag-router/blob/master/src/rag_router/checking/base.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_decision_engine,node_decision_routes,node_legacy_decision,node_calibration,node_calibration_cache,node_lancedb_store toneAmber
class node_embedder,node_backend_protocol,node_lancedb toneMint
class node_answer_checker,node_llm_checker,node_answer_contract toneRose
class node_config_loader,node_rag_config,node_caller,node_laya_service,node_slm_service,node_json_documents toneIndigo
```

## Components

### Public workflow

- [RagRouter pipeline](https://github.com/fbr65/rag-router/blob/master/src/rag_router/pipeline.py): `src/rag_router/pipeline.py`

### Decision and calibration

- [Routing engine](https://github.com/fbr65/rag-router/blob/master/src/rag_router/router.py): `src/rag_router/router.py`
- [Laya / SLM / hybrid routes](https://github.com/fbr65/rag-router/blob/master/src/rag_router/decision/route.py): `src/rag_router/decision/route.py`
- [Legacy decision backend](https://github.com/fbr65/rag-router/tree/master/src/rag_router/decision): `src/rag_router/decision`
- [Auto calibration](https://github.com/fbr65/rag-router/blob/master/src/rag_router/calibration.py): `src/rag_router/calibration.py`
- [Calibration cache and locking](https://github.com/fbr65/rag-router/blob/master/src/rag_router/locking.py): `src/rag_router/locking.py`

### Knowledge retrieval

- [Embedding providers](https://github.com/fbr65/rag-router/blob/master/src/rag_router/embedding.py): `src/rag_router/embedding.py`
- [Backend contract and hits](https://github.com/fbr65/rag-router/blob/master/src/rag_router/backends/base.py): `src/rag_router/backends/base.py`
- [LanceDB retrieval and indexing](https://github.com/fbr65/rag-router/blob/master/src/rag_router/backends/lancedb.py): `src/rag_router/backends/lancedb.py`

### Answer verification

- [Laya answer checker](https://github.com/fbr65/rag-router/blob/master/src/rag_router/checking/laya.py): `src/rag_router/checking/laya.py`
- [SLM answer checker](https://github.com/fbr65/rag-router/blob/master/src/rag_router/checking/llm.py): `src/rag_router/checking/llm.py`
- [Answer-check result contract](https://github.com/fbr65/rag-router/blob/master/src/rag_router/checking/base.py): `src/rag_router/checking/base.py`

### Configuration

- [YAML configuration](https://github.com/fbr65/rag-router/blob/master/src/rag_router/config.py): `src/rag_router/config.py`
- [RAG definitions](https://github.com/fbr65/rag-router/blob/master/src/rag_router/config.py): `src/rag_router/config.py`

### Other components

- **Library / CLI caller**
- **Laya model**
- **OpenAI-compatible model endpoint**
- **LanceDB tables**
- **JSON document input**

## Connections

- Library / CLI caller → RagRouter pipeline: calls (evidence: `src/rag_router/pipeline.py`)
- RagRouter pipeline → YAML configuration: loads settings (evidence: `src/rag_router/cli.py`)
- RagRouter pipeline → Routing engine: routes question (evidence: `src/rag_router/pipeline.py`)
- RagRouter pipeline → Laya / SLM / hybrid routes: constructs route (evidence: `src/rag_router/pipeline.py`)
- RagRouter pipeline → Auto calibration: derives thresholds (evidence: `src/rag_router/pipeline.py`)
- RagRouter pipeline → Calibration cache and locking: locks calibration (evidence: `src/rag_router/locking.py`)
- Routing engine → Laya / SLM / hybrid routes: asks skip / choose (evidence: `src/rag_router/router.py`)
- RagRouter pipeline → LanceDB retrieval and indexing: searches selected RAGs (evidence: `src/rag_router/pipeline.py`)
- RagRouter pipeline → Laya answer checker: checks passages (evidence: `src/rag_router/pipeline.py`)
- RagRouter pipeline → SLM answer checker: checks passages (evidence: `src/rag_router/pipeline.py`)
- RagRouter pipeline → Embedding providers: creates embedder (evidence: `src/rag_router/pipeline.py`)
- RagRouter pipeline → RAG definitions: builds backends (evidence: `src/rag_router/pipeline.py`)
- LanceDB retrieval and indexing → Embedding providers: embeds text (evidence: `src/rag_router/backends/lancedb.py`)
- LanceDB retrieval and indexing → LanceDB tables: reads / writes (evidence: `src/rag_router/backends/lancedb.py`)
- RagRouter pipeline → LanceDB retrieval and indexing: indexes documents (evidence: `src/rag_router/pipeline.py`)
- JSON document input → RagRouter pipeline: provides JSON (evidence: `src/rag_router/pipeline.py`)
- Laya answer checker → Answer-check result contract: returns result (evidence: `src/rag_router/checking/laya.py`)
- SLM answer checker → Answer-check result contract: returns result (evidence: `src/rag_router/checking/llm.py`)
- Laya / SLM / hybrid routes → Laya model: predicts (evidence: `src/rag_router/decision/route.py`)
- Laya / SLM / hybrid routes → OpenAI-compatible model endpoint: requests logprobs (evidence: `src/rag_router/decision/route.py`)
- Laya answer checker → Laya model: predicts (evidence: `src/rag_router/checking/laya.py`)
- SLM answer checker → OpenAI-compatible model endpoint: requests completion (evidence: `src/rag_router/checking/llm.py`)
- Routing engine → Legacy decision backend: supports legacy backend (evidence: `src/rag_router/router.py`)
- Auto calibration → Laya / SLM / hybrid routes: measures routes (evidence: `src/rag_router/pipeline.py`)
