Bring your own generation model

Last updated Aug 25, 2026 By default, AI Search uses a Workers AI model to generate responses. To use a model outside of Workers AI, use AI Search for search and pass the retrieved content to a different model for generation. This guide uses an OpenAI model.

Note

AI Search supports bringing your own models natively. You can attach provider keys through AI Gateway and select third-party models directly in your AI Search settings. The example below still works, but the recommended approach is to configure your external model through AI Gateway.

Prerequisites

  1. Sign up for a Cloudflare account ↗︎.
  2. Install Node.js ↗︎.

<details>

<summary>

Node.js version manager

</summary>

Use <a href="https://viteplus.dev/guide/env">Vite+ (<code>vp env</code>) ↗︎</a> to install and manage Node.js versions.

For supported Node.js versions, refer to the <a href="https://developers.cloudflare.com/workers/wrangler/install-and-update/#install-wrangler">Wrangler system requirements</a>.

</details>

You also need:

1. Create a Worker project

Create a new Worker project using the create-cloudflare CLI (C3). C3 ↗︎ is a command-line tool designed to help you set up and deploy new applications to Cloudflare.

Create a new project named byo-model by running:

npmyarnpnpm

npm create cloudflare@latest -- byo-model
yarn create cloudflare byo-model
pnpm create cloudflare@latest byo-model

For setup, select the following options:

  • For What would you like to start with?, choose Hello World example.
  • For Which template would you like to use?, choose Worker only.
  • For Which language do you want to use?, choose TypeScript.
  • For Do you want to use git for version control?, choose Yes.
  • For Do you want to add an AGENTS.md file to help AI coding tools understand Cloudflare APIs?, choose Yes.
  • For Do you want to deploy your application?, choose No (we will be making some changes before deploying).

Go to your application directory:

cd byo-model

2. Install the AI SDK and OpenAI provider

Install the AI SDK ↗︎ and its OpenAI provider:

npmyarnpnpmbun

npm i ai @ai-sdk/openai
yarn add ai @ai-sdk/openai
pnpm add ai @ai-sdk/openai
bun add ai @ai-sdk/openai

3. Bind your Worker and set your API key

Add the AI Search binding to your Wrangler configuration file:

{
  "$schema": "./node_modules/wrangler/config-schema.json",
  "ai_search_namespaces": [
    {
      "binding": "AI_SEARCH",
      "namespace": "default",
      "remote": true
    }
  ]
}
[[ai_search_namespaces]]
binding = "AI_SEARCH"
namespace = "default"
remote = true

Store your OpenAI API key as a secret:

npx wrangler secret put OPENAI_API_KEY

For local development, add the key to a .dev.vars file in your project root instead:

.dev.varstxt

OPENAI_API_KEY="<YOUR_OPENAI_API_KEY>"

4. Add the code

Update src/index.ts. This Worker searches your instance, formats the retrieved chunks, and passes them to OpenAI to generate an answer. Replace my-instance with the name of your instance.

src/index.jsjs

import { createOpenAI } from "@ai-sdk/openai";
import { generateText } from "ai";

export default {
    async fetch(request, env) {
        const url = new URL(request.url);
        const userQuery = url.searchParams.get("query") ?? "What is Cloudflare?";

        // Search for documents in AI Search.
        const searchResult = await env.AI_SEARCH.get("my-instance").search({
            messages: [{ role: "user", content: userQuery }],
        });

        if (searchResult.chunks.length === 0) {
            return Response.json({ text: `No data found for query "${userQuery}"` });
        }

        // Join the retrieved chunks into a single string.
        const chunks = searchResult.chunks
            .map((chunk) => `<file name="${chunk.item.key}">${chunk.text}</file>`)
            .join("\n\n");

        // Send the query and retrieved content to OpenAI for the answer.
        const openai = createOpenAI({ apiKey: env.OPENAI_API_KEY });
        const generateResult = await generateText({
            model: openai("gpt-4o-mini"),
            messages: [
                {
                    role: "system",
                    content:
                        "You are a helpful assistant. Answer the user question using the provided files.",
                },
                { role: "user", content: chunks },
                { role: "user", content: userQuery },
            ],
        });

        return Response.json({ text: generateResult.text });
    },
};

src/index.tsts

import { createOpenAI } from "@ai-sdk/openai";
import { generateText } from "ai";

export interface Env {
    AI_SEARCH: AiSearchNamespace;
    OPENAI_API_KEY: string;
}

export default {
    async fetch(request, env): Promise<Response> {
        const url = new URL(request.url);
        const userQuery = url.searchParams.get("query") ?? "What is Cloudflare?";

        // Search for documents in AI Search.
        const searchResult = await env.AI_SEARCH.get("my-instance").search({
            messages: [{ role: "user", content: userQuery }],
        });

        if (searchResult.chunks.length === 0) {
            return Response.json({ text: `No data found for query "${userQuery}"` });
        }

        // Join the retrieved chunks into a single string.
        const chunks = searchResult.chunks
            .map((chunk) => `<file name="${chunk.item.key}">${chunk.text}</file>`)
            .join("\n\n");

        // Send the query and retrieved content to OpenAI for the answer.
        const openai = createOpenAI({ apiKey: env.OPENAI_API_KEY });
        const generateResult = await generateText({
            model: openai("gpt-4o-mini"),
            messages: [
                {
                    role: "system",
                    content:
                        "You are a helpful assistant. Answer the user question using the provided files.",
                },
                { role: "user", content: chunks },
                { role: "user", content: userQuery },
            ],
        });

        return Response.json({ text: generateResult.text });
    },
} satisfies ExportedHandler<Env>;

5. Run and deploy

Start a local development server, then query it at /?query=your+search+terms:

npx wrangler dev

Log in with your Cloudflare account, then deploy your Worker to make it accessible on the Internet:

npx wrangler login
npx wrangler deploy

Next steps

Models

Use third-party models natively through AI Gateway.

Search Workers binding

Full reference for searching and chatting from a Worker.

Was this helpful?

YesNo

On this page

Bring your own generation model - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/ai-search/how-to/bring-your-own-generation-model/#page","headline":"Bring your own generation model","description":"Use AI Search for retrieval while generating responses with an external model like OpenAI.","url":"https://developers.cloudflare.com/ai-search/how-to/bring-your-own-generation-model/","inLanguage":"en","image":"https://developers.cloudflare.com/ai-search/how-to/bring-your-own-generation-model/og.png?v=cb697903cd55b5fa","dateModified":"2026-08-25","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/"},"keywords":["AI"]}


description: Display source citations alongside AI-generated answers. title: Show source citations in responses image: https://developers.cloudflare.com/ai-search/how-to/chunk-citations/og.png?v=9c6da9684828a4cb


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.