# RAG on Cloudflare: Vectorize, D1, R2 and AI Search

> How to build retrieval-augmented generation on Cloudflare with R2, Queues, Vectorize, D1 and AI Search, the limits to plan around, and when to use pgvector.

- Published: 2026-11-02; last updated 2026-11-14
- Author: the WeStack AI team
- Subject: Cloud and DevOps
- Address: https://westack.ai/blog/rag-on-cloudflare

> Cloudflare has every part of a retrieval stack: R2 for documents, Queues and Workers for ingestion, Workers AI for embeddings and reranking, Vectorize for vectors, D1 for metadata and permissions, and AI Search as the managed version of the whole pipeline. Start with AI Search, and build the pieces yourself only when you need control over chunking, permissions or ranking. Choose Postgres with pgvector when your data and access rules already live in Postgres.

## The stack at a glance

Retrieval-augmented generation has the same stages on every platform: store documents, turn them into searchable chunks, find the right chunks for a question, and hand them to a model. If the stages are new to you, start with our [guide to RAG](https://westack.ai/blog/retrieval-augmented-generation-guide). This is how they map on to Cloudflare, with the limit that matters most at each one.

| Stage | Cloudflare piece | Limit to know |
| --- | --- | --- |
| Store source documents | R2 | None that bites early |
| Trigger ingestion | R2 event notifications into Queues | 128 KB per message |
| Parse and chunk | A Worker, plus Workers AI Markdown conversion | 128 MB of memory per isolate |
| Embed | Workers AI embedding models | Per-account rate limits |
| Vector index | Vectorize | 20 million vectors per index, 1,536 dimensions |
| Text, metadata, permissions | D1 | 10 GB per database, one query at a time |
| Rerank | Workers AI reranker | Per-account rate limits |
| Generate | Workers AI, or any provider through AI Gateway | Depends on the model |
| All of the above, managed | AI Search | File count and file size per instance |

There are two ways to use this. Let AI Search run the pipeline, or assemble it yourself. We would try them in that order.

## Should you start with AI Search?

For most document and knowledge-base search, yes.

[AI Search](https://developers.cloudflare.com/ai-search/concepts/how-ai-search-works/) is the service that launched as AutoRAG. The legacy Worker binding still carries that name. You give it a data source: files uploaded to its built-in storage, an R2 bucket, a website to crawl or, in beta, rows from a D1 query. It converts each file to Markdown, chunks it, embeds the chunks and builds a vector index.

It builds a BM25 keyword index beside it. Hybrid search is on by default, merged with reciprocal rank fusion. Reranking is available and off by default. Query rewriting, metadata filters, a similarity cache and answer generation are optional, and every instance exposes an MCP endpoint so an agent can use it as a tool.

That list is most of what teams otherwise spend their first weeks building by hand. What you give up is control, and the [limits page](https://developers.cloudflare.com/ai-search/platform/limits-pricing/) shows where:

- An instance holds up to a million files on the paid plan, or 500,000 with hybrid search.
- Files are capped at 4 MiB for most formats, and 10 MiB for plain text and for PDFs with OCR turned on.
- You get five custom metadata fields per instance for filtering.

Pricing is by ingestion tokens, storage and queries, with the managed embedding and reranking calls included and generation billed separately. Usage-based billing is new, so read the current rates and start date on that page instead of trusting a blog post.

The reasons to move past it are specific. You need document-level permissions that five metadata fields cannot express. Your documents need special chunking: contracts by clause, tables kept whole, code by function. Your files are too large. You want ranking signals of your own, such as recency or source authority. If none of those applies, stay.

For multi-tenant products, Cloudflare's guidance is one instance per tenant when you need strong isolation, and a shared instance with a tenant filter when you have many small tenants.

## Building the pipeline yourself

### Ingestion: R2, a queue and a Worker

Uploads land in R2. An [event notification](https://developers.cloudflare.com/r2/buckets/event-notifications/) puts a message on a queue when an object is created or deleted, and a consumer Worker does the work: fetch the object, convert it to Markdown, chunk it, embed the chunks in batches, upsert the vectors and write the chunk rows to D1.

Four details decide whether this pipeline is reliable.

**Make it idempotent.** Queues retry, so the same document will be processed twice sooner or later. Give vectors deterministic IDs such as `doc42:v3:017` and use upserts.

**Handle deletes on day one.** When a document is deleted or replaced, look up its vector IDs in D1 and remove them from Vectorize. Skipping this is how a withdrawn policy keeps appearing in answers.

**Expect a delay.** Vectorize acknowledges a write and applies it to the index asynchronously, so a new document is not searchable the instant the upsert returns. Mark a document as indexed in D1 only after you have confirmed it.

**Respect the memory ceiling.** A Worker has 128 MB. A long scanned PDF may not fit once it is loaded and parsed. Split large files before conversion, or send them to a container, and keep the queue message to a reference, not the document.

### Vectorize: the vector index

An index is created with a number of dimensions and a distance metric, and neither can be changed afterwards. The [limits](https://developers.cloudflare.com/vectorize/platform/limits/) are plain: 1,536 dimensions at most, 20 million vectors per index, 10 KiB of metadata per vector. Check your embedding model first. The BGE models on Workers AI produce 384 to 1,024 dimensions, inside the limit, but some popular hosted models default to more than 1,536 and have to be asked for shorter vectors. How to pick is covered in [choosing an embedding model](https://westack.ai/blog/choosing-an-embedding-model).

Filtering comes in two forms. A namespace is a hard partition, one per vector, with up to 50,000 per index on the paid plan. It is the natural home for a tenant ID. [Metadata filters](https://developers.cloudflare.com/vectorize/reference/metadata-filtering/) are more flexible, with equality, set membership and range operators, and they are applied before the nearest-neighbour search, so a selective filter does not starve the results. The constraints: ten metadata indexes per index, strings indexed on their first 64 bytes, and only vectors written after a metadata index exists are covered by it. Create the metadata indexes before you load anything.

A query returns at most 100 matches, or 50 if you ask for values or metadata back. So ask for IDs and fetch the text from D1.

Vectorize has no keyword search. If people will search for part numbers, clause references or surnames, you need a keyword index as well. D1 supports SQLite's FTS5 module, which is enough for a first version. The merge logic is in [hybrid search and reranking](https://westack.ai/blog/hybrid-search-and-reranking).

Billing is by vector dimensions stored and queried, with no hourly charge for keeping an index available.

### D1: text, metadata and permissions

D1 is where the truth lives: one table for documents (owner, version, status), one for chunks (text, position, vector ID), and one for who may see what. The query path uses both stores:

```ts
const embedded = await env.AI.run("@cf/baai/bge-m3", { text: [question] });

const { matches } = await env.DOCS.query(embedded.data[0], {
  topK: 40,
  namespace: tenantId,
  filter: { branch: { $in: [userBranch, "all"] } },
});

const ids = matches.map((m) => m.id);
const marks = ids.map(() => "?").join(",");
const { results } = await env.DB.prepare(
  `SELECT c.vector_id, c.text, d.title
     FROM chunks c JOIN documents d ON d.id = c.document_id
    WHERE c.vector_id IN (${marks}) AND d.status = 'published'`
).bind(...ids).all();
```

The coarse filter (tenant, branch) happens in Vectorize. The fine check (is this document published, may this user see it) happens in SQL. Fetch more candidates than you need, because some will be dropped.

Know [D1's limits](https://developers.cloudflare.com/d1/platform/limits/) before you design around it. A database is capped at 10 GB and that cap cannot be raised. Each database processes one query at a time, so throughput depends on how fast your queries are. A statement takes at most 100 bound parameters, which is why the example asks for 40 IDs and not 400. Cloudflare's intended pattern is many small databases, per tenant or per customer, not one large one.

### Reranking and generation

Pass the 40 candidates and the question to the reranker on Workers AI, which scores each passage against the question, and keep the best six to eight. Then call the model, through AI Gateway so the call is logged and has a fallback (see [Workers AI and AI Gateway](https://westack.ai/blog/cloudflare-workers-ai-and-ai-gateway)). Ask for citations by chunk ID and check that the IDs it returns were in the context you sent.

## A worked example

A property management firm has 40,000 documents: leases, inspection reports and policies. It has 300 staff in 25 branches. People may see their own branch's documents and the company-wide policies.

Sizing first. At around 30 chunks per document that is 1.2 million vectors, a small fraction of one index. Chunk text at about 1 KB each is a little over 1 GB in D1 before the keyword index, comfortably inside one database, with a note to split by branch if the archive grows fivefold.

Permissions next. The rule is "my branch or everyone", which is one metadata field and one `$in` filter. That fits AI Search: 40,000 files is well under the instance limit, and branch is one of five custom fields. So the right first build is an AI Search instance over the R2 bucket, with reranking turned on and tested.

Now change one fact. HR adds personnel files that only named individuals may open. A metadata filter matches single values, not a stored list of permitted readers that changes weekly. That is the moment to build the custom path, with a grants table in D1 checked on every query. Or keep those files out of the index.

## When is pgvector or a dedicated database better?

**Postgres with pgvector** is the better choice when the rows you filter and join on are already in Postgres, when row-level security already encodes your permissions, when a document and its vectors need to change in the same transaction, or when your application does not run on Workers. Workers can still reach that database through Hyperdrive, Cloudflare's connection pooler. The full comparison is in [vector database or pgvector](https://westack.ai/blog/vector-database-or-pgvector).

**A dedicated vector database** is the better choice at hundreds of millions of vectors, when most queries carry several filters over a very large index, or when you want sparse and dense vectors fused in a single query.

**The Cloudflare stack** is the better choice when the application already runs on Workers, the corpus is in the thousands to low tens of millions of chunks, the filter model is simple, and nobody on the team wants to run a database server. It also suits products with many small tenants, where a namespace, a D1 database or an AI Search instance per tenant gives clean isolation.

Whichever you pick, keep the source documents and the chunk rows. If the index can be rebuilt by re-running ingestion, moving to another vector store later is a week of work and not a migration project.

## How to start in the first week

1. Collect 50 real questions and the documents that should answer each.
2. Create an AI Search instance over a representative sample and run the questions. Record whether the right document appears in the top five.
3. Turn on reranking and run them again. Keep whichever setting scores better.
4. Delete a document and confirm it stops appearing. Replace one and confirm the old text is gone.
5. Write down what the managed service cannot do for you. If the list is empty, you are done with infrastructure and can work on answer quality.

Only build the custom pipeline for the items on that list. Retrieval quality comes from the test set, the chunking and the reranker far more than from which index holds the vectors. This is the kind of system we build as part of our [RAG and enterprise search](https://westack.ai/services/generative-ai) and [data engineering](https://westack.ai/services/data) work.

## Frequently asked questions

### What is Cloudflare AI Search?

AI Search is Cloudflare's managed retrieval service, launched under the name AutoRAG. You connect an R2 bucket or a website, or upload files, and it converts, chunks, embeds and indexes the content. It then serves vector, keyword or hybrid search with optional reranking and answer generation. It became generally available in October 2026 with usage-based pricing.

### What are the limits of Cloudflare Vectorize?

At the time of writing an index holds up to 20 million vectors of at most 1,536 dimensions. A query returns at most 100 matches, or 50 if you ask for values or metadata, and you can define ten metadata indexes per index. The dimensions and the distance metric are fixed when the index is created.

### Should I use Vectorize or pgvector?

Use Vectorize when your application already runs on Workers, your collection fits within its index limits and your filters are simple. Use Postgres with pgvector when the data you filter and join on already lives in Postgres, when permissions are complex, or when rows and vectors need to change together in one transaction.

### Do I need D1 if I have Vectorize?

Usually, yes. Vectorize stores vectors and a small amount of metadata, not your documents' text, ownership or access rules. A relational store such as D1 holds the chunk text, document versions and who may see what. It also lets you delete or re-embed a document reliably, because you know which vector IDs belong to it.
