Skip to content

Workers binding

Workers provides a serverless execution environment that allows you to create new applications or augment existing ones. Use a Workers binding to create, list, update, and delete AI Search instances from a Cloudflare Worker. You can also check instance configuration and monitor indexing progress.

Configure the binding

To use AI Search with Workers, you must create an AI Search binding. You create bindings by updating your Wrangler configuration. AI Search provides two types of bindings:

  • Namespace binding: ai_search_namespaces
  • Instance binding: ai_search

Namespace binding

Access all instances within a namespace. You can get, create, list, and delete instances at runtime.

JSONC
{
"$schema": "./node_modules/wrangler/config-schema.json",
"compatibility_date": "2026-03-27",
"ai_search_namespaces": [
{
"binding": "AI_SEARCH",
"namespace": "my-namespace"
}
]
}
FieldTypeRequiredDescription
bindingstringYesThe variable name available on env. For example, "AI_SEARCH" makes it accessible as env.AI_SEARCH.
namespacestringYesThe namespace to bind to. A default namespace is created automatically for every account. If the namespace does not exist, Wrangler creates it on deploy.
remotebooleanNoSet to true for local development with wrangler dev.

Instance binding

Bind directly to a single instance in the default namespace. Use this when you know which instance you need at deploy time.

JSONC
{
"$schema": "./node_modules/wrangler/config-schema.json",
"compatibility_date": "2026-03-27",
"ai_search": [
{
"binding": "MY_SEARCH",
"instance_name": "my-instance"
}
]
}
FieldTypeRequiredDescription
bindingstringYesThe variable name available on env. For example, "MY_SEARCH" makes it accessible as env.MY_SEARCH.
instance_namestringYesThe name of the AI Search instance. Must exist in the default namespace at deploy time.
remotebooleanNoSet to true for local development with wrangler dev.

Namespace methods

The following methods are only available when using the ai_search_namespaces binding. The namespace handle (env.AI_SEARCH) exposes methods for working with instances within a namespace.

get()

Returns a handle to a specific instance. This is synchronous and does not make a network call. The instance is resolved lazily when you call methods like search() or info().

TypeScript
const instance = env.AI_SEARCH.get("my-instance");
const results = await instance.search({
messages: [{ role: "user", content: "What is Cloudflare?" }],
});

Parameters

ParameterTypeRequiredDescription
namestringYesThe name of the instance to get a handle to.

list()

Returns all instances within the namespace.

TypeScript
const { result, result_info } = await env.AI_SEARCH.list();
for (const instance of result) {
console.log(`${instance.id} (${instance.type}) - ${instance.status}`);
}
// result_info.total_count contains the total number of instances

Parameters

ParameterTypeRequiredDescription
pagenumberNoThe page number to return. Defaults to 1.
per_pagenumberNoThe number of instances per page. Defaults to 20. Maximum 100.
searchstringNoSearch instances by ID.
order_bystringNoSort column. Valid value: created_at. Defaults to created_at.
order_by_directionstringNoSort direction. Valid values: asc, desc. Defaults to desc.

Response

FieldTypeDescription
resultarrayArray of instance objects.
result[].idstringThe instance identifier.
result[].typestringThe data source type (r2, web-crawler, or null for empty instances).
result[].sourcestringThe data source location.
result[].statusstringThe instance status (active, waiting, indexing).
result[].enablebooleanWhether the instance is enabled.
result[].namespacestringThe namespace the instance belongs to.
result[].created_atstringISO 8601 timestamp of when the instance was created.
result[].modified_atstringISO 8601 timestamp of the last modification.
result_infoobjectPagination metadata.
result_info.total_countnumberTotal number of instances in the namespace.

create()

Creates a new instance and returns a handle to it. You can create instances backed by a data source or create empty instances for use with the Items API.

Create an empty instance for file uploads:

AI Search instances come with built-in storage where you can upload documents directly.

TypeScript
const instance = await env.AI_SEARCH.create({
id: "knowledge-base",
});
// Upload documents using the Items API
await instance.items.upload("guide.pdf", pdfArrayBuffer);

Create a web-crawler instance:

Automatically crawl and index a website that you own. For more configuration options, refer to Website data source.

TypeScript
const instance = await env.AI_SEARCH.create({
id: "my-docs",
type: "web-crawler",
source: "developers.cloudflare.com",
});

Create an R2-backed instance:

Index documents stored in an R2 bucket. For more configuration options, refer to R2 data source.

TypeScript
const instance = await env.AI_SEARCH.create({
id: "internal-docs",
type: "r2",
source: "my-docs-bucket",
});

Parameters

id string required

The unique identifier for the AI Search instance. Must be 1-64 characters and match the pattern ^[a-z0-9_]+(?:-[a-z0-9_]+)*$.


