Joel Stevick

Production RAG, line by line

Running it

Uploads and questions both depend on the model clients, and some of the Worker's code runs when no visitor is there. The clients behind the Models interface call Claude, Workers AI and Vectorize. Once a day the scheduled handler rolls up the day's questions, checks them for drift and runs the canary. The same daily job enforces the data lifecycle: it purges documents after 10 idle days and deletes what was kept with consent after 25 months. A file the visitor removes is deleted at once, in the removal request itself. The routes share a few small helpers; the site creates a visitor's demo account through the Worker's internal routes (POST /internal/demos), and the page loads its session from GET /api/session when it opens.

Demo Worker: Models interface: the one object the pipeline calls for Claude, embeddings and vectorsDEMO WORKERModels interfacethe one object thepipeline calls forClaude, embeddingsand vectorsDemo Worker: Model clients: call the Messages API, Workers AI and VectorizeDEMO WORKERModel clientscall the MessagesAPI, Workers AI andVectorizeDemo Worker: Scheduled handler: the cron trigger calls scheduled(), which runs dailyHealth, purge, expire and forgetHits in orderDEMO WORKERScheduledhandlerthe cron trigger callsscheduled(), whichruns dailyHealth,purge, expire andforgetHits in orderDemo Worker: Daily health: rolls up yesterday's questions per route and flags fallback rates and times that driftDEMO WORKERDaily healthrolls up yesterday'squestions per routeand flags fallbackrates and times thatdriftDemo Worker: The canary: asks 6 fixed questions about an invented 16-page document through the real askDEMO WORKERThe canaryasks 6 fixedquestions about aninvented 16-pagedocument throughthe real askDemo Worker: Idle purge and expiry: deletes the documents of demo accounts idle 10 days, then questions and archived pages older than 25 monthsDEMO WORKERIdle purge andexpirydeletes thedocuments of demoaccounts idle 10days, then questionsand archived pagesolder than 25 monthsRemoving a file: DELETE /api/files deletes the vectors, then the rows in one D1 batchRemoving a fileDELETE /api/filesdeletes the vectors,then the rows in oneD1 batchDemo Worker: Consent and the archive: X-Demo-Keep decides what is kept 25 months; /admin reads it and exports a golden setDEMO WORKERConsent and thearchiveX-Demo-Keepdecides what is kept25 months; /adminreads it and exports agolden setDemo Worker: Shared helpers: json(), sha256(), randomKey(), and signedIn(), which finds the demo account by its access key's hashDEMO WORKERShared helpersjson(), sha256(),randomKey(), andsignedIn(), whichfinds the demoaccount by its accesskey's hashDemo accounts and sessions: the site creates demo accounts and gives them new access keys; GET /api/session returns the page's stateDemo accountsand sessionsthe site creates demoaccounts and givesthem new accesskeys; GET/api/session returnsthe page's state
Demo Worker: the private Worker behind the DEMO service binding, demo/src/New to Cloudflare? How these fit together

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

Stages, in the order they run

  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

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