Joel Stevick

Code

Production RAG, line by line

The demo app at /demo answers questions from PDFs a visitor uploads, and before the browser shows an answer, code checks every sentence against the passages it cites. Its code is in a public repository. A visitor who verifies an email address gets a demo account: one row in the demos table (one per address) that owns their uploaded files, the chunks and vectors made from them (kept in a Vectorize namespace, the account's own partition of the vector index, named by the account's id) and their allowance of ten questions. The browser keeps the account's access key, a random string, in the demo cookie; the Worker stores only the key's SHA-256 hash and finds the account by hashing the key it is sent. In the code the account is called a demo (the Demo type, demo.id). The page's two requests, uploading a PDF and asking a question, go from the browser through the site's Pages Function to the demo Worker's router, which hands an upload (POST /api/files) to upload() and a question (POST /api/ask) to ask().

Browser: The demo page: the static demo.html, the markup and module script the visitor's browser runs (outside the flow)BROWSERThe demo pagethe static demo.html,the markup andmodule script thevisitor's browser runsPages Function: functions/demo.ts: serves GET /demo and sets the demo cookie to the access key in ?key= (outside the flow)PAGES FUNCTIONfunctions/demo.tsserves GET /demoand sets the democookie to the accesskey in ?key=Browser: Upload handler: pdf.js extracts each page's text; the page posts the text to /api/demo/filesBROWSERUpload handlerpdf.js extracts eachpage's text; the pageposts the text to/api/demo/filesBrowser: File list: the JSON reply comes back through the Pages Function; listFile() adds the file's rowBROWSERFile listthe JSON replycomes back throughthe Pages Function;listFile() adds thefile's rowPages Function: Pages Function: reads the visitor's access key from the demo cookie and sends it as the X-Demo-Key header over the DEMO service bindingPAGES FUNCTIONPages Functionreads the visitor'saccess key from thedemo cookie andsends it as theX-Demo-Key headerover the DEMOservice bindingDemo Worker: Worker router: route() looks up the visitor's demo account by the SHA-256 hash of that key, then calls upload()DEMO WORKERWorker routerroute() looks up thevisitor's demoaccount by theSHA-256 hash of thatkey, then callsupload()Demo Worker: INGEST 1–7: validates, claims the file name, checks the limits, chunks, stores, embeds and indexes the pagesDEMO WORKERINGEST 1–7validates, claims thefile name, checks thelimits, chunks, stores,embeds and indexesthe pagesBrowser: Ask form: POSTs to /api/demo/ask with Accept: text/event-streamBROWSERAsk formPOSTs to/api/demo/ask withAccept:text/event-streamBrowser: Stream reader: the events come back through the Pages Function unbuffered; events() parses them and the page renders each one as it arrivesBROWSERStream readerthe events comeback through thePages Functionunbuffered; events()parses them and thepage renders eachone as it arrivesPages Function: Pages Function: the same function sends the access key as X-Demo-Key and passes the event stream back unreadPAGES FUNCTIONPages Functionthe same functionsends the access keyas X-Demo-Key andpasses the eventstream back unreadDemo Worker: Worker router: route() looks up the visitor's demo account by the SHA-256 hash of that key, then calls ask()DEMO WORKERWorker routerroute() looks up thevisitor's demoaccount by theSHA-256 hash of thatkey, then calls ask()Demo Worker: ASK 1–8: validates, reserves one of ten questions, retrieves, reranks, answers and checks each sentenceDEMO WORKERASK 1–8validates, reservesone of ten questions,retrieves, reranks,answers and checkseach sentence
Browser: the visitor's page: demo.html and its module scriptPages Function: the site's server code on Cloudflare Pages, functions/Demo Worker: the private Worker behind the DEMO service binding, demo/src/New to Cloudflare? How these fit together

New to Cloudflare? Cloudflare in five minutes. The tables and vectors: The data model.

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

The six sections

Production RAG, line by line

855 of 855 lines explained (100%)

  1. The demo page Markup, styles and how the site serves it 161 of 161 lines explained (100%)
  2. Upload handler pagesOf() extracts the text with pdf.js; the change handler posts it 42 of 42 lines explained (100%)
  3. Ask form submit Post the question and open the answer's stream 26 of 26 lines explained (100%)
  4. Pages Function /api/demo/* Forwards to the Worker over the DEMO service binding and returns its response 131 of 131 lines explained (100%)
  5. Worker router index.ts, createApp's fetch, route() and ask()'s two renderings 296 of 296 lines explained (100%)
  6. File list and stream reader listFile() draws a file's row; events() reads the answer's stream 199 of 199 lines explained (100%)

Ingestion

200 of 200 lines explained (100%)

  1. INGEST 1 Validate the upload 27 of 27 lines explained (100%)
  2. INGEST 2 Claim the file name 14 of 14 lines explained (100%)
  3. INGEST 3 Check the limits 22 of 22 lines explained (100%)
  4. INGEST 4 Chunk each page 56 of 56 lines explained (100%)
  5. INGEST 5 Store the chunks 32 of 32 lines explained (100%)
  6. INGEST 6 Embed and index 17 of 17 lines explained (100%)
  7. INGEST 7 Notify and reply 32 of 32 lines explained (100%)

The ask

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

  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%)

Eval harness

623 of 623 lines explained (100%)

MCP server

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

Running it

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

  1. The Models interface What the pipeline calls 68 of 68 lines explained (100%)
  2. Model clients Claude, Workers AI and Vectorize 167 of 167 lines explained (100%)
  3. Daily health Daily roll-up and drift check 227 of 227 lines explained (100%)
  4. The canary Six fixed questions a day 144 of 144 lines explained (100%)
  5. The scheduled handler The daily cron at 04:17 UTC 11 of 11 lines explained (100%)
  6. Removing a file DELETE /api/files 72 of 72 lines explained (100%)
  7. Idle purge and expiry Documents after 10 idle days, kept rows after 25 months 67 of 67 lines explained (100%)
  8. Consent and the archive What is kept, and who reads it 149 of 149 lines explained (100%)
  9. Shared helpers Responses, key hashing and sign-in 42 of 42 lines explained (100%)
  10. Demo accounts and sessions Creating a demo account and loading its session 162 of 162 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