# kaizer-1/llm-council architecture

> Architecture diagram of the GitHub repository kaizer-1/llm-council, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kaizer-1/llm-council
- Repository: https://github.com/kaizer-1/llm-council

## Overview

1. **What this repo is**
   - This is a full-stack research system for comparing LLM debate against single-model routing.
   - The backend runs the core inference, debate orchestration, memory, document retrieval, and evaluation pipelines.
   - The frontend is a Next.js app for live debate sessions, benchmark visualizations, and project explanation pages.

2. **Top-level architecture boundary**
   - The backend is the system of record and execution engine.
   - The frontend is a presentation and interaction layer only; it does not host model logic.
   - External model calls are routed through OpenRouter via a LiteLLM-based inference layer, which keeps provider access behind one boundary.

3. **Request/response path for interactive usage**
   - A user query enters the FastAPI backend through `/api` routes and WebSocket endpoints.
   - The system either sends the query to the router, the council, or a direct “fast” model path.
   - Results stream back to the frontend live, including debate turns, convergence signals, cost, sources, and final judge output.

4. **Council debate subsystem**
   - The council is a multi-agent workflow with four specialist roles: Researcher, Reasoner, Critic, and Skeptic.
   - Debate runs in rounds, with orchestration handled as a state machine/graph rather than a simple sequential chain.
   - A Judge can synthesize the transcript into the final answer; there is also a majority-vote ablation path without Judge.
   - This subsystem is the main differentiator of the project and is separated from routing and evaluation.

5. **Router subsystem**
   - The router chooses per-query between debate and a single-model path.
   - It is implemented as a learned classifier/regressor stack using XGBoost, plus feature extraction and embeddings.
   - Router features are computed before inference, so routing is a pre-inference decision boundary.
   - The router service sits between the API layer and execution paths, making it a policy layer rather than an inference layer.

6. **Memory and retrieval subsystem**
   - Session memory stores prior conversation context and retrieves semantically relevant snippets for follow-up questions.
   - It uses pgvector-backed similarity search in Postgres.
   - This is a separate augmenting service, not part of the debate engine itself, and it can feed either routed or council-mode queries.

7. **Document/PDF ingestion subsystem**
   - PDF upload and extraction are handled as a distinct ingestion pipeline.
   - Documents are chunked with page tracking so answers can cite sources back to page-level evidence.
   - The frontend exposes upload/status UI, while the backend manages extraction, chunking, and retrieval.
   - This creates a second retrieval boundary alongside session memory: one for prior dialogue, one for attached documents.

8. **Evaluation and benchmarking subsystem**
   - The evaluation layer is a standalone harness for running benchmark suites such as TruthfulQA, GPQA, HumanEval, MT-Bench, and AlpacaEval.
   - Grading is benchmark-specific: exact match, code execution, or LLM-judge scoring.
   - Evaluation results are persisted and later used by the benchmark pages and router training/evaluation scripts.
   - This subsystem is operationally separate from live serving, even though it reuses the same inference and agent components.

9. **Persistence and data model**
   - Postgres is the main datastore for users, conversations, debates, messages, turns, responses, evaluations, and benchmark runs.
   - Alembic migrations define schema evolution, indicating the data model is an explicit part of the architecture.
   - Repositories under `backend/app/db/repositories` provide a boundary between business logic and SQLAlchemy persistence.
   - Redis is used as an operational dependency for caching/coordination rather than as the primary data store.

10. **Inference boundary**
   - All model access goes through `backend/app/inference`, which abstracts providers and models.
   - This keeps prompt logic, retries, provider quirks, and error handling out of the agents and services.
   - The design supports swapping model providers without changing the higher-level orchestration or API contracts.

11. **API surface and client interaction**
   - FastAPI exposes separate route groups for debate, baseline runs, conversations, health, PDF uploads, and benchmark-related endpoints.
   - WebSocket streaming is important for the debate experience because the frontend shows live turns and state transitions.
   - The frontend’s components are organized by feature area: debate session view, memory sidebar, router decision cards, benchmark charts, and PDF controls.

12. **Operational scripts and offline workflows**
   - `backend/scripts` contains repeatable offline workflows for evaluation runs, grading, cost estimation, router training, and plotting.
   - These scripts are how the system generates the benchmark tables and router artifacts described in the README.
   - They share code with the live backend, so the repo supports both serving and research/reproduction from one codebase.

13. **Frontend architecture**
   - The frontend is a Next.js App Router application with pages for the main landing experience, debate UI, benchmarks, about, and how-it-works.
   - Most visual complexity is in the debate page, which composes multiple cards for transcript, judge, convergence, sources, memory, and cost.
   - Static benchmark content is rendered from repo data and citations, while live debate views depend on backend API/WebSocket connectivity.

