Skip to content
TypeScript
10.1 KB
Raw Blame
---
title: Effect AI
description: Wire Effect's LanguageModel and Chat services into a Cloudflare Worker — read API keys with effect/Config, provide the model layer to your handler, plug in persistence.
sidebar:
  order: 5
---

Effect's [`@effect/ai`](https://effect.website/docs/ai/introduction)
package gives you a single `LanguageModel.LanguageModel` service that
every model provider (OpenAI, Anthropic, Workers AI, …) ships as a
`Layer`. Once that layer is in scope, the rest of the surface
(`generateText`, `streamText`, `Toolkit`, `Chat`, …) is
provider-agnostic.

This guide is about the *Alchemy* side of that picture: how to build
the `LanguageModel` layer inside a Worker or Lambda
([Runtime](/infrastructure-as-effects/runtime))
(`Cloudflare.Worker`, `AWS.Lambda.Function`), where to read the API
key from, and how to plug `Chat.Persistence` into a backing store
that matches the platform. For the AI calls themselves —
`LanguageModel.generateText`, `streamText`, `Toolkit`, structured
outputs — refer to
[Effect's AI documentation](https://effect.website/docs/ai/introduction).

## The pattern

Every Effect AI provider is two stacked layers:

1. A **client** Layer that holds the API key and HTTP transport
   (`OpenAiClient.layer({ apiKey })`, `AnthropicClient.layer({ apiKey })`, …).
2. A **language model** Layer that picks the model and any defaults
   (`OpenAiLanguageModel.layer({ model: "gpt-4o-mini" })`, …).

You build that stack once in the Runtime's **Construction phase** and
`Effect.provide(...)` it on the handler:

```typescript
Effect.gen(function* () {
  // 1. Construct: build the LanguageModel layer (deploy bindings happen here).
  const languageModel = ...;

  return {
    // 2. Exec: per-request handler with the model available.
    fetch: Effect.gen(function* () {
      const response = yield* LanguageModel.generateText({ prompt });
      return HttpServerResponse.json({ text: response.text });
    }).pipe(Effect.provide(languageModel)),
  };
});
```

`fetch` is just one example — the same `Effect.provide(languageModel)`
pattern works in a Lambda handler, an HTTP API endpoint, an RPC
procedure, a Workflow, or any other Effect.

## Read the API key with `Config.Redacted`

Every upstream provider's client Layer (`OpenAiClient.layer`,
`AnthropicClient.layer`, …) takes an `apiKey: Redacted<string>`.
[`Config.Redacted`](/environments/secrets) resolved in the Runtime's
Construction phase gives you exactly that `Redacted<string>` — and Alchemy
automatically binds the value as a `secret_text` binding
(Cloudflare) or environment variable (Lambda) at deploy time:

```typescript
import * as Config from "effect/Config";

// constructor — resolves the value AND records the binding
const apiKey = yield* Config.Redacted("OPENAI_API_KEY");
```

`Config.Redacted("OPENAI_API_KEY")` reads `OPENAI_API_KEY` from your
`ConfigProvider` (e.g. `.env`) at deploy time, records the binding,
and resolves from that binding again at runtime — one line, both
phases. See [Concepts › Secrets and Config](/environments/secrets) for
the Construction/Runtime split, `.env`, and transformations.

Because the value is in scope right in the constructor, the model Layer can be
built eagerly — no `Layer.unwrap` needed:

```typescript
import * as Layer from "effect/Layer";

const languageModel = OpenAiLanguageModel.layer({
  model: "gpt-4o-mini",
}).pipe(
  Layer.provide(OpenAiClient.layer({ apiKey })),
  Layer.provide(FetchHttpClient.layer),
);
```

## In a Cloudflare Worker

`FetchHttpClient.layer` plugs the runtime `fetch` API into the
Effect HTTP client that every provider's client Layer depends on:

```typescript
// src/Worker.ts
import * as Cloudflare from "@alchemy.run/cloudflare";
import * as Config from "effect/Config";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import { LanguageModel } from "effect/ai";
import * as FetchHttpClient from "effect/http/FetchHttpClient";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import { OpenAiClient, OpenAiLanguageModel } from "@effect/ai-openai";

export default Cloudflare.Worker(
  "Worker",
  { main: import.meta.url },
  Effect.gen(function* () {
    const apiKey = yield* Config.Redacted("OPENAI_API_KEY");

    const languageModel = OpenAiLanguageModel.layer({
      model: "gpt-4o-mini",
    }).pipe(
      Layer.provide(OpenAiClient.layer({ apiKey })),
      Layer.provide(FetchHttpClient.layer),
    );

    return {
      fetch: Effect.gen(function* () {
        const response = yield* LanguageModel.generateText({
          prompt: "Say hello.",
        }).pipe(Effect.orDie);
        return yield* HttpServerResponse.json({ text: response.text });
      }).pipe(Effect.provide(languageModel)),
    };
  }),
);
```

## Persisting chats

For multi-turn conversations, Effect's `Chat` service holds the
turn history and exposes the same `generateText` / `streamText`
surface. `Chat.layerPersisted` wraps it with a
`Chat.Persistence` interface that needs a `BackingPersistence`
Layer underneath:

```typescript
import { Chat } from "effect/ai";
import * as Persistence from "effect/persistence/Persistence";

// inside your handler:
const chat = yield* (yield* Chat.Persistence).getOrCreate("session-id");
yield* chat.generateText({ prompt: "Hello again." });
```

