# kruddra/lawrag architecture

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

- Interactive diagram: https://gitdiagram.com/kruddra/lawrag
- Repository: https://github.com/kruddra/lawrag
- Video tour (about a minute): https://gitdiagram.com/kruddra/lawrag/video

## Overview

Legal Chat is a US/Canada statutes-and-regulations RAG application. Users choose a jurisdiction and law categories, submit a question, and receive a scoped answer with citations and confidence information. The main runtime path runs from the chat UI through the API, hybrid retrieval, context assembly, and response generation. A separate ingestion path obtains official-source documents, normalizes and chunks them, then indexes them; the CLI and API expose different ingestion routes. The excerpts show key calls, but not every adapter and UI component, so some documented relationships are supported by the README.

Main entry points: [`backend/api/routes.py`](https://github.com/kruddra/lawrag/blob/main/backend/api/routes.py) (API routes), [`backend/ingestion/pipeline.py`](https://github.com/kruddra/lawrag/blob/main/backend/ingestion/pipeline.py) (Ingestion pipeline), [`backend/ingestion/adapters`](https://github.com/kruddra/lawrag/tree/main/backend/ingestion/adapters) (Official-source adapters), [`backend/retrieval/hybrid_search.py`](https://github.com/kruddra/lawrag/blob/main/backend/retrieval/hybrid_search.py) (Hybrid search)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Chat interface"]
  node_chat_ui["Chat interface<br/>[ChatInterface.tsx]"]
  node_chat_state["Chat state<br/>[useChat.ts]"]
  node_api_client["API client<br/>[api.ts]"]
  node_message_view["Message display<br/>[MessageBubble.tsx]"]
  node_confidence["Confidence badge"]
end

subgraph group_ingest["Legal corpus ingestion"]
  node_scope_taxonomy["Jurisdiction and categories<br/>[jurisdictions.py]"]
  node_category_taxonomy["Category vocabulary<br/>[categories.py]"]
  node_source_registry["Source registry<br/>[source_registry.py]"]
  node_source_adapters["Official-source adapters"]
  node_ingest_pipeline["Ingestion pipeline<br/>[pipeline.py]"]
  node_document_processing["Parse and normalize"]
  node_chunking["Legal chunking<br/>[chunking.py]"]
  node_ingest_cache[("Content-hash cache<br/>[ingest_cache.py]")]
  node_ingest_cli["Ingestion CLI<br/>[ingest.py]"]
end

subgraph group_retrieve["Scoped retrieval"]
  node_vector_store[("Vector store<br/>[vector_store.py]")]
  node_query_enhancer["Query enhancement<br/>[query_enhancer.py]"]
  node_hybrid_search["Hybrid search<br/>[hybrid_search.py]"]
  node_context_builder["Context and cross-references<br/>[context.py]"]
end

subgraph group_answer["API and answers"]
  node_api_routes["API routes<br/>[routes.py]"]
  node_api_models["API and document models<br/>[models.py]"]
  node_prompting["Answer prompts<br/>[prompts.py]"]
  node_llm_client["LLM client<br/>[llm_client.py]"]
  node_response_safety["Response safety<br/>[safety.py]"]
  node_response_format["Citations and formatting<br/>[formatter.py]"]
end

node_user(("Legal questioner"))
node_openai{{"OpenAI service"}}
node_official_sources[("Government law sources")]

node_user -->|"asks question"| node_chat_ui
node_chat_ui -->|"sends message"| node_chat_state
node_chat_state -->|"calls API"| node_api_client
node_api_client -.->|"posts chat"| node_api_routes
node_api_routes -->|"enhances query"| node_query_enhancer
node_api_routes -->|"searches law"| node_hybrid_search
node_hybrid_search -->|"enhances query"| node_query_enhancer
node_hybrid_search -->|"retrieves chunks"| node_vector_store
node_api_routes -->|"builds context"| node_context_builder
node_context_builder -->|"loads crossrefs"| node_vector_store
node_api_routes -->|"uses prompts"| node_prompting
node_api_routes -->|"generates answer"| node_llm_client
node_llm_client -->|"requests completion"| node_openai
node_api_routes -->|"formats response"| node_response_format
node_api_routes -->|"checks safety"| node_response_safety
node_response_format -->|"uses citations"| node_context_builder
node_api_routes -->|"returns response"| node_api_models
node_chat_ui -->|"renders messages"| node_message_view
node_message_view -->|"shows confidence"| node_confidence
node_ingest_cli -->|"lists sources"| node_source_registry
node_ingest_cli -->|"selects adapter"| node_source_adapters
node_source_adapters -.->|"downloads law"| node_official_sources
node_ingest_cli -->|"runs ingestion"| node_ingest_pipeline
node_ingest_pipeline -->|"reads documents"| node_source_adapters
node_ingest_pipeline -->|"skips unchanged"| node_ingest_cache
node_ingest_pipeline -->|"chunks documents"| node_chunking
node_ingest_pipeline -->|"upserts chunks"| node_vector_store
node_document_processing -.->|"creates documents"| node_api_models
node_source_adapters -->|"uses jurisdiction"| node_scope_taxonomy
node_source_adapters -.->|"assigns categories"| node_category_taxonomy
node_chunking -->|"creates chunks"| node_api_models
node_api_routes -->|"processes uploads"| node_document_processing
node_api_routes -->|"chunks for reindex"| node_chunking
node_api_routes -->|"indexes chunks"| node_vector_store
node_hybrid_search -.->|"enforces jurisdiction"| node_scope_taxonomy

click node_chat_ui "https://github.com/kruddra/lawrag/blob/main/frontend/components/ChatInterface.tsx"
click node_chat_state "https://github.com/kruddra/lawrag/blob/main/frontend/hooks/useChat.ts"
click node_api_client "https://github.com/kruddra/lawrag/blob/main/frontend/lib/api.ts"
click node_message_view "https://github.com/kruddra/lawrag/blob/main/frontend/components/MessageBubble.tsx"
click node_confidence "https://github.com/kruddra/lawrag/blob/main/frontend/components/ConfidenceBadge.tsx"
click node_scope_taxonomy "https://github.com/kruddra/lawrag/blob/main/backend/jurisdictions.py"
click node_category_taxonomy "https://github.com/kruddra/lawrag/blob/main/backend/categories.py"
click node_source_registry "https://github.com/kruddra/lawrag/blob/main/backend/ingestion/source_registry.py"
click node_source_adapters "https://github.com/kruddra/lawrag/tree/main/backend/ingestion/adapters"
click node_ingest_pipeline "https://github.com/kruddra/lawrag/blob/main/backend/ingestion/pipeline.py"
click node_document_processing "https://github.com/kruddra/lawrag/tree/main/backend/ingestion"
click node_chunking "https://github.com/kruddra/lawrag/blob/main/backend/ingestion/chunking.py"
click node_ingest_cache "https://github.com/kruddra/lawrag/blob/main/backend/ingestion/ingest_cache.py"
click node_vector_store "https://github.com/kruddra/lawrag/blob/main/backend/retrieval/vector_store.py"
click node_query_enhancer "https://github.com/kruddra/lawrag/blob/main/backend/retrieval/query_enhancer.py"
click node_hybrid_search "https://github.com/kruddra/lawrag/blob/main/backend/retrieval/hybrid_search.py"
click node_context_builder "https://github.com/kruddra/lawrag/blob/main/backend/retrieval/context.py"
click node_api_routes "https://github.com/kruddra/lawrag/blob/main/backend/api/routes.py"
click node_api_models "https://github.com/kruddra/lawrag/blob/main/backend/api/models.py"
click node_prompting "https://github.com/kruddra/lawrag/blob/main/backend/generation/prompts.py"
click node_llm_client "https://github.com/kruddra/lawrag/blob/main/backend/generation/llm_client.py"
click node_response_safety "https://github.com/kruddra/lawrag/blob/main/backend/generation/safety.py"
click node_response_format "https://github.com/kruddra/lawrag/blob/main/backend/generation/formatter.py"
click node_ingest_cli "https://github.com/kruddra/lawrag/blob/main/scripts/ingest.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_chat_ui,node_chat_state,node_api_client,node_message_view,node_confidence toneBlue
class node_scope_taxonomy,node_category_taxonomy,node_source_registry,node_source_adapters,node_ingest_pipeline,node_document_processing,node_chunking,node_ingest_cache,node_ingest_cli,node_official_sources toneAmber
class node_vector_store,node_query_enhancer,node_hybrid_search,node_context_builder toneMint
class node_api_routes,node_api_models,node_prompting,node_llm_client,node_response_safety,node_response_format toneRose
class node_user,node_openai toneIndigo
```

## Components

### Chat interface

- [Chat interface](https://github.com/kruddra/lawrag/blob/main/frontend/components/ChatInterface.tsx): `frontend/components/ChatInterface.tsx`
- [Chat state](https://github.com/kruddra/lawrag/blob/main/frontend/hooks/useChat.ts): `frontend/hooks/useChat.ts`
- [API client](https://github.com/kruddra/lawrag/blob/main/frontend/lib/api.ts): `frontend/lib/api.ts`
- [Message display](https://github.com/kruddra/lawrag/blob/main/frontend/components/MessageBubble.tsx): `frontend/components/MessageBubble.tsx`
- [Confidence badge](https://github.com/kruddra/lawrag/blob/main/frontend/components/ConfidenceBadge.tsx): `frontend/components/ConfidenceBadge.tsx`

### Legal corpus ingestion

- [Jurisdiction and categories](https://github.com/kruddra/lawrag/blob/main/backend/jurisdictions.py): `backend/jurisdictions.py`
- [Category vocabulary](https://github.com/kruddra/lawrag/blob/main/backend/categories.py): `backend/categories.py`
- [Source registry](https://github.com/kruddra/lawrag/blob/main/backend/ingestion/source_registry.py): `backend/ingestion/source_registry.py`
- [Official-source adapters](https://github.com/kruddra/lawrag/tree/main/backend/ingestion/adapters): `backend/ingestion/adapters`
- [Ingestion pipeline](https://github.com/kruddra/lawrag/blob/main/backend/ingestion/pipeline.py): `backend/ingestion/pipeline.py`
- [Parse and normalize](https://github.com/kruddra/lawrag/tree/main/backend/ingestion): `backend/ingestion`
- [Legal chunking](https://github.com/kruddra/lawrag/blob/main/backend/ingestion/chunking.py): `backend/ingestion/chunking.py`
- [Content-hash cache](https://github.com/kruddra/lawrag/blob/main/backend/ingestion/ingest_cache.py): `backend/ingestion/ingest_cache.py`
- [Ingestion CLI](https://github.com/kruddra/lawrag/blob/main/scripts/ingest.py): `scripts/ingest.py`

### Scoped retrieval

- [Vector store](https://github.com/kruddra/lawrag/blob/main/backend/retrieval/vector_store.py): `backend/retrieval/vector_store.py`
- [Query enhancement](https://github.com/kruddra/lawrag/blob/main/backend/retrieval/query_enhancer.py): `backend/retrieval/query_enhancer.py`
- [Hybrid search](https://github.com/kruddra/lawrag/blob/main/backend/retrieval/hybrid_search.py): `backend/retrieval/hybrid_search.py`
- [Context and cross-references](https://github.com/kruddra/lawrag/blob/main/backend/retrieval/context.py): `backend/retrieval/context.py`

### API and answers

- [API routes](https://github.com/kruddra/lawrag/blob/main/backend/api/routes.py): `backend/api/routes.py`
- [API and document models](https://github.com/kruddra/lawrag/blob/main/backend/api/models.py): `backend/api/models.py`
- [Answer prompts](https://github.com/kruddra/lawrag/blob/main/backend/generation/prompts.py): `backend/generation/prompts.py`
- [LLM client](https://github.com/kruddra/lawrag/blob/main/backend/generation/llm_client.py): `backend/generation/llm_client.py`
- [Response safety](https://github.com/kruddra/lawrag/blob/main/backend/generation/safety.py): `backend/generation/safety.py`
- [Citations and formatting](https://github.com/kruddra/lawrag/blob/main/backend/generation/formatter.py): `backend/generation/formatter.py`

### Other components

- **Legal questioner**
- **OpenAI service**
- **Government law sources**

## Connections

- Legal questioner → Chat interface: asks question (evidence: `README.md`)
- Chat interface → Chat state: sends message (evidence: `frontend/components/ChatInterface.tsx`)
- Chat state → API client: calls API (evidence: `frontend/hooks/useChat.ts`)
- API client → API routes: posts chat
- API routes → Query enhancement: enhances query (evidence: `backend/api/routes.py`)
- API routes → Hybrid search: searches law (evidence: `backend/api/routes.py`)
- Hybrid search → Query enhancement: enhances query (evidence: `backend/retrieval/hybrid_search.py`)
- Hybrid search → Vector store: retrieves chunks (evidence: `backend/retrieval/hybrid_search.py`)
- API routes → Context and cross-references: builds context (evidence: `backend/api/routes.py`)
- Context and cross-references → Vector store: loads crossrefs (evidence: `backend/retrieval/context.py`)
- API routes → Answer prompts: uses prompts (evidence: `backend/api/routes.py`)
- API routes → LLM client: generates answer (evidence: `backend/api/routes.py`)
- LLM client → OpenAI service: requests completion (evidence: `backend/generation/llm_client.py`)
- API routes → Citations and formatting: formats response (evidence: `backend/api/routes.py`)
- API routes → Response safety: checks safety (evidence: `backend/api/routes.py`)
- Citations and formatting → Context and cross-references: uses citations (evidence: `backend/generation/formatter.py`)
- API routes → API and document models: returns response (evidence: `backend/api/routes.py`)
- Chat interface → Message display: renders messages (evidence: `frontend/components/ChatInterface.tsx`)
- Message display → Confidence badge: shows confidence (evidence: `frontend/components/MessageBubble.tsx`)
- Ingestion CLI → Source registry: lists sources (evidence: `scripts/ingest.py`)
- Ingestion CLI → Official-source adapters: selects adapter (evidence: `scripts/ingest.py`)
- Official-source adapters → Government law sources: downloads law (evidence: `README.md`)
- Ingestion CLI → Ingestion pipeline: runs ingestion (evidence: `scripts/ingest.py`)
- Ingestion pipeline → Official-source adapters: reads documents (evidence: `backend/ingestion/pipeline.py`)
- Ingestion pipeline → Content-hash cache: skips unchanged (evidence: `backend/ingestion/pipeline.py`)
- Ingestion pipeline → Legal chunking: chunks documents (evidence: `backend/ingestion/pipeline.py`)
- Ingestion pipeline → Vector store: upserts chunks (evidence: `backend/ingestion/pipeline.py`)
- Parse and normalize → API and document models: creates documents (evidence: `backend/ingestion/pipeline.py`)
- Official-source adapters → Jurisdiction and categories: uses jurisdiction (evidence: `backend/ingestion/adapters/base.py`)
- Official-source adapters → Category vocabulary: assigns categories (evidence: `backend/ingestion/adapters/justice_laws.py`)
- Legal chunking → API and document models: creates chunks (evidence: `backend/ingestion/chunking.py`)
- API routes → Parse and normalize: processes uploads (evidence: `backend/api/routes.py`)
- API routes → Legal chunking: chunks for reindex (evidence: `backend/api/routes.py`)
- API routes → Vector store: indexes chunks (evidence: `backend/api/routes.py`)
- Hybrid search → Jurisdiction and categories: enforces jurisdiction