14. **Infrastructure and deployment shape**
   - Local development uses Docker Compose for Postgres 16 with pgvector and Redis 7.
   - The backend is packaged as a containerized Python 3.12 service; the frontend is a Node/TypeScript app.
   - The repo is therefore a two-tier application with separate runtime stacks and a shared domain model anchored in the backend.

15. **How to draw the architecture diagram**
   - Put the frontend on one side, the FastAPI backend in the center, and external services/datastores on the other side.
   - Inside the backend, show distinct boxes for API, router, council orchestration, memory, document retrieval, inference, evaluation, and persistence.
   - Emphasize two main data flows: interactive serving flow and offline evaluation/training flow.
   - The main architectural boundary is between policy/orchestration in the backend and provider-specific model execution outside the repo.

Main entry points: [`backend/app/services`](https://github.com/kaizer-1/llm-council/tree/main/backend/app/services) (Services), [`backend/app/router`](https://github.com/kaizer-1/llm-council/tree/main/backend/app/router) (Router), [`backend/app/evaluation`](https://github.com/kaizer-1/llm-council/tree/main/backend/app/evaluation) (Evaluation), [`backend/app/inference`](https://github.com/kaizer-1/llm-council/tree/main/backend/app/inference) (Inference)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_web_app["Web app<br/>Next.js UI"]
  node_debate_ui["Debate UI<br/>interactive views"]
  node_query_ui["Query tools<br/>upload/status controls"]
end

subgraph group_backend_live["Backend serving"]
  node_api_routes["API routes<br/>FastAPI endpoints"]
  node_services["Services"]
  node_council["Council<br/>multi-agent workflow"]
  node_agents["Agents<br/>specialist roles"]
  node_memory["Memory<br/>session retrieval"]
  node_pdfs["PDF ingest<br/>document pipeline"]
end

subgraph group_backend_research["Backend research"]
  node_router["Router<br/>policy classifier"]
  node_evaluation["Evaluation<br/>benchmark harness"]
  node_scripts["Scripts<br/>offline workflows"]
end

subgraph group_data_ext["Data & external"]
  node_inference["Inference<br/>provider boundary"]
  node_db[("Postgres<br/>primary datastore")]
  node_redis["Redis<br/>coordination cache<br/>[cache.py]"]
  node_openrouter(("OpenRouter<br/>LLM provider"))
end

node_web_app -->|"HTTP + WS"| node_api_routes
node_web_app -->|"renders"| node_debate_ui
node_web_app -->|"renders"| node_query_ui
node_api_routes -->|"delegates"| node_services
node_services -->|"route query"| node_router
node_services -->|"orchestrate"| node_council
node_services -->|"augment"| node_memory
node_services -->|"ingest"| node_pdfs
node_council -->|"invokes roles"| node_agents
node_council -->|"model calls"| node_inference
node_router -->|"features"| node_evaluation
node_router -->|"embeddings"| node_inference
node_evaluation -->|"benchmarks"| node_inference
node_evaluation -->|"store results"| node_db
node_scripts -->|"run offline"| node_evaluation
node_scripts -->|"train/eval"| node_router
node_memory -->|"similarity search"| node_db
node_pdfs -->|"persist chunks"| node_db
node_inference -->|"provider access"| node_openrouter
node_services -->|"persist"| node_db
node_services -->|"cache"| node_redis

click node_web_app "https://github.com/kaizer-1/llm-council/tree/main/frontend/app"
click node_debate_ui "https://github.com/kaizer-1/llm-council/tree/main/frontend/components/debate"
click node_query_ui "https://github.com/kaizer-1/llm-council/tree/main/frontend/components/query"
click node_api_routes "https://github.com/kaizer-1/llm-council/tree/main/backend/app/api"
click node_services "https://github.com/kaizer-1/llm-council/tree/main/backend/app/services"
click node_council "https://github.com/kaizer-1/llm-council/tree/main/backend/app/orchestration"
click node_agents "https://github.com/kaizer-1/llm-council/tree/main/backend/app/agents"
click node_memory "https://github.com/kaizer-1/llm-council/tree/main/backend/app/memory"
click node_pdfs "https://github.com/kaizer-1/llm-council/tree/main/backend/app/documents"
click node_router "https://github.com/kaizer-1/llm-council/tree/main/backend/app/router"
click node_evaluation "https://github.com/kaizer-1/llm-council/tree/main/backend/app/evaluation"
click node_scripts "https://github.com/kaizer-1/llm-council/tree/main/backend/scripts"
click node_inference "https://github.com/kaizer-1/llm-council/tree/main/backend/app/inference"
click node_db "https://github.com/kaizer-1/llm-council/tree/main/backend/app/db"
click node_redis "https://github.com/kaizer-1/llm-council/blob/main/backend/app/cache.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_web_app,node_debate_ui,node_query_ui toneBlue
class node_api_routes,node_services,node_council,node_agents,node_memory,node_pdfs toneAmber
class node_router,node_evaluation,node_scripts toneMint
class node_inference,node_db,node_redis,node_openrouter toneRose
```

## Components

### Frontend

Next.js presentation and interaction layer

- [Web app](https://github.com/kaizer-1/llm-council/tree/main/frontend/app): Next.js UI · Debate, benchmarks, about, and how-it-works pages · `frontend/app`
- [Debate UI](https://github.com/kaizer-1/llm-council/tree/main/frontend/components/debate): interactive views · Live session cards, transcript, judge, memory, sources, and routing panels · `frontend/components/debate`
- [Query tools](https://github.com/kaizer-1/llm-council/tree/main/frontend/components/query): upload/status controls · PDF upload and document status controls · `frontend/components/query`

### Backend serving

FastAPI request handling and live execution

- [API routes](https://github.com/kaizer-1/llm-council/tree/main/backend/app/api): FastAPI endpoints · Debate, baseline, conversations, health, and PDF routes · `backend/app/api`
- [Services](https://github.com/kaizer-1/llm-council/tree/main/backend/app/services): application services · Council, baseline, memory, PDF, and context injection services · `backend/app/services`
- [Council](https://github.com/kaizer-1/llm-council/tree/main/backend/app/orchestration): multi-agent workflow · Researcher, Reasoner, Critic, Skeptic, and Judge orchestration · `backend/app/orchestration`
- [Agents](https://github.com/kaizer-1/llm-council/tree/main/backend/app/agents): specialist roles · Prompted agent roles and parsing schemas · `backend/app/agents`
- [Memory](https://github.com/kaizer-1/llm-council/tree/main/backend/app/memory): session retrieval · Conversation memory and embedding-backed snippet retrieval · `backend/app/memory`
- [PDF ingest](https://github.com/kaizer-1/llm-council/tree/main/backend/app/documents): document pipeline · Upload extraction and page-aware chunking for citations · `backend/app/documents`

### Backend research

Offline evaluation, routing, and orchestration

- [Router](https://github.com/kaizer-1/llm-council/tree/main/backend/app/router): policy classifier · Feature extraction and XGBoost-based path selection · `backend/app/router`
- [Evaluation](https://github.com/kaizer-1/llm-council/tree/main/backend/app/evaluation): benchmark harness · Benchmarks, graders, scoring, and offline runs · `backend/app/evaluation`
- [Scripts](https://github.com/kaizer-1/llm-council/tree/main/backend/scripts): offline workflows · Training, grading, plotting, and reproduction scripts · `backend/scripts`

### Data & external

Persistence and provider boundaries

- [Inference](https://github.com/kaizer-1/llm-council/tree/main/backend/app/inference): provider boundary · Model/provider abstraction for external LLM calls · `backend/app/inference`
- [Postgres](https://github.com/kaizer-1/llm-council/tree/main/backend/app/db): primary datastore · Users, conversations, debates, messages, evaluations, and runs · `backend/app/db`
- [Redis](https://github.com/kaizer-1/llm-council/blob/main/backend/app/cache.py): coordination cache · Operational cache and coordination dependency · `backend/app/cache.py`
- **OpenRouter**: LLM provider · External model access behind the inference layer

## Connections

- Web app → API routes: HTTP + WS · Interactive debate and upload requests
- Web app → Debate UI: renders · Composes live debate session views
- Web app → Query tools: renders · Shows document upload and PDF status
- API routes → Services: delegates · API handlers call application services
- Services → Router: route query · Chooses council, router, or fast path
- Services → Council: orchestrate · Runs debate sessions and judge synthesis
- Services → Memory: augment · Adds session memory to follow-up queries
- Services → PDF ingest: ingest · Extracts and chunks uploaded documents
- Council → Agents: invokes roles · Uses specialist agents and parsers
- Council → Inference: model calls · Runs debate turns and judge synthesis
- Router → Evaluation: features · Uses router features and evaluation artifacts
- Router → Inference: embeddings · Derives representations for routing
- Evaluation → Inference: benchmarks · Executes benchmark queries through shared inference
- Evaluation → Postgres: store results · Persists benchmark and evaluation outputs
- Scripts → Evaluation: run offline · Training, grading, plotting, and batch runs
- Scripts → Router: train/eval · Builds router artifacts and reports
- Memory → Postgres: similarity search · Reads and stores session context in Postgres
- PDF ingest → Postgres: persist chunks · Stores document and chunk metadata
- Inference → OpenRouter: provider access · Routes all external model calls through provider boundary
- Services → Postgres: persist · Stores conversations, debates, messages, and turns
- Services → Redis: cache · Uses operational cache/coordination
