Skip to content

How to Use Cloudflare AI Search: Data Sources, Hybrid Retrieval, and Workers Bindings

Aug 29, 202612 min
TL;DRFormerly AutoRAG, the managed search primitive, generally available since 2026-10-01 with billing from 2026-12-01: drop files into built-in storage or attach R2, websites, or D1 (beta), auto-index with Markdown conversion plus vector and BM25, retrieve with hybrid by default, RRF, and optional reranking, and query from Workers via namespace or instance bindings, REST, or MCP. Chinese content needs the trigram keyword tokenizer.

Look up & save

Loading dictionary…

中文版

If every agent needs search, do you build the vector index, the chunking and sync pipeline, and the BM25 fusion yourself — or reach for a primitive? Cloudflare AI Search is the latter: formerly AutoRAG, now positioned as a managed search primitive. Create an instance, give it data, query it with natural language; the R2, Vectorize, and Workers AI plumbing is handled for you. This post covers how data gets in, how indexing runs, how to tune retrieval, and how to wire it from a Worker — so you can decide when to use the managed path and when to assemble your own.

What AI Search is and why it is called a search primitive

The design pitch is literal: make "searchable" as basic as fetch() in Workers. The docs call it the search primitive for your applications and agents (Overview). It is not another vector database — it is a full pipeline: upload or connect a data source → auto-index and keep it fresh → hybrid retrieval → scored chunks with sources, optionally followed by generation.

How it differs from the usual alternatives:

  • DIY on Vectorize: most flexible — you control chunk size, embedding model, filter extraction, and fallback — but you own crawling, Markdown Conversion, reindexing, and observability. See the on-site Vector Database Comparison.
  • External managed search like Azure AI Search or Algolia: feature-complete, but with an extra hop, separate billing, and data movement out of the Cloudflare edge. AI Search wins on colocation with Workers and the Agents SDK.
  • The old env.AI.autorag(): still works, but marked legacy. New capabilities — built-in storage, namespace bindings, cross-instance search, boost — are only on the new Workers binding. The official upgrade path is Workers binding migration.

Good fits: documentation and knowledge-base search, research tools for agents, per-tenant or per-agent searchable context (e.g., past resolutions per customer in support). Poor fits: teams that need custom chunking, more than five metadata fields per instance, or pinned embedding versions for offline evaluation.

How data gets in: four sources and the indexing pipeline

AI Search ingests from four sources (Data source):

  1. Built-in storage: every instance ships with its own storage and vector index. Upload files via API and they are indexed — no R2 bucket to create first. This is the default since April 2026 and the right choice for dynamic per-agent instances.
  2. R2 Bucket: attach an existing R2 bucket as the source. AI Search syncs it continuously. Use it when your corpus already lives in R2.
  3. Website: attach a domain you own. AI Search crawls it with Browser Run (formerly Browser Rendering) and supports discover and sitemap Parse types.
  4. D1 database (beta, since 2026-11-12): you provide a read-only SQL query, and AI Search indexes each returned row as a separate document, adding, updating, and removing rows on scheduled syncs. Use it to make articles, FAQs, or product data that already live in D1 searchable without exporting them to files first.

File types are two-tiered: plain text (.md, .json, .csv, .py, .go, etc.) indexes directly; rich formats (.pdf, .docx, .xlsx, .html, .png/.jpg, etc.) go through Markdown Conversion first — images and scanned documents go through OCR, billed as image processing ingestion tokens. The per-file limit depends on format: 10 MiB for plain text, code, and PDFs with OCR enabled; 4 MiB for PDFs without OCR and other formats converted to Markdown. Oversized files are not indexed and appear in error logs.

Indexing is automatic (Automated indexing). R2, Website, and D1 sources sync on a schedule, every 24 hours by default since 2026-11-02 (previously 6 hours), adjustable from 15 minutes to 24 hours; built-in storage indexes on upload. The ownership shift matters: built-in files live on AI Search-managed R2 and Vectorize — you no longer need a bucket in your account. The R2 bucket that early AutoRAG created for you is no longer written to and can be removed (see Limits & pricing history).

// Built-in storage: upload and index
const instance = env.AI_SEARCH.get("my-instance");
const item = await instance.items.upload("handbook.pdf", pdfBytes);
// or wait until searchable
const ready = await instance.items.uploadAndPoll("handbook.pdf", pdfBytes);

Indexing and models: five stages from Markdown conversion to embedding

The pipeline is fixed; the models at each stage are configurable (Models):

  1. Image to Markdown (optional): object detection plus captioning.
  2. Embedding: turns chunks and queries into vectors. This is the only model choice locked at instance creation.
  3. Query rewriting (optional): an LLM reformulates the user query for better recall.
  4. Reranking (optional): a cross-encoder re-scores fused results by semantic relevance.
  5. Generation: produces the final answer from retrieved context. The generation model is selectable at creation and can be overridden later in the dashboard or per request.

