Workers AI
Use AI Gateway for analytics, caching, and security on requests to Workers AI. Workers AI integrates seamlessly with AI Gateway, allowing you to execute AI inference via API requests or through an environment binding for Workers scripts. The binding simplifies the process by routing requests through your AI Gateway with minimal setup.
To interact with a REST API, update the URL used for your request:
- Previous:
- New:
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.
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.
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.