type string optional

The type of data source. Valid values: r2, web-crawler. Required when creating an instance with a data source. Omit when creating an empty instance for use with the Items API.


source string optional

The data source location. For r2 type, this is the R2 bucket name. For web-crawler type, this is the website domain. Required when type is specified.


source_params object optional

Additional parameters for the data source.

  • prefix string optional

    • For R2 sources, limits indexing to objects with this key prefix.
  • r2_jurisdiction string optional

    • The jurisdiction for the R2 bucket, for example eu.
  • include_items array optional

    • Glob patterns for paths to include in indexing. For example: ["/blog/**", "/docs/**/*.html"].
  • exclude_items array optional

    • Glob patterns for paths to exclude from indexing. For example: ["/admin/**", "/private/**"].
  • web_crawler object optional

    • Configuration for web crawler sources.

    • parse_type string optional

      • The parsing method. Valid value: sitemap.
    • parse_options object optional

      • include_headers object optional

        • Custom HTTP headers to include when crawling.
      • include_images boolean optional

        • Whether to include images in the index.
      • specific_sitemaps array optional

        • Specific sitemap URLs to crawl. For example: ["https://example.com/sitemap.xml"].
      • use_browser_rendering boolean optional

        • Use Browser Run (formerly Browser Rendering) to crawl JavaScript-rendered pages.
    • store_options object optional

      • storage_type string optional

        • The storage type. Valid value: r2.
      • storage_id string optional

        • The storage bucket ID.
      • r2_jurisdiction string optional

        • The jurisdiction for the storage bucket.

index_method object optional

Configures which indexing methods are enabled for the instance. Determines whether vector (semantic) search, keyword search, or both are available. At least one must be true.

  • vector boolean optional

    • Enable vector-based semantic search. Defaults to true.
  • keyword boolean optional

    • Enable keyword-based search. Defaults to false.

Set both to true for hybrid search.


fusion_method string optional

Controls how vector and keyword scores are combined when using hybrid search. Valid values: rrf (Reciprocal Rank Fusion), max (takes the maximum score). Defaults to rrf.


indexing_options object optional

Configuration for how content is indexed.

  • keyword_tokenizer string optional
    • The tokenizer used for keyword search indexing. Valid values: porter (stemming-based), trigram (character n-gram). Defaults to porter.

retrieval_options object optional

Default retrieval configuration for the instance. These defaults can be overridden per-request using ai_search_options.

  • keyword_match_mode string optional

    • Controls how keyword (BM25) matching selects candidate documents. and requires all terms to match. or requires any term to match. Defaults to and.
  • boost_by array optional

    • Default boost fields applied to all search queries. Maximum 3 items. Each item has:
      • field string required - The metadata field name to boost by. Maximum 64 characters.
      • direction string optional - The boost direction. Valid values: asc, desc, exists, not_exists.

sync_interval number optional

Seconds between automatic data source syncs. Valid values: 3600, 7200, 14400, 21600, 43200, 86400. Defaults to 21600 (6 hours).


token_id string optional

The UUID of the service API token to use for this instance. Only required if you have never created an AI Search instance before. Refer to the API get started guide for how to create and register a service token.


ai_gateway_id string optional

The AI Gateway ID to route requests through for logging and analytics.


embedding_model string optional

The embedding model to use for vectorizing content.


ai_search_model string optional

The text-generation model to use for generating responses.


rewrite_query boolean optional

Enable query rewriting to improve retrieval accuracy. Defaults to false.


rewrite_model string optional

The model to use for query rewriting.


reranking boolean optional

Enable reranking to reorder retrieved results by semantic relevance. Defaults to false.


reranking_model string optional

The reranking model to use. Valid value: @cf/baai/bge-reranker-base.


chunk_size number optional

The size of chunks when splitting documents. Minimum value: 64.


chunk_overlap number optional

The overlap between chunks. Minimum value: 0.


max_num_results number optional

The default maximum number of results to return. Minimum value: 1.


score_threshold number optional

The default minimum score threshold for results. Minimum value: 0.


cache boolean optional

Enable response caching. Defaults to true.


cache_threshold string optional

The cache matching threshold. Valid values: super_strict_match, close_enough, flexible_friend, anything_goes. Defaults to close_enough.


custom_metadata array optional

Custom metadata fields to extract and index from documents.

  • field_name string required

    • The name of the metadata field.
  • data_type string required

    • The data type of the field. Valid values: text, number, boolean, datetime.

enable boolean optional

Whether the instance is enabled. Defaults to true.

Response

Returns an AiSearchInstance handle that is immediately usable for calling methods like search(), info(), stats(), and items.upload(). Call info() on the handle to get the instance configuration.

delete()

