
# Cloudflare Workers AI

[workers-ai-provider](https://github.com/cloudflare/ai/tree/main/packages/workers-ai-provider) is a community provider that allows you to use Cloudflare's [Workers AI](https://ai.cloudflare.com/) models with the AI SDK.

## Setup

The Cloudflare Workers AI provider is available in the `workers-ai-provider` module. You can install it with:

<Tabs items={['pnpm', 'npm', 'yarn']}>
  <Tab>
    <Snippet text="pnpm add workers-ai-provider" />
  </Tab>
  <Tab>
    <Snippet text="npm install workers-ai-provider" />
  </Tab>
  <Tab>
    <Snippet text="yarn add workers-ai-provider" />
  </Tab>
</Tabs>

Then, setup an AI binding in your Cloudflare Workers project `wrangler.toml` file:

```bash filename="wrangler.toml"
[ai]
binding = "AI"
```

## Provider Instance

To create a `workersai` provider instance, use the `createWorkersAI` function, passing in the AI binding as an option:

```typescript
import { createWorkersAI } from 'workers-ai-provider';

const workersai = createWorkersAI({ binding: env.AI });
```

## Language Models

To create a model instance, call the provider instance and specify the model you would like to use as the first argument. You can also pass additional settings in the second argument:

```typescript highlight="4-7"
import { createWorkersAI } from 'workers-ai-provider';

const workersai = createWorkersAI({ binding: env.AI });
const model = workersai('@cf/meta/llama-3.1-8b-instruct', {
  // additional settings
  safePrompt: true,
});
```

You can use the following optional settings to customize:

- **safePrompt** _boolean_

  Whether to inject a safety prompt before all conversations. Defaults to `false`

### Examples

You can use Cloudflare Workers AI language models to generate text with the **`generateText`** or **`streamText`** function:

#### `generateText`

```typescript
import { createWorkersAI } from 'workers-ai-provider';
import { generateText } from 'ai';

type Env = {
  AI: Ai;
};

export default {
  async fetch(_: Request, env: Env) {
    const workersai = createWorkersAI({ binding: env.AI });
    const result = await generateText({
      model: workersai('@cf/meta/llama-2-7b-chat-int8'),
      prompt: 'Write a 50-word essay about hello world.',
    });

    return new Response(result.text);
  },
};
```

#### `streamText`

```typescript
import { createWorkersAI } from 'workers-ai-provider';
import { streamText } from 'ai';

type Env = {
  AI: Ai;
};

export default {
  async fetch(_: Request, env: Env) {
    const workersai = createWorkersAI({ binding: env.AI });
    const result = streamText({
      model: workersai('@cf/meta/llama-2-7b-chat-int8'),
      prompt: 'Write a 50-word essay about hello world.',
    });

    return result.toTextStreamResponse({
      headers: {
        // add these headers to ensure that the
        // response is chunked and streamed
        'Content-Type': 'text/x-unknown',
        'content-encoding': 'identity',
        'transfer-encoding': 'chunked',
      },
    });
  },
};
```

#### `generateObject`

Some Cloudflare Workers AI language models can also be used with the `generateObject` function:

```typescript
import { createWorkersAI } from 'workers-ai-provider';
import { generateObject } from 'ai';
import { z } from 'zod';

type Env = {
  AI: Ai;
};

export default {
  async fetch(_: Request, env: Env) {
    const workersai = createWorkersAI({ binding: env.AI });
    const result = await generateObject({
      model: workersai('@cf/meta/llama-3.1-8b-instruct'),
      prompt: 'Generate a Lasagna recipe',
      schema: z.object({
        recipe: z.object({
          ingredients: z.array(z.string()),
          description: z.string(),
        }),
      }),
    });

    return Response.json(result.object);
  },
};
```


## Navigation

- [Writing a Custom Provider](/v4/providers/community-providers/custom-providers)
- [Qwen](/v4/providers/community-providers/qwen)
- [Ollama](/v4/providers/community-providers/ollama)
- [Chrome AI](/v4/providers/community-providers/chrome-ai)
- [FriendliAI](/v4/providers/community-providers/friendliai)
- [Portkey](/v4/providers/community-providers/portkey)
- [Cloudflare Workers AI](/v4/providers/community-providers/cloudflare-workers-ai)
- [Cloudflare AI Gateway](/v4/providers/community-providers/cloudflare-ai-gateway)
- [OpenRouter](/v4/providers/community-providers/openrouter)
- [Azure AI](/v4/providers/community-providers/azure-ai)
- [Crosshatch](/v4/providers/community-providers/crosshatch)
- [Requesty](/v4/providers/community-providers/requesty)
- [Mixedbread](/v4/providers/community-providers/mixedbread)
- [Voyage AI](/v4/providers/community-providers/voyage-ai)
- [Hyperbolic](/v4/providers/community-providers/hyperbolic)
- [Mem0](/v4/providers/community-providers/mem0)
- [Letta](/v4/providers/community-providers/letta)
- [LLamaCpp](/v4/providers/community-providers/llama-cpp)
- [Anthropic Vertex](/v4/providers/community-providers/anthropic-vertex-ai)
- [Spark](/v4/providers/community-providers/spark)
- [Inflection AI](/v4/providers/community-providers/inflection-ai)
- [LangDB](/v4/providers/community-providers/langdb)
- [Zhipu AI](/v4/providers/community-providers/zhipu)
- [SambaNova](/v4/providers/community-providers/sambanova)
- [Dify](/v4/providers/community-providers/dify)
- [Sarvam](/v4/providers/community-providers/sarvam)


[Full Sitemap](/sitemap.md)
