Skip to main content
Workers AI provides access to AI models directly from Workers. Use the API to run inference on text, images, and embeddings.

Overview

Access the Workers AI API:

Run inference

Execute AI models on-demand.

Text generation

Generate text using large language models.
string
required
The AI model identifier (e.g., ‘@cf/meta/llama-2-7b-chat-int8’)
string
required
Your Cloudflare account ID
string
required
The input text prompt
number
Maximum number of tokens to generate
number
Controls randomness (0.0 to 1.0, higher = more random)
number
Nucleus sampling parameter
number
Top-k sampling parameter
string
The generated text response
object
Token usage statistics
number
Number of tokens in the prompt
number
Number of tokens in the completion
number
Total number of tokens used

Chat completion

Generate chat responses using a messages format.
array
required
Array of message objects
string
required
Message role: ‘system’, ‘user’, or ‘assistant’
string
required
Message content

Text embeddings

Generate vector embeddings for text.
string
required
The embedding model (e.g., ‘@cf/baai/bge-base-en-v1.5’)
string | array
required
Text string or array of strings to embed
array
Array of embedding vectors
array
Shape of the embedding array [count, dimensions]

Text classification

Classify text into categories.
string
Classification label
number
Confidence score (0-1)

Translation

Translate text between languages.
string
required
Text to translate
string
Source language code (e.g., ‘en’)
string
required
Target language code (e.g., ‘es’)

Summarization

Summarize long text.
string
required
Text to summarize
number
Maximum summary length in tokens

Text-to-image

Generate images from text descriptions.
string
required
Text description of the image to generate
string
Things to avoid in the image
number
Number of diffusion steps (higher = better quality, slower)
number
How closely to follow the prompt (1-20)
number
Image width in pixels
number
Image height in pixels

Image classification

Classify images into categories.
array
required
Image data as array of integers (8-bit unsigned)

Automatic speech recognition

Transcribe audio to text.
array
required
Audio data as array of integers
string
Source language of the audio
string
The transcribed text
number
Number of words transcribed

Models

Browse and discover available AI models.

List models

Retrieve all available AI models.
string
required
Your Cloudflare account ID
string
Model identifier
string
Model description
string
Task type (e.g., ‘text-generation’, ‘text-embeddings’)

Using Workers AI in Workers

Bind Workers AI to your Worker:
Configure the binding when deploying:

Best practices

  1. Model selection: Choose the right model for your task (size vs. performance)
  2. Caching: Cache embeddings and frequently used results
  3. Rate limiting: Implement rate limiting for user-facing applications
  4. Error handling: Handle model errors gracefully with fallbacks
  5. Streaming: Use streaming for long-running text generation
  6. Context length: Be mindful of model context limits