Permanently deletes an instance and all its indexed content. This action cannot be undone.

TypeScript
await env.AI_SEARCH.delete("old-docs");

Parameters

ParameterTypeRequiredDescription
namestringYesThe name of the instance to delete.

Response

Returns void. Throws an error if the instance does not exist.

Instance methods

The following methods are available on both the ai_search_namespaces and ai_search bindings. With the namespace binding, call methods on the handle returned by get(). With the instance binding, call methods directly on the binding (for example, env.MY_SEARCH.info()).

The examples below use the namespace binding.

update()

Partially updates the instance configuration. Only the fields you pass are modified.

TypeScript
const updated = await env.AI_SEARCH.get("my-instance").update({
ai_search_model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast",
reranking: true,
});

Parameters

Accepts a partial version of the create parameters. Only the fields you include are updated.

FieldTypeDescription
ai_search_modelstringThe text-generation model.
embedding_modelstringThe embedding model.
index_methodobjectIndexing methods: \{ vector: boolean, keyword: boolean \}.
fusion_methodstringHow vector and keyword scores are combined (rrf or max).
indexing_optionsobjectIndexing configuration including keyword_tokenizer.
retrieval_optionsobjectRetrieval configuration including keyword_match_mode and boost_by.
rerankingbooleanTurn on or off reranking.
reranking_modelstringThe reranking model.
rewrite_querybooleanTurn on or off query rewriting.
rewrite_modelstringThe query rewriting model.
sourcestringUpdate the data source location.
cachebooleanTurn on or off response caching.
chunk_sizenumberToken size of each chunk.
chunk_overlapnumberToken overlap between chunks.
score_thresholdnumberMinimum score threshold for results.
max_num_resultsnumberMaximum number of results per query.
custom_metadataarrayCustom metadata field definitions.
sync_intervalnumberSeconds between automatic data source syncs.

Response

Returns the updated instance configuration. Same shape as info().

info()

Returns the current configuration and metadata for the instance.

TypeScript
const info = await env.AI_SEARCH.get("my-instance").info();

Response

FieldTypeDescription
idstringThe instance identifier.
typestringThe data source type (r2, web-crawler, or null).
sourcestringThe data source location.
namespacestringThe namespace the instance belongs to.
statusstringThe instance status (active, waiting, indexing).
enablebooleanWhether the instance is enabled.
created_atstringTimestamp of when the instance was created.
modified_atstringTimestamp of the last modification.
ai_search_modelstringThe text-generation model.
embedding_modelstringThe embedding model.
rerankingbooleanWhether reranking is enabled.
reranking_modelstringThe reranking model.
rewrite_querybooleanWhether query rewriting is enabled.
rewrite_modelstringThe query rewriting model.
cachebooleanWhether response caching is enabled.
cache_thresholdstringThe similarity threshold for cache hits.
index_methodobjectWhich indexing methods are enabled (vector, keyword).
fusion_methodstringHow vector and keyword scores are combined (rrf or max).
indexing_optionsobjectIndexing configuration including keyword_tokenizer.
retrieval_optionsobjectRetrieval configuration including keyword_match_mode and boost_by.
chunk_sizenumberToken size of each chunk.
chunk_overlapnumberToken overlap between chunks.
score_thresholdnumberMinimum score threshold for results.
max_num_resultsnumberMaximum number of results per query.
sync_intervalnumberSeconds between automatic data source syncs.
custom_metadataarrayCustom metadata field definitions.
last_activitystringTimestamp of the last indexing activity.

stats()

Returns the current indexing progress for the instance. Use this to poll for completion after creating an instance or uploading files.

TypeScript
const stats = await env.AI_SEARCH.get("my-instance").stats();

Response

FieldTypeDescription
queuednumberItems waiting to be processed.
runningnumberItems currently being processed.
completednumberItems successfully indexed.
errornumberItems that failed to index.
skippednumberItems skipped during indexing.
outdatednumberItems that need re-indexing.
last_activitystringISO 8601 timestamp of the last indexing activity.
file_embed_errorsobjectMap of file IDs to embedding error details.
engine.vectorize.vectorsCountnumberTotal number of vectors stored.
engine.vectorize.dimensionsnumberDimensions of the vector embeddings.
engine.r2.payloadSizeBytesnumberTotal size of stored payloads in bytes.
engine.r2.metadataSizeBytesnumberTotal size of stored metadata in bytes.
engine.r2.objectCountnumberTotal number of objects in storage.

Local development

Local development is supported by proxying requests to your deployed AI Search instance. Add remote: true to your binding configuration to enable local development with wrangler dev.

JSONC
// wrangler.jsonc
{
"ai_search": [
{
"binding": "MY_SEARCH",
"instance_name": "my-instance",
"remote": true,
},
],
}