This site is not affiliated with or endorsed by Cloudflare, Inc. It simply showcases experiments built using Cloudflare services.
Cloudflare Experiments
AI & Machine Learning

AI Search Demo

Query Cloudflare AI Search (managed RAG) from a Worker

Query Cloudflare AI Search (managed RAG, formerly AutoRAG) from a Worker via the Workers AI binding using env.AI.autorag(name).aiSearch({ query }).

API Reference

Run a natural-language search against your AI Search instance.

q string (required)

Natural language query.

Example Request

curl "https://your-worker.workers.dev/search?q=What%20is%20Workers%20AI"

Success Response

{
  "answer": "Generated answer from AI Search",
  "results": [],
  "query": "What is Workers AI",
  "instance": "demo-index"
}

Error Codes

  • 400 - Missing or empty q (INVALID_QUERY)
  • 502 - AI Search / AutoRAG failure (SEARCH_ERROR)

Use Cases

  • Learn managed RAG via AI Search from a Worker
  • Prototype Q&A over indexed docs without building Vectorize yourself
  • Compare AutoRAG-style search with a custom RAG Mini Search experiment
  • Demo grounded answers with retrieval metadata

Limitations

  • Requires a configured AI Search instance and indexed data source
  • Uses the legacy AI.autorag(...).aiSearch API; newer projects may prefer dedicated ai_search bindings
  • Remote/account access is typically needed for real searches
  • No auth on the public /search endpoint

Use in your project

Copy these files into an existing Worker. Prefer Deployment to try the full experiment first. Source: apps/experiments/ai-search-demo.

DependenciesNoneBindingsWorkers AI (AI binding)PlatformWorkers AI
import {  DEFAULT_INSTANCE_NAME,  DEFAULT_MAX_RESULTS,  MAX_QUERY_LENGTH,} from "../constants/defaults";import type { AISearchBinding, AISearchResult, Env } from "../types/env";import type { SearchResponse } from "../types/search";export function validateQuery(input: string | undefined): string | null {  if (!input || typeof input !== "string") return null;  const trimmed = input.trim();  if (!trimmed || trimmed.length > MAX_QUERY_LENGTH) return null;  return trimmed;}/** * Wraps the evolving Workers AI Search / AutoRAG API behind a stable interface. * Prefer injecting a mock AISearchBinding in tests. */export function createAISearchBinding(env: Env): AISearchBinding {  const instanceName = env.INSTANCE_NAME?.trim() || DEFAULT_INSTANCE_NAME;  return {    async search(params: { query: string; max_num_results?: number }): Promise<AISearchResult> {      const result = await env.AI.autorag(instanceName).aiSearch({        query: params.query,        max_num_results: params.max_num_results ?? DEFAULT_MAX_RESULTS,      });      return result as AISearchResult;    },  };}export async function runSearch(  search: AISearchBinding,  query: string,  instanceName: string): Promise<SearchResponse> {  const result = await search.search({    query,    max_num_results: DEFAULT_MAX_RESULTS,  });  return {    answer: result.response?.trim() || "",    results: Array.isArray(result.data) ? result.data : [],    query,    instance: instanceName,  };}

Deployment

Click the deploy button

Deploy to Cloudflare Workers

Create an AI Search instance in the dashboard, index your data, and set INSTANCE_NAME in wrangler.json vars (default: demo-index).

Test your deployment

curl "https://your-worker.workers.dev/search?q=hello"

Local Development

cd apps/experiments/ai-search-demo
npm install
npm run dev
curl "http://localhost:8787/search?q=hello"

AI Search typically requires a remote instance (wrangler may need remote AI / account access).

Configuration

wrangler.json declares:

  • Workers AI binding AI
  • Var INSTANCE_NAME (AI Search / AutoRAG instance name)

See AI Search Workers binding docs for the newer dedicated binding shape.

Cloudflare Features Used

On this page