How kaizer-1/llm-council works
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: services Services · router Router · evaluation Evaluation · inference Inference
Components 16
Frontend
Next.js presentation and interaction layer
- Web app · Debate, benchmarks, about, and how-it-works pages
frontend/app - Debate UI · Live session cards, transcript, judge, memory, sources, and routing panels
frontend/components/debate - Query tools · PDF upload and document status controls
frontend/components/query
Backend serving
FastAPI request handling and live execution
- API routes · Debate, baseline, conversations, health, and PDF routes
backend/app/api - Services · Council, baseline, memory, PDF, and context injection services
backend/app/services - Council · Researcher, Reasoner, Critic, Skeptic, and Judge orchestration
backend/app/orchestration - Agents · Prompted agent roles and parsing schemas
backend/app/agents - Memory · Conversation memory and embedding-backed snippet retrieval
backend/app/memory - PDF ingest · Upload extraction and page-aware chunking for citations
backend/app/documents
Backend research
Offline evaluation, routing, and orchestration
- Router · Feature extraction and XGBoost-based path selection
backend/app/router - Evaluation · Benchmarks, graders, scoring, and offline runs
backend/app/evaluation - Scripts · Training, grading, plotting, and reproduction scripts
backend/scripts
Data & external
Persistence and provider boundaries
- Inference · Model/provider abstraction for external LLM calls
backend/app/inference - Postgres · Users, conversations, debates, messages, evaluations, and runs
backend/app/db - Redis · Operational cache and coordination dependency
backend/app/cache.py - OpenRouter · External model access behind the inference layer
Connections 21
- 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