Workers AI
Use AI Gateway for analytics, caching, and security on requests to Workers AI.
To interact with a REST API, update the URL used for your request:
- Previous:
https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/run/{model_id}
- New:
https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/workers-ai/{model_id}
For these parameters:
{account_id}
is your Cloudflare account ID.{gateway_id}
refers to the name of your existing AI Gateway.{model_id}
refers to the model ID of the Workers AI model.
First, generate an API token with Workers AI Read
access and use it in your request.
Workers AI supports OpenAI compatible endpoints for text generation (/v1/chat/completions
) and text embedding models (/v1/embeddings
). This allows you to use the same code as you would for your OpenAI commands, but swap in Workers AI easily.
To include an AI Gateway within your Worker, add the gateway as an object in your Workers AI request.
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)- Controls whether the request should skip the cache.
cacheTtl
number- Controls the Cache TTL.