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().
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%)
- The demo page Markup, styles and how the site serves it 161 of 161 lines explained (100%)
- Upload handler pagesOf() extracts the text with pdf.js; the change handler posts it 42 of 42 lines explained (100%)
- Ask form submit Post the question and open the answer's stream 26 of 26 lines explained (100%)
- Pages Function /api/demo/* Forwards to the Worker over the DEMO service binding and returns its response 131 of 131 lines explained (100%)
- Worker router index.ts, createApp's fetch, route() and ask()'s two renderings 296 of 296 lines explained (100%)
- 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%)
- INGEST 1 Validate the upload 27 of 27 lines explained (100%)
- INGEST 2 Claim the file name 14 of 14 lines explained (100%)
- INGEST 3 Check the limits 22 of 22 lines explained (100%)
- INGEST 4 Chunk each page 56 of 56 lines explained (100%)
- INGEST 5 Store the chunks 32 of 32 lines explained (100%)
- INGEST 6 Embed and index 17 of 17 lines explained (100%)
- INGEST 7 Notify and reply 32 of 32 lines explained (100%)
The ask
1,032 of 1,032 lines explained (100%)
- ASK 1 Validate the question 6 of 6 lines explained (100%)
- ASK 2 Reserve a question 6 of 6 lines explained (100%)
- ASK 3–4 Retrieval 195 of 195 lines explained (100%)
- Whole documents and the empty pool Between ASK 4 and 5 23 of 23 lines explained (100%)
- ASK 5 Rerank and widen 170 of 170 lines explained (100%)
- ASK 6–7 Answer and check 606 of 606 lines explained (100%)
- ASK 8 Answer with sources 26 of 26 lines explained (100%)
Eval harness
623 of 623 lines explained (100%)
- The golden set
- Reading the cases cases.jsonl and the corpora 10 of 10 lines explained (100%)
- Eval runner
- Caps, arguments and the Worker Before any case 100 of 100 lines explained (100%)
- One demo account per corpus The PDFs uploaded 70 of 70 lines explained (100%)
- Asking each case Ask, trace, score, spend 54 of 54 lines explained (100%)
- Scoring a case Facts, sources, status 63 of 63 lines explained (100%)
- Writing the report results.json and results.md 52 of 52 lines explained (100%)
- The harness Worker
- Traced demo app and /eval routes evals/worker.ts 209 of 209 lines explained (100%)
- Stream usage reader
- Reading a streamed reply's usage evals/trace.ts 65 of 65 lines explained (100%)
- Reading a run
- Tuning journal
MCP server
1,533 of 1,533 lines explained (100%)
- Discovery
- The discovery documents /.well-known/ 79 of 79 lines explained (100%)
- Client registration
- Registering a client Metadata document or dynamic registration 6 of 6 lines explained (100%)
- Authorize and consent
- The consent page /oauth/authorize 178 of 178 lines explained (100%)
- Token
- Tokens and grants /oauth/token 12 of 12 lines explained (100%)
- Token exchange (RFC 8693) /api/docs-store 261 of 261 lines explained (100%)
- Resource server
- Token validation and limits Every MCP request 184 of 184 lines explained (100%)
- Tools
- TOOL 1 search_documents 82 of 82 lines explained (100%)
- TOOL 2 ask_documents 119 of 119 lines explained (100%)
- TOOL 3 remove_file 23 of 23 lines explained (100%)
- Resources
- Files and pages demo://files/… 135 of 135 lines explained (100%)
- Prompt
- PROMPT brief_me 27 of 27 lines explained (100%)
- Connected apps
- Listing and disconnecting apps GET and DELETE /api/grants 116 of 116 lines explained (100%)
- Site forwarding
- The Pages Functions /mcp/demo and /oauth/* 89 of 89 lines explained (100%)
- Your own host: agentic search
- Signing in: the SDK's OAuth client main.ts 121 of 121 lines explained (100%)
- The loop: Claude with the server's read-only tools loop.ts 101 of 101 lines explained (100%)
Running it
1,109 of 1,109 lines explained (100%)
- The Models interface What the pipeline calls 68 of 68 lines explained (100%)
- Model clients Claude, Workers AI and Vectorize 167 of 167 lines explained (100%)
- Daily health Daily roll-up and drift check 227 of 227 lines explained (100%)
- The canary Six fixed questions a day 144 of 144 lines explained (100%)
- The scheduled handler The daily cron at 04:17 UTC 11 of 11 lines explained (100%)
- Removing a file DELETE /api/files 72 of 72 lines explained (100%)
- Idle purge and expiry Documents after 10 idle days, kept rows after 25 months 67 of 67 lines explained (100%)
- Consent and the archive What is kept, and who reads it 149 of 149 lines explained (100%)
- Shared helpers Responses, key hashing and sign-in 42 of 42 lines explained (100%)
- 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