Workers AI
Use AI Gateway for analytics, caching, and security on requests to Workers AI.
Use the REST API to call Workers AI models. Workers AI models use the @cf/ prefix in the model name and require the cf-aig-gateway-id header to specify which gateway to route through.
curl -X POST "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions" \ --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ --header "cf-aig-gateway-id: my-gateway" \ --header "Content-Type: application/json" \ --data '{ "model": "@cf/moonshotai/kimi-k2.6", "messages": [ { "role": "user", "content": "What is Cloudflare?" } ] }'You can integrate Workers AI with AI Gateway using an environment binding. To include an AI Gateway within your Worker, add the gateway as an object in your Workers AI request.
export default { async fetch(request, env) { const response = await env.AI.run( "@cf/meta/llama-3.1-8b-instruct", { prompt: "Why should you use Cloudflare for your AI inference?", }, { gateway: { id: "{gateway_id}", skipCache: false, cacheTtl: 3360, }, }, ); return new Response(JSON.stringify(response)); },};export interface Env { AI: Ai;}
export default { async fetch(request: Request, env: Env): Promise<Response> { const response = await env.AI.run( "@cf/meta/llama-3.1-8b-instruct", { prompt: "Why should you use Cloudflare for your AI inference?", }, { gateway: { id: "{gateway_id}", skipCache: false, cacheTtl: 3360, }, }, ); return new Response(JSON.stringify(response)); },} satisfies ExportedHandler<Env>;For a detailed step-by-step guide on integrating Workers AI with AI Gateway using a binding, refer to Integrations in AI Gateway.
Workers AI supports the following parameters for AI gateways:
idstring- Name of your existing AI Gateway. Must be in the same account as your Worker.
skipCacheboolean(default: false)- Controls whether the request should skip the cache.
cacheTtlnumber- Controls the Cache TTL.