Joel Stevick

Production RAG, line by line

The ask

ask() validates the question and reserves one of the ten questions of the visitor's demo account (their row in the demos table, which owns their files, chunks and question allowance), then starts the answering() generator, which retrieves up to 48 candidate chunks by BM25 and vector search, merged by reciprocal rank fusion; the later stages work from this pool. Claude Haiku reranks the candidates, and widening adds each chosen chunk's neighbours. Claude Sonnet writes the answer, code checks each sentence against the passages it cites before releasing it, and the answer ends with its sources.

ASK 1: Validate the question: a trimmed string of 1 to 500 charactersASK 1: Validatethe questiona trimmed string of 1to 500 charactersASK 2: Reserve a question: one conditional UPDATE; 10 questions a demo accountASK 2: Reserve aquestionone conditionalUPDATE; 10questions a demoaccountASK 3–4: Retrieval: BM25 and vector search, fused into up to 48 candidatesASK 3–4:RetrievalBM25 and vectorsearch, fused into upto 48 candidatesWhole documents and the empty pool: an account of 12 chunks or fewer is read wholeWhole documentsand the emptypoolan account of 12chunks or fewer isread wholeASK 5: Rerank and widen: Haiku orders the pool and 12 are kept; widen() adds their neighboursASK 5: Rerankand widenHaiku orders the pooland 12 are kept;widen() adds theirneighboursASK 6–7: Answer and check: Sonnet writes; code checks each sentence or list item before it is releasedASK 6–7:Answer andcheckSonnet writes; codechecks eachsentence or list itembefore it is releasedASK 8: Answer with sources: sources in order of first citation, with the stored passage textASK 8: Answerwith sourcessources in order offirst citation, with thestored passage textA question that fails validation (400) or a used-up limit (429) ends the ask with a JSON error. An empty pool, or an answer whose first sentence or list item fails its check, ends it with the fallback line.A question that fails validation (400) or a used-up limit (429) ends the ask with a JSON error. An empty pool, or ananswer whose first sentence or list item fails its check, ends it with the fallback line.
Runs in: Demo Worker, the private Worker behind the DEMO service binding, demo/src/. New to Cloudflare? How these fit together

1,032 of 1,032 lines explained (100%)

Stages, in the order they run

  1. ASK 1 Validate the question 6 of 6 lines explained (100%)
  2. ASK 2 Reserve a question 6 of 6 lines explained (100%)
  3. ASK 3–4 Retrieval 195 of 195 lines explained (100%)
  4. Whole documents and the empty pool Between ASK 4 and 5 23 of 23 lines explained (100%)
  5. ASK 5 Rerank and widen 170 of 170 lines explained (100%)
  6. ASK 6–7 Answer and check 606 of 606 lines explained (100%)
  7. ASK 8 Answer with sources 26 of 26 lines explained (100%)
In the browser
runs in the visitor's browser, on the demo page
Ingestion
runs when a PDF's text is uploaded
Query time
runs when the Worker answers a question or a search_documents call from the MCP server, and in the request handling every route shares, in the Worker and in the site's functions, which uploads and removals pass through too
MCP request
runs on a request to the MCP server or its OAuth endpoints
Site request
runs on a site request that is not a question, an upload, an MCP call or the cron: a page load, a session read, the demo page's /api/grants calls, an admin read, a file's removal
Scheduled
runs in the Worker's daily cron job
Eval run
runs when the eval harness runs on a developer's machine
MCP host
runs in an MCP host outside the Worker: the search loop example, on a developer's machine (npm run search) or in its test
Module load
runs once, when the module is loaded
Type only
erased by the TypeScript compiler; never runs
Never runs
a comment, a licence or markup with no behaviour of its own

Pinned to joelstevick-demo-rag-pipeline at 6a9c6c7.