AI Search Worker Bindings, Namespaces and Wrangler Configuration
How to choose ai_search vs ai_search_namespaces, configure Wrangler, use remote dev, and migrate from legacy autorag on Workers.
This page summarises Cloudflare documentation on Workers bindings that connect serverless Workers to AI Search and Workers AI. Bindings expose platform resources on the Worker’s env object so your code can run search, chat, and model inference without calling public APIs for every resource type.
Bindings allow your Worker to interact with resources on the Cloudflare Developer Platform.
— Cloudflare, Get started with AI Search on Workers
What Worker bindings are and when you need them
Section titled “What Worker bindings are and when you need them”A binding is a named entry in your Wrangler configuration that maps a JavaScript variable (for example env.AI_SEARCH) to a Cloudflare resource. You need a binding when Worker code must talk to AI Search (managed retrieval and indexes) or Workers AI (on-network model inference via env.AI.run()).
Workers AI and AI Search solve different jobs:
| Binding key | env handle |
Typical use |
|---|---|---|
ai |
env.AI |
Run ML models with env.AI.run(modelName, inputs) — RAG generation, summarisation, embeddings. |
ai_search |
env.<BINDING> |
Talk to one pre-existing AI Search instance (search, chatCompletions(), items API on that instance). |
ai_search_namespaces |
env.<BINDING> |
Manage all instances in a namespace: get(), list(), create(), delete(), cross-instance search. |
Workers AI allows you to run machine learning models, on the Cloudflare network, from your own code – whether that be from Workers, Pages, or anywhere via REST API.
— Cloudflare, Wrangler configuration — Workers AI
AI Search is Cloudflare’s managed search service. A namespace is a logical grouping of AI Search instances. The binding grants full access to all instances within the namespace.
— Cloudflare, Wrangler configuration — AI Search namespaces
Unlike most bindings, the Workers AI ai binding is limited to one AI binding per Worker project (Wrangler configuration — Workers AI).
ai_search_namespaces vs ai_search: which binding to use
Section titled “ai_search_namespaces vs ai_search: which binding to use”Cloudflare documents two AI Search binding types (Workers binding):
- Namespace binding (
ai_search_namespaces): access every instance in a namespace and perform namespace-level operations at runtime. - Instance binding (
ai_search): bind directly to a single instance that must already exist in the default namespace at deploy time.
To bind directly to a pre-existing AI Search instance in the default namespace, assign an array of the below object to the
ai_searchkey. This binding does not support namespace-level operations likelist(),create(), ordelete().— Cloudflare, Wrangler configuration — AI Search instances
Choose ai_search when you know the instance name at deploy time and only need instance methods (search(), chatCompletions(), info(), stats(), items). This is the simplest path when migrating from the legacy env.AI.autorag("name") pattern (Workers binding migration).
Choose ai_search_namespaces when you must create or delete instances without redeploying, run multitenancy (one instance per tenant), or search across multiple instances in one namespace (Multitenancy, Search across multiple sources).
The new
ai_search_namespacesbinding replaces the previousenv.AI.autorag()API provided through the AI binding. It gives your Worker access to all instances within a namespace and lets you create, update, and delete instances at runtime without redeploying.— Cloudflare, Changelog — AI Search namespace binding
At runtime with a namespace binding, env.AI_SEARCH.get("my-instance") returns a handle synchronously; the instance is resolved lazily when you call search() or chatCompletions() (Namespaces).
Default namespace and multiple namespace bindings
Section titled “Default namespace and multiple namespace bindings”Every account gets a default namespace automatically. If you do not need isolation between groups of instances, set namespace to default (Namespaces — Default namespace).
You can declare several ai_search_namespaces entries in one Worker so each binding maps to a different namespace (Namespaces — Multiple namespaces).
How to add AI Search bindings in Wrangler
Section titled “How to add AI Search bindings in Wrangler”Add either ai_search_namespaces or ai_search to wrangler.jsonc or wrangler.toml. Field reference: Workers binding — namespace and instance.
Namespace binding example
Section titled “Namespace binding example”{ "$schema": "./node_modules/wrangler/config-schema.json", "compatibility_date": "2026-03-27", "ai_search_namespaces": [ { "binding": "AI_SEARCH", "namespace": "default", "remote": true } ]}compatibility_date = "2026-03-27"
[[ai_search_namespaces]]binding = "AI_SEARCH"namespace = "default"remote = trueRequired fields: binding (variable name on env) and namespace. If the namespace does not exist, Wrangler can create it on deploy (Wrangler configuration — AI Search namespaces).
The ai_search_namespaces binding requires a compatibility_date of 2026-03-27 or later (AI SDK — Bind your Worker to AI Search).
Instance binding example
Section titled “Instance binding example”{ "compatibility_date": "2026-03-27", "ai_search": [ { "binding": "MY_SEARCH", "instance_name": "my-instance", "remote": true } ]}instance_name must exist in the default namespace at deploy time (Workers binding — instance binding).
Create an instance at runtime (namespace binding only)
Section titled “Create an instance at runtime (namespace binding only)”const instance = await env.AI_SEARCH.create({ id: "my-instance",});(Changelog — namespace binding)
Workers AI binding (ai) alongside AI Search
Section titled “Workers AI binding (ai) alongside AI Search”Many AI Search apps also need Workers AI for generation or embeddings. Add a separate ai block — it does not replace AI Search bindings.
{ "ai": { "binding": "AI", "remote": true }, "ai_search": [ { "binding": "AI_SEARCH", "instance_name": "my-instance", "remote": true } ]}Example call pattern:
const answer = await env.AI.run("@cf/meta/llama-3-8b-instruct", { messages: [{ role: "user", content: "What is the square root of 9?" }],});(Build a Retrieval Augmented Generation AI)
For AI Gateway, pass a gateway option as a third argument to env.AI.run() (Set up Workers AI with AI Gateway).
Pages Functions: you can configure a Workers AI binding in Wrangler or the dashboard (Workers & Pages → project → Settings → Bindings → Workers AI) and use wrangler pages dev --ai AI for local context.env.AI (Functions bindings).
Cloudflare deprecated the @cloudflare/ai npm package in favour of the native AI binding (Workers AI changelog).
remote: true and local development with Wrangler
Section titled “remote: true and local development with Wrangler”AI Search does not run fully on your laptop. Setting remote: true on ai_search_namespaces or ai_search makes wrangler dev proxy requests to deployed AI Search (Create a simple search engine, AI Search tool configuration).
The remote option lets wrangler dev proxy requests to your deployed instance, since AI Search does not run locally.
— Cloudflare, Fetch and index single web pages
The same pattern applies when you combine a browser binding with AI Search for indexing: both may need remote: true, with compatibility dates documented in Fetch and index web pages.
For Workers AI tutorials, remote: true on the ai binding connects local dev to Cloudflare’s model catalog (Build a RAG AI — Adding the AI binding).
After changing bindings in TypeScript projects, regenerate types:
npx wrangler typesMigrate from env.AI.autorag() to AI Search bindings
Section titled “Migrate from env.AI.autorag() to AI Search bindings”Legacy access used the generic ai binding and env.AI.autorag("name"). New bindings split configuration and change response shapes (Workers binding migration):
| Area | Legacy | New |
|---|---|---|
| Wrangler | ai binding |
ai_search or ai_search_namespaces |
| Access | env.AI.autorag("name") |
env.MY_INSTANCE or env.AI_SEARCH.get("name") |
| Search input | query string | messages array or query string |
| Response | data array |
chunks array |
Step 1 — Wrangler: replace [ai] with [[ai_search]] (simplest) or [[ai_search_namespaces]], and set compatibility_date to at least 2026-03-27 (Workers binding migration — Step 1).
Step 2 — Types: change Env from AI: Ai to MY_INSTANCE: AiSearchInstance (or your namespace binding type) (Step 2: Update the type definition).
Keep a dedicated ai binding only for Workers AI inference, not for AI Search instance access.
Automatic provisioning and IaC
Section titled “Automatic provisioning and IaC”Wrangler can automatically provision some resources on deploy when bindings omit IDs; AI Search is listed among supported types (Wrangler configuration — Automatic provisioning). For Terraform-style setups, binding shapes include type = "ai" for Workers AI (Infrastructure as Code).
Common mistakes
Section titled “Common mistakes”- Using only the
aibinding and callingautorag()— replaced byai_search/ai_search_namespaces(Workers binding migration). - Expecting
list()/create()on an instance binding — those exist only on namespace bindings (Namespaces). - Deploying with
ai_searchwhen the instance is not in the default namespace at deploy time (Wrangler configuration — AI Search instances). - Local dev without
remote: true— AI Search operations fail or do not match production (Get started with AI Search on Workers). - Adding multiple
aibindings in one project — only one is allowed (Wrangler configuration — Workers AI). - Forgetting
compatibility_date≥2026-03-27for namespace bindings (Workers binding reference).
How to check this on your site
Section titled “How to check this on your site”- Open your Wrangler file and confirm
ai_searchorai_search_namespacesentries match the Workers binding reference (binding,namespaceorinstance_name, andremoteif you use local dev). - Set
compatibility_dateto at least2026-03-27for AI Search namespace bindings, then runnpx wrangler typesand verify generatedEnvtypes (Talk to your knowledge base). - Deploy with
npx wrangler deployand call the Worker URL; for Workers AI, confirmenv.AI.run()returns JSON as in Workers Bindings get started. - If you use Pages, open Workers & Pages in the Cloudflare dashboard, check Bindings for Workers AI, and redeploy after changes.
- You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact sentence you are asking about.