The `BackingPersistence` Layer is what makes the chat history
durable. Pick the one that matches your platform:

| Platform | Backing |
| --- | --- |
| Cloudflare Worker + Durable Object | `Cloudflare.AI.DurableObjectChatPersistence` — bytes live in `state.storage`, one DO instance per session |
| Any platform, in-memory (tests) | `Persistence.layerBackingMemory` — lost on restart |
| Anything else (KV, R2, DynamoDB, …) | Implement `BackingPersistence` against your store |

The `Chat.Persistence` API and handler code never change — only the
backing layer does.

## Cloudflare Workers AI binding

For Workers AI models on Cloudflare, the shortest path is the native
[Workers AI binding](/cloudflare/ai/workers-ai): no API key, no HTTP
client layer, no resource to declare. `yield*
Cloudflare.Workers.AI()` in the Construction phase attaches the binding, and
`ai.model({...})` builds the `LanguageModel` Layer directly on top of
it:

```typescript
Effect.gen(function* () {
  const ai = yield* Cloudflare.Workers.AI();

  const languageModel = ai.model({
    model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast",
    parameters: { temperature: 0.7, maxTokens: 1024 },
  });

  return {
    fetch: Effect.gen(function* () {
      const response = yield* LanguageModel.generateText({
        prompt: "Say hello.",
      }).pipe(Effect.orDie);
      return yield* HttpServerResponse.json({ text: response.text });
    }).pipe(Effect.provide(languageModel)),
  };
}).pipe(Effect.provide(Cloudflare.Workers.AIBinding));
```

The [Workers AI guide](/cloudflare/ai/workers-ai) covers the rest of
the binding surface — raw `ai.run(...)` inference, listing models,
and declaring the binding on a plain async Worker's `env`.

## Cloudflare AI Gateway

To route Workers AI through an **AI Gateway** instead — caching, rate
limiting, retries, and a unified request log — `Cloudflare.AI.Gateway`
declares the gateway as a resource and
`Cloudflare.AI.QueryGateway(Gateway)` returns a typed client whose
`.model({...})` builds the same `LanguageModel` Layer shape, proxied
through the gateway.

Bind the gateway in the Construction phase and call `aiGateway.model({...})`
with a Workers AI model id. It returns a `LanguageModel` Layer
directly — no API key, no `Layer.unwrap`, since the gateway binding
handles auth and the URL:

```typescript
const aiGateway = yield* Cloudflare.AI.QueryGateway(Gateway);

const languageModel = aiGateway.model({
  model: "@cf/meta/llama-3.1-8b-instruct",
  parameters: { temperature: 0.7, maxTokens: 1024 },
});
```

That `languageModel` Layer slots into the same `Effect.provide(...)`
spot as any other provider — the rest of your handler doesn't
change. Provide `Cloudflare.AI.QueryGatewayBinding` once at the
bottom of the constructor's layer chain so the binding resolves at runtime:

```typescript
Effect.gen(function* () {
  const aiGateway = yield* Cloudflare.AI.QueryGateway(Gateway);

  const languageModel = aiGateway.model({
    model: "@cf/meta/llama-3.1-8b-instruct",
    parameters: { temperature: 0.7, maxTokens: 1024 },
  });

  return {
    fetch: Effect.gen(function* () {
      const response = yield* LanguageModel.generateText({
        prompt: "Say hello.",
      }).pipe(Effect.orDie);
      return yield* HttpServerResponse.json({ text: response.text });
    }).pipe(Effect.provide(languageModel)),
  };
}).pipe(Effect.provide(Cloudflare.AI.QueryGatewayBinding));
```

The [AI Gateway tutorial](/cloudflare/ai/ai-gateway) walks
through it end to end — declaring the resource, streaming with
`streamText`, and tuning caching, rate limits, and DLP.

## Picking a provider

Each upstream provider is its own `@effect/ai-*` package. The
shape — `*Client.layer({ apiKey })` + `*LanguageModel.layer({
model })` — is the same.

- [`@effect/ai-openai`](https://www.npmjs.com/package/@effect/ai-openai)
- [`@effect/ai-anthropic`](https://www.npmjs.com/package/@effect/ai-anthropic)
- [`@effect/ai-google`](https://www.npmjs.com/package/@effect/ai-google)
- [`@effect/ai-amazon-bedrock`](https://www.npmjs.com/package/@effect/ai-amazon-bedrock)
- `Cloudflare.Workers.AI` (the native Workers AI binding, via Alchemy itself)
- `Cloudflare.AI.Gateway` (Workers AI behind an AI Gateway, via Alchemy itself)
- [`AWS.Bedrock.LanguageModel`](/aws/ai/bedrock) (Amazon Bedrock over the
  Converse API with IAM-scoped bindings, via Alchemy itself)

## Where to go next

- [Effect AI documentation](https://effect.website/docs/ai/introduction)
  — the API surface (`generateText`, `streamText`, `Toolkit`,
  structured outputs, embeddings).
- [Run Workers AI models](/cloudflare/ai/workers-ai) — the native
  Workers AI binding, with or without the `LanguageModel` layer.
- [Add an AI Gateway](/cloudflare/ai/ai-gateway) — wire
  Workers AI behind a Cloudflare AI Gateway with caching and
  streaming.
- [Secrets & env](/cloudflare/security/secrets-env) — how `Config.Redacted`
  feeds the upstream providers' API keys at deploy and runtime.