AI SDK (2)·续

Stream a response

For longer responses, use streamText instead of generateText. AI Search emits the retrieved chunks as source parts before the first text part.

// streamText returns right away; tokens stream in as they are generated.
const result = streamText({
    model: aiSearch.get(INSTANCE_NAME).chat(),
    messages: [{ role: "user", content: query }],
});

// toTextStreamResponse() streams the generated text only.
return result.toTextStreamResponse();
// streamText returns right away; tokens stream in as they are generated.
const result = streamText({
    model: aiSearch.get(INSTANCE_NAME).chat(),
    messages: [{ role: "user", content: query }],
});

// toTextStreamResponse() streams the generated text only.
return result.toTextStreamResponse();

toTextStreamResponse() sends the generated text and drops the sources. To stream the retrieved chunks as well, return a UI message stream with sendSources enabled, or read result.fullStream directly:

// sendSources forwards each retrieved chunk as a source-url part. They arrive
// before the first text part, so you can render citations as the answer streams.
return result.toUIMessageStreamResponse({ sendSources: true });
// sendSources forwards each retrieved chunk as a source-url part. They arrive
// before the first text part, so you can render citations as the answer streams.
return result.toUIMessageStreamResponse({ sendSources: true });

Search as a tool

With chat(), AI Search searches your instance on every request. To let the model decide when to search instead, expose instance.search() as an AI SDK tool ↗︎ and pass it to a model that supports function calling, such as a Workers AI model. This is the pattern to use in an agent, where the model chooses between searching and other tools.

Install the Workers AI provider and Zod. Use version 3 of workers-ai-provider: the latest version 4 requires AI SDK v7, but ai-search-provider requires v6, so npm fails to install them together.

npmyarnpnpmbun

npm i workers-ai-provider@^3 zod
yarn add workers-ai-provider@^3 zod
pnpm add workers-ai-provider@^3 zod
bun add workers-ai-provider@^3 zod

Add a Workers AI binding to your Wrangler configuration:

{
  "$schema": "./node_modules/wrangler/config-schema.json",
  "ai": {
    "binding": "AI"
  }
}
[ai]
binding = "AI"

Then define a search tool. The model calls it when it needs to retrieve content:

import { createWorkersAI } from "workers-ai-provider";
import { generateText, tool, stepCountIs } from "ai";
import { z } from "zod";

const instance = aiSearch.get(INSTANCE_NAME);
// The tool-caller must support function calling. Use a dedicated model here
// rather than the AI Search chat model, which retrieves on every request.
const workersai = createWorkersAI({ binding: env.AI });

const { text } = await generateText({
    model: workersai("@cf/zai-org/glm-5.2"),
    messages: [{ role: "user", content: query }],
    tools: {
        search_knowledge_base: tool({
            description: "Search the indexed knowledge base for relevant content.",
            inputSchema: z.object({
                query: z.string().describe("The search query"),
            }),
            // The model decides when to call this; it searches the instance.
            execute: async ({ query }) =>
                instance.search({
                    query,
                    ai_search_options: { retrieval: { max_num_results: 5 } },
                }),
        }),
    },
    // Cap the tool-call loop so the model cannot invoke tools indefinitely.
    stopWhen: stepCountIs(5),
});
import { createWorkersAI } from "workers-ai-provider";
import { generateText, tool, stepCountIs } from "ai";
import { z } from "zod";

const instance = aiSearch.get(INSTANCE_NAME);
// The tool-caller must support function calling. Use a dedicated model here
// rather than the AI Search chat model, which retrieves on every request.
const workersai = createWorkersAI({ binding: env.AI });

const { text } = await generateText({
    model: workersai("@cf/zai-org/glm-5.2"),
    messages: [{ role: "user", content: query }],
    tools: {
        search_knowledge_base: tool({
            description: "Search the indexed knowledge base for relevant content.",
            inputSchema: z.object({
                query: z.string().describe("The search query"),
            }),
            // The model decides when to call this; it searches the instance.
            execute: async ({ query }) =>
                instance.search({
                    query,
                    ai_search_options: { retrieval: { max_num_results: 5 } },
                }),
        }),
    },
    // Cap the tool-call loop so the model cannot invoke tools indefinitely.
    stopWhen: stepCountIs(5),
});

6. Test it locally

Before you deploy, confirm the whole flow works against your instance with wrangler dev, which proxies the remote AI Search binding. The output below is from the generate a response option.

Start a local development server:

npx wrangler dev

First, index the sample document by visiting /setup:

curl http://localhost:8787/setup

The first /setup can take a minute or two while the document indexes. When it finishes, you get back the item key and a completed status:

{ "key": "caching.md", "status": "completed" }

Then query the instance:

curl "http://localhost:8787/?q=How+does+caching+work%3F"

A working integration returns generated text grounded in your document, along with a sources array referencing the file it retrieved (fields trimmed):

{
    "text": "Cloudflare caches static assets at the edge...",
    "sources": [{ "sourceType": "url", "url": "caching.md" }]
}

If sources comes back empty, the document has not finished indexing yet. Run /setup again, then retry the query.

7. Deploy

Log in with your Cloudflare account:

npx wrangler login

Deploy your Worker to make it accessible on the Internet:

npx wrangler deploy

Considerations

  • The chat model is text-only. File and image message parts are not supported.
  • Generation options such as temperature and maxOutputTokens are passed through to the instance's generation model. maxOutputTokens truncates the response and sets finishReason to "length". If a model ignores an option, the result's warnings array stays empty, so an unsupported option fails silently.
  • AI Search uses the generation model configured on the instance by default. Pass instance.chat({ model: "..." }) to override it per request.

Next steps

Combine vector and keyword search with configurable fusion.

Instances Workers binding

Full reference for create, update, list, and delete.

Search Workers binding

Full reference for searching and chatting from a Worker.

Agents SDK

Build a stateful chat agent that searches your instance.

Was this helpful?

YesNo

On this page

AI SDK (2)·续 - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/ai-search/agent-sdks/ai-sdk/#page","headline":"AI SDK","description":"Use AI Search from the Vercel AI SDK to create an instance, index content, and generate grounded responses in a TypeScript project.","url":"https://developers.cloudflare.com/ai-search/agent-sdks/ai-sdk/","inLanguage":"en","image":"https://developers.cloudflare.com/ai-search/agent-sdks/ai-sdk/og.png?v=8f22888cce1b37ad","dateModified":"2026-10-01","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}


description: Use AI Search from LangChain to create an instance, index content, and search it with the CloudflareAISearchRetriever. title: LangChain image: https://developers.cloudflare.com/ai-search/agent-sdks/langchain/og.png?v=5d58e36f11c67f45


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/ai-search/llms.txt
Use this file to discover all available pages before exploring further.