Skip to content

Workers AI

Use AI Gateway for analytics, caching, and security on requests to Workers AI.

REST API

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.

Request to Workers AI Kimi model
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?"
}
]
}'

Workers binding

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.

JavaScript
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));
},
};

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:

  • id string
    • Name of your existing AI Gateway. Must be in the same account as your Worker.
  • skipCache boolean(default: false)
  • cacheTtl number