Providers are either Workers AI models or external models via AI Gateway with your own OpenAI or Anthropic keys (Bring your own keys). Attach a Gateway when creating the instance or switch it under Settings, then pick external models. "Smart Default" lets Cloudflare choose and auto-update; explicit selection pins a model but requires tracking the Supported models lifecycle (Production → Announcement → End of life).

Since GA, the Workers AI embedding and reranking calls that AI Search makes are included in AI Search pricing and no longer appear on your Workers AI bill or in AI Gateway logs; generation, query rewriting, and external providers still go through your account and gateway.

Trade-off: Smart Default saves ops but scores can drift on upgrade; pinning is reproducible but you must follow Release notes and deprecation notices. For Traditional Chinese, generation quality is often steadier with external models — the Gateway integration is the practical bridge.

How to query: vector, keyword, and hybrid

Retrieval is where the recent release changed most (Search modes, Hybrid search, Keyword search).

  • Vector-only: strong on intent, weak on exact terms. Searching ERR_CONNECTION_REFUSED timeout may return generic networking docs instead of the page that literally contains the error code.
  • Keyword-only (BM25): scores by term frequency, rarity, and document length — exact terms win, paraphrases lose.
  • Hybrid: runs both in parallel and fuses. Instances created since GA use hybrid by default (index_method: { vector: true, keyword: true }); fusion is rrf (Reciprocal Rank Fusion, by rank not score, the default) or max (higher of the normalized scores). Hybrid search docs recommend rrf for most cases. The DIY counterpart and formula are covered in Hybrid Search: BM25 + Vector + RRF.

Tunable parameters (instance-level or per-request override):

  • keyword_tokenizer: porter (stemming, running matches run) or trigram (substring, conf matches configuration, better for code). Chinese, Japanese, and Korean content needs trigram: porter splits on whitespace, so a Chinese query is treated as one exact phrase and the keyword leg usually returns nothing. Since 2026-10-16 trigram also matches full natural-language CJK questions; changing the tokenizer triggers a full reindex.
  • keyword_match_mode: and (all terms required) or or (any term).
  • reranking: cross-encoder rerank after fusion, e.g. @cf/baai/bge-reranker-base.
  • query_rewrite: rewrites the query for recall.
  • boost_by: boosts ranking by metadata, e.g. timestamp desc for recency, up to 3 fields (Filtering).
  • filters: filter by custom metadata with eq/ne/gt/gte/lt/lte and and/or compounds. Only the first 64 bytes of a string field are indexed — keep filterable keys short.
  • max_num_results (1–50, default 10), match_threshold (0–1, default 0.4), context_expansion (0–3 surrounding chunks).
const instance = await env.AI_SEARCH.create({
  id: "my-instance",
  index_method: { vector: true, keyword: true },
  indexing_options: { keyword_tokenizer: "trigram" },  // trigram for CJK content
  retrieval_options: { keyword_match_mode: "or" },
  fusion_method: "rrf",
  reranking: true,
  reranking_model: "@cf/baai/bge-reranker-base",
});

// Per-request override and boosting
const hits = await instance.search({
  messages: [{ role: "user", content: "How to fix ERR_CONNECTION_REFUSED?" }],
  ai_search_options: {
    retrieval: {
      retrieval_type: "hybrid",
      fusion_method: "rrf",
      keyword_match_mode: "or",
      max_num_results: 8,
      filters: { category: "runbook" },
      boost_by: [{ field: "timestamp", direction: "desc" }],
    },
    reranking: { enabled: true },
  },
});

The other key primitive is cross-instance search. Shared docs and per-customer history often live in separate instances; the namespace search() merges them into one ranked list:

// From the official support-agent example
const results = await env.SUPPORT_KB.search({
  query: "billing error",
  ai_search_options: { instance_ids: ["product-knowledge", "customer-abc123"] },
});

That avoids two round-trips and lets boost_by and reranking apply after the merge.

How to connect: five interfaces and two Workers bindings

Pick the surface by who calls and when the instance is known:

InterfaceBest forNotes
Workers bindingInside Workers and AgentsLowest latency; namespace and instance bindings
REST APIBackend services outside WorkersAccount API token with AI Search permission
Wrangler CLIOps and one-offswrangler ai-search create/list/delete
Python SDKOffline batches and pipelinesParity with REST, good for crawlers and ETL
DashboardManual setup and inspectionPick Gateways, check index status
MCP server + UI snippetsExpose to models or websitesEvery instance ships with an MCP endpoint and embeddable search

Workers offers two bindings (Workers binding):

Namespace binding ai_search_namespaces — dynamic, for per-customer or per-agent instances:

// wrangler.jsonc
{
  "ai_search_namespaces": [{ "binding": "SUPPORT_KB", "namespace": "support" }],
  "ai": { "binding": "AI" }
}
await env.SUPPORT_KB.create({ id: `customer-${customerId}`, index_method: { vector: true, keyword: true } });
const inst = env.SUPPORT_KB.get(`customer-${customerId}`);
await inst.search({ messages: [{ role: "user", content: "What did we try last time?" }] });

