Skip to content
GitHubXDiscordRSS

AiGateway

Learn how to create and configure Cloudflare AI Gateway using Alchemy to route and manage AI requests.

The AiGateway resource lets you create and manage Cloudflare AI Gateway configurations for accessing AI models through Cloudflare Workers.

Create a basic AI Gateway with default settings:

import { AiGateway } from "alchemy/cloudflare";
const gateway = await AiGateway("my-ai-gateway", {
name: "my-ai-gateway",
});

Configure an AI Gateway with authentication and rate limiting:

import { AiGateway } from "alchemy/cloudflare";
const secureGateway = await AiGateway("secure-gateway", {
name: "secure-gateway",
authentication: true,
rateLimitingInterval: 60,
rateLimitingLimit: 100,
rateLimitingTechnique: "sliding",
});

Create an AI Gateway with logging and logpush enabled:

import { AiGateway } from "alchemy/cloudflare";
const loggingGateway = await AiGateway("logging-gateway", {
name: "logging-gateway",
collectLogs: true,
logpush: true,
logpushPublicKey: "mypublickey...",
});

Use the AI Gateway in a Cloudflare Worker:

import { Worker, Ai, AiGateway } from "alchemy/cloudflare";
const gateway = await AiGateway("my-gateway", {
name: "my-gateway",
});
await Worker("my-worker", {
name: "my-worker",
script: "console.log('Hello, world!')",
bindings: {
AI: Ai(),
GATEWAY_ID: gateway.id,
},
});

Here’s a simple example showing how to use AI Gateway in a Cloudflare Worker:

import { Worker, Ai, AiGateway } from "alchemy/cloudflare";
const aiGateway = await AiGateway("chat-gateway", {
rateLimitingInterval: 60,
rateLimitingLimit: 100,
collectLogs: true,
});
await Worker("chat-worker", {
entrypoint: "./src/worker.ts",
bindings: {
AI: Ai(),
GATEWAY_ID: aiGateway.id,
},
});
src/worker.ts
export default {
async fetch(request, env) {
const { prompt } = await request.json();
const response = await env.AI.run(
"@cf/meta/llama-3.1-8b-instruct-fast",
{
prompt,
},
{
gateway: {
id: env.GATEWAY_ID,
},
},
);
return Response.json({ response: response.response });
},
};