Cloudflare Workers AI
Use the prefix and interface for your task:
| Interface | Prefix | Native Workers AI task | Adapter |
|---|---|---|---|
Agent | cloudflare: | Chat, including supported vision inputs | CloudflareChatModel |
Agent | cloudflare-decisions: | Clef decisions | CloudflareDecisionsModel |
Embedder | cloudflare: | Text embeddings | CloudflareEmbeddingModel |
ImageGenerator | cloudflare: | Image generation and editing | CloudflareImageGenerationModel |
Any full Workers AI model ID is accepted; the autocomplete list is representative, and constructing a model makes no catalog request.
These models run on Cloudflare. Third-party model routing through AI Gateway, speech/realtime, and standalone reranking, classification, translation and image-to-text APIs are outside these adapters.
For chat and embeddings, install pydantic-ai-slim[openai]. Clef and native image generation need
only pydantic-ai-slim; importing their adapters does not import the OpenAI SDK.
Create a Cloudflare API token with Workers AI access and set the token and account ID:
export CLOUDFLARE_API_TOKEN='your-api-token'
export CLOUDFLARE_ACCOUNT_ID='your-account-id'
from pydantic_ai import Agent
agent = Agent('cloudflare:@cf/meta/llama-4-scout-17b-16e-instruct')
result = agent.run_sync('Explain how a shipping delay affects an order.')
print(result.output)
Cloudflare’s Chat Completions endpoint
supports most Workers AI text-generation models. Tool calling, vision and structured-output behavior
depend on the selected model. Profiles are conservative for unlisted models; pass an explicit
profile when enabling additional capabilities you have verified.
With Agent, cloudflare: always selects the chat adapter, and cloudflare-decisions: the
decision adapter. Agent does not validate catalog tasks; embedding and image IDs
require Embedder and ImageGenerator respectively.
from pydantic_ai import Embedder
embedder = Embedder('cloudflare:@cf/baai/bge-small-en-v1.5')
result = embedder.embed_query_sync('How can I track my order?')
print(len(result.embeddings[0]))
Embedder uses the OpenAI-compatible embeddings endpoint with the same account credentials.
Model-specific dimension limits are enforced by Workers AI.
from pydantic_ai import ImageGenerator
generator = ImageGenerator('cloudflare:@cf/black-forest-labs/flux-2-klein-4b')
result = generator.generate_sync(
'A watercolor illustration of a delivery bicycle.',
settings={'dimensions': (1024, 1024)},
)
FLUX.2 uses multipart requests, including for text-only generation, and accepts up to four reference
images named by their position. Pass binary images or image URLs through images; URLs are downloaded
and sent inline. Cloudflare’s reference-image size limits apply; the adapter does not resize inputs.
Other JSON image models, including Stable Diffusion XL, accept text prompts through this adapter.
Provider-side uploaded file IDs are unsupported.
CloudflareImageGenerationSettings
exposes native steps, seed, guidance and negative-prompt controls. Their
availability and ranges depend on the selected model. dimensions maps to width/height except on
FLUX.1 Schnell, which has no size control and rejects geometry settings before making a request.
aspect_ratio maps to integer dimensions preserving the requested ratio with the longest edge at most
1024 pixels; model-specific size limits are enforced by Workers AI. Explicit dimensions are forwarded unchanged.
The image models accept the following Cloudflare-specific settings:
cloudflare_steps— the number of sampling steps.cloudflare_seed— a seed for reproducible generation.cloudflare_guidance— the guidance scale.cloudflare_negative_prompt— text describing what to avoid.
CloudflareDecisionsModel runs agents on
Clef and
Clef Flash. These are
decision models: they answer yes/no, pick-one, and rubric questions with probabilities.
Use an agent’s output_type to define those questions.
Select either model with the cloudflare-decisions: prefix:
from pydantic_ai import Agent
agent = Agent(
'cloudflare-decisions:@cf/cloudflare/clef-flash',
output_type=bool,
instructions='Does this report describe an urgent production incident?',
)
result = agent.run_sync('All production API requests return 503. Please help urgently.')
print(result.output)
The Decision models guide explains supported output types, tool routing, confidence, and fallback to a language model.
Pass a CloudflareProvider to set the token,
account ID, or HTTP client explicitly:
from pydantic_ai.models.cloudflare_decisions import CloudflareDecisionsModel
from pydantic_ai.providers.cloudflare import CloudflareProvider
model = CloudflareDecisionsModel(
'@cf/cloudflare/clef',
provider=CloudflareProvider(api_key='your-api-token', account_id='your-account-id'),
)
For connection-based configuration, put the account in base_url and pass the token as api_key.
An explicit base_url takes precedence over account_id and its environment variable:
from pydantic_ai.models.cloudflare_decisions import CloudflareDecisionsModel
from pydantic_ai.providers.cloudflare import CloudflareProvider
model = CloudflareDecisionsModel(
'@cf/cloudflare/clef-flash',
provider=CloudflareProvider(
api_key='your-api-token',
base_url='https://api.cloudflare.com/client/v4/accounts/your-account-id',
),
)
The provider also accepts a custom httpx2.AsyncClient through http_client.
Both models accept up to four PNG, JPEG, or WebP images. Pass
BinaryContent or ImageUrl
in the prompt; image URLs are downloaded and sent inline. Cloudflare’s
image limits apply to the decoded images.
from pathlib import Path
from typing import Literal
from pydantic_ai import Agent, BinaryContent
agent = Agent(
'cloudflare-decisions:@cf/cloudflare/clef-flash',
output_type=Literal['kiwi', 'apple'],
instructions='Which fruit is shown?',
)
result = agent.run_sync(
['Identify this fruit.', BinaryContent(data=Path('fruit.jpg').read_bytes(), media_type='image/jpeg')]
)
print(result.output)
DecisionModelSettings provides the shared decision
thresholds. timeout, extra_headers, and extra_body are forwarded to Workers AI. Generic sampling
settings, including temperature, are ignored because Clef rejects them.
Field aliases and route names are mapped to Cloudflare-compatible question IDs when needed; returned answers retain their original names.
The model profile sets these context windows:
| Model ID | Context window |
|---|---|
@cf/cloudflare/clef | 65,536 tokens |
@cf/cloudflare/clef-flash | 24,576 tokens |
Both profiles allow up to 255 options in a pick-one question and 10 levels in a rubric. Workers AI accepts
at most 64 questions and four images per request. Requests over those transport limits raise
ModelAPIError before being sent, so a
FallbackModel can take over.