Instance binding ai_search — static, bound to a single instance in the default namespace at deploy time, simplest call site:

{ "ai_search": [{ "binding": "MY_SEARCH", "instance_name": "my-instance" }] }
await env.MY_SEARCH.search({ messages: [{ role: "user", content: "What is Cloudflare?" }] });

The legacy env.AI.autorag("my-rag").search({ query }) still works but takes messages (or query) plus ai_search_options and receives no new features. See Workers binding migration and REST API migration.

The recommended Agents SDK pattern is to expose AI Search as a tool (see AI Search: the search primitive for your agents): the model decides when to search_knowledge_base and when to save_resolution (via uploadAndPoll so the next turn is searchable), and uses instance_ids to span shared knowledge and private context.

Limits, pricing, and when not to use it

Hard limits (Limits & pricing):

LimitWorkers FreeWorkers Paid
Instances per account1005,000 by default, $0.10/month each beyond
Namespaces per account100100
Files per instance100,0001,000,000 (500,000 with hybrid)
Max file size (plain text, code, OCR PDFs)10 MiB10 MiB
Max file size (PDFs without OCR, other formats)4 MiB4 MiB
Included queries per month1,000 semantic + 1,000 full-text1,000 semantic + 1,000 full-text (billed beyond)
Pages per discover crawl100,000100,000
Instances per cross-instance request1010
Pages crawled per day500Unlimited
Custom metadata fields5 per instance5 per instance
Metadata per vector10 KiB (incl. overhead)10 KiB
Filterable string prefixFirst 64 bytesFirst 64 bytes

Pricing: AI Search went GA on 2026-10-01. Usage-based billing was scheduled for 11-01 and then postponed to 2026-12-01. Every account gets 5 million ingestion tokens, 10 GB-month of storage, 1,000 semantic queries, and 1,000 full-text queries per month; beyond that:

UsagePrice
Base ingestion$0.75 per million tokens
Image processing (including OCR)an additional $0.50 per million tokens
Storage$2.00 per GB-month
Semantic, vector, and hybrid queries$0.75 per 1,000 queries
Full-text queries$0.10 per 1,000 queries

Ingestion tokens are counted on the final chunks with the cl100k_base tokenizer, so overlapping text is counted in every chunk it appears in; storage counts only the content AI Search keeps (uploaded files and Markdown extracted from sources), not the vector or keyword indexes. The R2, Vectorize, and Browser Run usage AI Search runs on your behalf is not billed separately; generation, query rewriting, and the file summaries added on 2026-11-17 are billed through Workers AI or the external provider. Older invoices with separate R2/Vectorize line items are pre-migration history.

The pricing cuts differently for two kinds of usage: a FAQ-style instance with little content and many queries pays mostly for hybrid queries at $0.75 per thousand, while a large knowledge base with few queries pays mostly for ingestion and storage. Run your corpus through tiktoken once before estimating.

When not to use it:

  • You need custom chunking, overlap, or multi-granularity indexing (AI Search chunking is fixed).
  • You need more than five filterable metadata fields or filtering on long strings (only the first 64 bytes are filterable).
  • You need pinned embedding/reranker versions for long-horizon evaluation (Smart Default drifts; explicit choices still face the Model lifecycle).
  • Files frequently exceed the limit (e.g., scanned books over 10 MiB) — pre-split before upload.

Conversely, if the job is "hybrid search over a corpus with exact-term hits, dynamic per-tenant instances, and minimal ops," the managed trade is compelling. The same build-vs-buy note closes the on-site Hybrid Search piece: use managed to save ops, build on Vectorize for full control over filter extraction and degradation.

Overall

The increment is not "can do RAG" — it is "RAG without the dirty work as a primitive": built-in storage per instance, auto-sync for Website and R2 sources, hybrid retrieval with boost_by and filters tunable per request, and namespace bindings plus cross-instance search that make multi-tenant and multi-agent isolation cheap.

Start with one ai_search instance (minimal config), validate recall and match_threshold, then compare vector vs keyword vs hybrid with reranking; only move to ai_search_namespaces and instance_ids when you need tenancy. A concrete next step tonight: take a corpus you already have (e.g., src/content/posts/), create a test instance on a Paid account, upload 50 documents to built-in storage, query the same questions with the three retrieval types, and log scoring_details (vector_score/keyword_score/fusion_method) before choosing a default.

Update Log

  • 2026-11-23: AI Search went GA on 2026-10-01 with billing postponed to 2026-12-01, so the pricing section is rewritten with included usage and rates; added D1 (beta) as a data source; new instances are hybrid by default; CJK content should use the trigram tokenizer; per-file limits are now 10 MiB / 4 MiB by format; default sync interval is 24 hours; the query rows in the limits table now show monthly included usage; Browser Rendering is now Browser Run.
  • 2026-08-30: Added the post to the Cloudflare AI Stack series and kept the Edge Platform reading path through additionalSeries.

References