The stored chunks are embedded with bge-m3, 50 texts per call, and each batch's vectors are upserted into Vectorize in the demo account's namespace (its own partition of the vector index, named by the account's id, demo.id), with the chunk's id as the vector's id.
demo/src/app.ts17 / 17 lines on this page explainedlines 56-57,370-384 of 1124; the rest are on other stage pages
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
How many chunk texts go to the embedding model in one call.
/** How many chunk texts go to the embedding model in one call. */constEMBED_BATCH = 50
EMBED_BATCH = 50 Module load
WhatEach Workers AI embedding call receives at most 50 chunk texts.
WhyBatching makes one model call per 50 chunks and bounds the size of each request; at CHUNK_CHARS (900) characters a chunk, one call carries at most 45,000 characters. The code gives no reason for 50 over another size.
// INGEST 6 · embed the chunks (Workers AI) and index them in Vectorize// Without vectors the demo answers from BM25 alone; a failed embedding leaves this file searched by its words only.if (models.embed !== undefined && models.vectors !== undefined) {
Only with both bindings Ingestion
WhatThe stage marker. The stage runs only when models.embed and models.vectors are both defined; embedFor and vectorsFor (models.ts) define them only when the AI and VECTORS bindings exist.
WhyWithout vectors, retrieval (ASK 3–4) falls back to BM25, which reads the chunks from D1. So a Worker deployed without Workers AI or Vectorize still ingests documents and answers questions.
try {// EMBED_BATCH texts an embedding call; each vector is written under the demo's namespace with its chunk's id, so a hit maps straight back to its row.for (let i = 0; i < chunks.length; i += EMBED_BATCH) {const batch = chunks.slice(i, i + EMBED_BATCH)
Fifty at a time Ingestion
WhatInside a try, the loop walks the chunk list in slices of EMBED_BATCH and awaits each slice in turn.
WhySequential batches keep one embedding call in flight at a time. Batches that finish before a failure stay indexed; the failure stops the loop, and the remaining chunks get no vectors.
Whatmodels.embed sends the slice's texts to @cf/baai/bge-m3 on Workers AI in one call and returns one vector per text, in the same order.
WhyAt query time the same model embeds the question, so questions and chunks are points in the same vector space. The index is created with the cosine metric (README.md, Run it), so cosine similarity decides which chunks are nearest.
model: @cf/baai/bge-m31,024 dimensions (bge-m3's output; the index is created at that size, README.md)
Explained in Ep 2: Embeddings: a transformer encoder, bge-m3, 1,024 numbers a chunk From RAG deep dive, Ep 2: RAG Embeddings Explained: BM25, Vector Search and Cosine Similarity
Whatmodels.vectors.upsert writes each vector under the namespace demo.id, with the chunk's id as its id; values[j] is the vector for the j-th text in the batch.
WhyThe namespace confines each account's queries to its own vectors. The shared id lets retrieval match each vector hit to the chunk it has already read from D1 (byId in fuseKeywordAndVector), and a hit with no matching chunk is dropped. Upsert replaces any vector already stored under the same id. Vectorize applies writes asynchronously, so for a while after an upload a chunk can be found by BM25 before vector search returns it; evals/run.mjs waits up to 3 minutes for every vector to read back.
The same idea, shown with HNSW, one kind of ANN index: Insertion: a random top layer, neighbour selection and links From RAG deep dive, Ep 3: HNSW Algorithm Explained: Hierarchical Navigable Small World Graph for ANN Search
} catch (error) {console.error('demo upload: embedding failed; this file is searched by its words alone', error) } }
Fall back, don't fail Ingestion
WhatAny error from the embedding model or Vectorize is logged as "demo upload: embedding failed; this file is searched by its words alone", and the upload continues to INGEST 7.
WhyThe chunks committed at INGEST 5 are the source of truth. A chunk without a vector is reachable only by its BM25 rank, so a passage that matches the question by meaning but shares few of its words can miss the candidate pool. The reply at INGEST 7 is the same 200 either way, and only the Worker's log records the failure. Nothing records which chunks lack vectors and nothing retries, so those chunks stay keyword-only until the file is removed and uploaded again.