The system at a glance
This page shows the parts of Scholar Lens and how a question becomes a cited answer. It is for developers who want to change the code. Each part has its own page, linked below.
The parts
Figure 1 shows the processes and what they talk to. Everything runs on one machine; nothing leaves it except web searches and page fetches.
flowchart LR
subgraph Browser
UI[React app]
EXT[Browser extension]
end
subgraph Server
API[FastAPI API<br/>answer graph, run pool]
W[Job worker<br/>indexing, imports, summaries]
PG[(Postgres + pgvector)]
end
subgraph Models
LMS[LM Studio<br/>chat + embedding]
RR[llama.cpp<br/>reranker]
end
subgraph Optional
SB[Code sandbox<br/>gVisor]
WEB[Tavily / SearXNG<br/>OpenAlex / arXiv]
BR[Playwright MCP<br/>browser]
SI[Sign-in browser]
end
UI -- HTTP + SSE --> API
EXT -- token --> API
API --> PG
W --> PG
API --> LMS
W --> LMS
API --> RR
API --> SB
API --> WEB
API --> BR
API --> SI
Figure 1. Processes and their connections. The worker can also run inside the API process.
| Part | Technology | Page |
|---|---|---|
| Web app | React 19, TypeScript, Vite, Tailwind, TanStack Query, Zustand | Frontend |
| API | FastAPI, SQLAlchemy, Alembic | Backend and API |
| Answer graph | LangGraph, with a Postgres checkpointer | The answer graph |
| Ingestion | pymupdf4llm, RapidOCR, python-docx, trafilatura | Ingestion |
| Retrieval | pgvector, Postgres full text, a cross-encoder reranker | Retrieval and reranking |
| Evidence | Code, not the model: citations, quote checks, confidence | Evidence and citations |
| Web research | Tavily, SearXNG, OpenAlex, arXiv, Playwright over MCP | Web research and sign-in |
| Code sandbox | Jupyter kernels in a gVisor container | The code sandbox |
| Runs | A bounded pool, durable runs, an event log for live view and replay | Runs, events and replay |
| Storage | One Postgres database for everything | Data model |
| Access | Cookies with short-lived JWTs, roles, Space sharing | Security and access |
| Extension | Manifest V3, TypeScript | Browser extension |
From a question to an answer
- The browser posts the message to
POST /api/threads/{id}/messages. The API stores it and creates an execution (a run). - The run pool starts the run, or queues it when two are already running.
- The run executes the answer graph: plan, retrieve (and search the web when needed), check coverage, pack the evidence, draft, verify the claims, revise if needed, and finalise. The graph saves its state after every step, so a run cut off by a restart resumes.
- Every step writes run events to the database: step started and finished, passages found, tokens of the draft as they stream. The browser follows them over server-sent events (SSE).
- The final answer, its citations, confidence and mind map are saved as an assistant message.
The model writes only the answer text and short structured replies. Page numbers, titles, links and the confidence label come from code and the index. This is what makes the citations trustworthy.
From an upload to the index
POST /api/library/uploadsaves the file and queues anindex_documentjob.- The worker claims the job, parses the file in a child process, builds the section tree, chunks the text per section and embeds the chunks.
- It replaces the document's chunks in one transaction, so a failed re-index never leaves a document empty.
- It queues section-summary jobs at background priority.
Design rules
These run through the whole code base.
- Local first. The models run on the user's GPU. Web search is opt-in per Space.
- Facts from code, words from the model. Citations, page numbers and confidence are computed, never generated.
- Abstain rather than guess. With no relevant evidence the graph declines instead of drafting.
- Everything durable is in Postgres. Jobs, runs, checkpoints, events and vectors share one database, so one backup holds everything.
- Private by default. A user's Library, chats and notes are theirs. Sharing is explicit.