Skip to content

AiGatewayBinding

Source: src/Cloudflare/AiGateway/AiGatewayBinding.ts

Binding service that turns an {@link AiGatewayResource} resource into a typed {@link AiGatewayClient} for Worker runtime code. Wraps the Cloudflare AI Gateway runtime binding so each operation returns an Effect tagged with {@link AiGatewayError}, exposes the raw Workers AI handle for ai.run(...), and provides a model(options) factory that produces an effect/unstable/ai LanguageModel Layer.

Bind the gateway during the Worker’s init phase, then use run or getUrl from request handlers.

const aiGateway = yield* Cloudflare.AiGatewayBinding.bind(gateway);
return {
fetch: Effect.gen(function* () {
return yield* aiGateway.run({
provider: "workers-ai",
endpoint: "@cf/meta/llama-3.1-8b-instruct",
headers: { "content-type": "application/json" },
query: { prompt: "Write a concise status update" },
});
}),
};

model(options) produces a Layer<LanguageModel, never, RuntimeContext> that translates LanguageModel.generateText / streamText calls (including tool calls and structured outputs) into ai.run(...) against the bound Workers AI model, routed through the gateway.

const aiGateway = yield* Cloudflare.AiGatewayBinding.bind(gateway);
const languageModel = aiGateway.model({
client: aiGateway,
model: "@cf/meta/llama-3.1-8b-instruct",
parameters: { temperature: 0.7, maxTokens: 1024 },
});
const response = yield* AiLanguageModel.generateText({ prompt }).pipe(
Effect.provide(languageModel),
);

Provide {@link AiGatewayBindingLive} in the worker’s runtime layer to resolve the underlying Cloudflare AI binding at request time.