Production RAG, line by line
Cloudflare in five minutes
The demo is three programs: demo.html in the visitor's browser, served by Cloudflare Pages; the site's Pages Functions; and a private Worker that the functions reach through the DEMO service binding. The Worker gets its D1 database, Vectorize index and Workers AI as bindings declared in wrangler.toml, and its model clients through createApp(modelsFor), so the deployed Worker, the tests and the evals each pass in their own.
Where each piece runs
Cloudflare Pages serves demo.html to the visitor's browser as a static file, and also runs the files under functions/ as Pages Functions: server code that runs on each request to its path, such as /api/demo/ask. The demo Worker is a separate program with no public address. A Worker is a script that Cloudflare runs on request: it exports a fetch(request, env, ctx) handler (and here a scheduled() handler too, for the daily job) and returns a Response.
The Pages Function calls the Worker through a service binding, which the function's code reaches as env.DEMO, an object of Cloudflare's Fetcher type. Calling env.DEMO.fetch(…) passes a Fetch API Request and returns a Response, but Cloudflare hands the request straight to the Worker's fetch() handler, by default on the same thread of the same server, with no added latency or cost, so the Worker needs no public URL. The Worker in turn calls D1, Vectorize, Workers AI, its KV namespace and the mailer through bindings of its own, and calls Anthropic's API with an ordinary fetch() and a secret key.
Injected resources: declared, delivered, used
A Worker never opens a connection string or holds a database password. demo/wrangler.toml declares each binding: a name for the code and the resource it stands for. When Cloudflare runs the Worker, it passes every binding in as a property of env, the second argument of fetch(). Each binding is an object whose methods call its resource: env.DB.prepare('SELECT …') runs SQL on the bound D1 database. The code describes env with the Env interface in demo/src/app.ts. wrangler.toml also binds OAUTH_KV (a KV namespace), MAILER (a service binding to another Worker, the mailer) and a [triggers] cron that runs scheduled() once a day.
A secret also arrives on env, but it is set with wrangler secret put (locally, in a git-ignored .dev.vars file), never written in wrangler.toml; the Worker has two, ANTHROPIC_API_KEY and DOCS_STORE_CLIENT_SECRET (the second is typed in demo/src/token-exchange.ts, not in Env). The site's own Pages configuration, which is not in this repository, declares the DEMO service binding the same way. wrangler deploy uploads the Worker with its configuration; the resources themselves are created once (wrangler d1 create, wrangler vectorize create), and the file refers to each by name or id.
The same code runs against other copies. evals/wrangler.toml binds the same names to a local D1 database and its own Vectorize index, joelstevick-com-demo-eval, so the eval harness never touches the demo's data, and the test suite gives the Worker a local D1 and KV of its own (demo/vitest.config.ts).
How the Worker is handed its models
The pipeline never calls Claude, Workers AI or Vectorize directly. It calls a Models object (demo/src/models.ts) with four members: claude (one JSON reply), stream (the answer, streamed), embed (texts to vectors) and vectors (upsert, query, delete in the index). createApp(modelsFor) in demo/src/app.ts takes a function that builds that object from env, and calls it on every request, because env exists only inside a handler. This is dependency injection: whoever creates the app decides which models it gets.
Three places create it. demo/src/index.ts, the deployed Worker, passes the real clients built from the bindings and the secret. demo/tests/demo.test.ts passes scriptedModels() from tests/support/scripted.ts, which answer by markers in the question ([model-down], [vectors-down]…), so the suite never calls a model and can make each one fail on purpose. evals/worker.ts passes the real clients again, and wraps fetch to record every Claude call's tokens for the eval report.
The keyword search, BM25, is not a library or a Cloudflare service: it is bm25() in demo/src/retrieval.ts, about sixty lines of TypeScript. fuseKeywordAndVector() runs it over the demo account's chunks, which retrieval() read from D1, and merges its ranking with Vectorize's by reciprocal rank fusion.
Pinned to joelstevick-demo-rag-pipeline at 6a9c6c7.