Skip to content

The answer graph ​

Every answer is one run of a LangGraph state graph, defined in app/agent/graph.py. This page walks through it node by node. The nodes live in app/agent/nodes.py and app/agent/code_task.py; the prompts in app/agent/prompts.py.

The shape ​

Figure 1 shows the nodes and the conditions between them.

flowchart TD
  START([question]) --> plan
  plan --> retrieve
  retrieve -->|web on| outline
  retrieve -->|web off| coverage
  outline --> web
  web --> coverage
  coverage -->|a query uncovered and web allowed and not yet searched| web
  coverage -->|nothing covered, no web results| finalize
  coverage --> pack
  pack -->|the plan needs code and the Space allows it| code
  pack -->|evidence| draft
  pack -->|no evidence| finalize
  code --> draft
  draft --> verify
  verify -->|Academic, equations, code on auto| math
  verify -->|problems and drafts left| revise
  verify --> finalize
  math -->|problems and drafts left| revise
  math --> finalize
  revise --> verify
  finalize --> END([answer])

Figure 1. The answer graph. With the web on, the search follows the core document: retrieve, then outline, then web.

Web on: following the core document ​

With Web on, the answer extends the course material instead of searching once for the whole question:

  1. retrieve finds the course passages as usual.
  2. outline (one short model call, no thinking) takes the core document, the one most of the best passages come from (a lesson, a chapter, a paper), and its sections in reading order with an excerpt each. The headings are numbered and the model picks from them: one section per heading, mapped back to the exact heading in code (by number or a 0.8 spelling match), kept in the material's order; a reply that merges two headings is split into one section each. Each section gets a web query in general wording that must name the section's subject (code adds it when missing and strips "Lesson 3", "Unit 4" and course codes), and the material's genre (lesson, book, paper, notes). Summary 2-3 sections, Comprehensive 3-5, Academic 3-4, Research 4-6. With no core document the plan's queries are used, as before.
  3. web searches each section with its own query (in parallel), reads that section's best page (two in Research mode), and scores each result against its section's query. A page is kept for a section only when its title or a passage names the section's subject (its words in order, at most two words apart): seen live, a query for "conceptual review" found pages on conceptual frameworks, which are now dropped.
  4. pack keeps the course passages and the best web passages of every section (up to two each, at most half the evidence), so one section's results can't crowd out the rest. Each web passage is labelled with its section.
  5. draft writes the body as those sections, in that order: what the course material says, then what the section's web passages add beyond it, in the material's style (a lesson's definition-explanation-example; a textbook's terms and notation).

answer_only drops a sentence repeated word for word (seen live: a small model copied the same course sentences into three sections).

Four nodes call the model with words to write: plan, draft, verify and revise. code and math call it to write Python. The rest are plain code. A Summary answer with no revision makes three model calls.

Nodes ​

plan ​

The model reads the conversation and the new message, and returns a JSON object (schema Plan):

  • standalone_query: the question rewritten to stand on its own, so "and the second one?" becomes searchable.
  • sub_questions: up to MAX_SUB_QUESTIONS extra search questions, for Comprehensive and Academic only.
  • topic: a short title for the mind map.
  • needs_web: whether the question needs current or outside information.
  • needs_code and code_goal: whether a computation would help, and what it should do.

The search queries are the standalone query plus the sub-questions.

retrieve ​

For each query, hybrid search over the Space's sources and scopes (see Retrieval and reranking), plus any file attached to the chat and the student's notes. Without Space sources, it searches the whole Library.

web ​

Runs when web search is allowed for the Space and the student pressed Web or Research, the Space's policy is Always, or the plan said needs_web. It also runs after coverage when a query found nothing relevant. See Web research and sign-in. In Research mode, a top source behind a login can pause the run here.

coverage ​

For each query: is there a passage the reranker scores at least COVERAGE_MIN_SCORE (0.3)? Without a reranker, any hit counts. It also emits the retrieval.candidates event that fills the Sources tab.

If nothing is covered and there are no web results, the graph goes straight to finalize, which declines. It never drafts from nothing.

pack ​

Chooses the evidence: course passages and web passages, merged and deduplicated, best first. It stops at the style's passage limit (8 for Summary, 16 otherwise), the token budget (EVIDENCE_TOKEN_BUDGET), and the relevance floor (EVIDENCE_MIN_SCORE). Every decision is logged in the evidence.packed event with its reason. Each passage gets an id, S1, S2 and so on, which is what the model cites.

code ​

When the plan asked for code and the Space's code policy allows it, the model writes a Python cell, the sandbox runs it in the chat's kernel, and the model fixes it after an error, up to 3 times. With the ask policy the run pauses first and shows the code. The final cell's code and output join the evidence as one passage titled "Python (sandbox)". See The code sandbox.

It also picks up to 3 of the sources' figures (app/agent/figures.py): one a passage refers to ("Fig. 9.3") comes first; one that only sits on an evidence page must share words with the question. They stay inside the Space's page scope.

draft ​

The model writes the answer from the evidence in the chosen style. The prompt holds, in order: the Space's instructions and the student's memory (marked "not evidence"), the style template, the citation rules, the evidence block, the recent conversation, the question, and a short format reminder. Small models follow the format better when it comes last. For a question about "the latest" something with web evidence, a note asks for the newest official source and an "as of" date.

Comprehensive and Academic drafts think. The tokens stream to the browser as they are written.

verify ​

The model lists the answer's claims (up to 12 for Summary, 25 otherwise), each with the passage ids it cites and a short quote from them. Then code checks every quote against the cited passage. A claim is verified only when its quote is really there. See Evidence and citations.

Code also finds sentences that cite nothing.

math ​

For an Academic answer with equations, when code may run without asking: the model writes a SymPy check of the answer's equations with a numeric example, and the sandbox runs it. Failed checks become problems for revise and cap the confidence at Medium.

revise ​

Runs when there are unsupported claims, uncited sentences (up to 15 per revision) or failed equation checks, and the answer has drafts left (MAX_GENERATION_ATTEMPTS, 2 by default: the first draft plus one revision). The model gets the list of problems and rewrites the answer: it adds a citation, corrects the statement, or replaces it with "(not established by the sources)". Then verify checks the new version.

finalize ​

Plain code. It:

  • turns [F1] figure markers into "(Fig. 9.3)" and puts the image after the paragraph or list,
  • turns the [S3] markers into numbered citations and builds the reference list from the index, never from model text,
  • computes the confidence label and its reasons,
  • counts the sentences that still cite nothing,
  • builds the mind map from the answer's headings and bullets,
  • or, with no evidence, writes the "not covered by the sources" reply.

State ​

The graph's state (app/agent/state.py) is one dictionary that every node reads and updates: the question and conversation, the Space's settings, the queries, hits, web results, evidence, the answer, claims, code cells, checks and the final fields. Nodes return only the keys they change.

LangGraph saves the state after every node, keyed by the run id, through the Postgres checkpointer. A run cut off by a restart resumes from its last finished node (see Runs, events and replay).

Pauses ​

Two nodes can pause a run with a LangGraph interrupt: web (a gated source in Research mode) and code (the ask policy). The run's status becomes waiting, the browser shows a card, and the student's choice resumes the graph from the same node. A run waits up to WAITING_RUN_HOURS.

Every step is visible ​

Each node is wrapped (_step in graph.py) so that it emits node.started and node.completed events with its time and a short summary, for example "8 passages kept" or "10 of 12 claims verified". The wrapper also checks for a cancel before each step. The Flow and Steps tabs in the UI are drawn from these events.