Workers AI foundations
Choose and run a Workers AI model
Bind Workers AI, select a current model for the task, validate input, and inspect the typed result instead of copying an old model name blindly.
8 minute lesson
Workers AI runs supported models through an AI binding or REST API. Inside a Worker, configure AI and call env.AI.run(model, input).
Choose the model from the current catalog by task, language, context, output format, latency, and cost. Model identifiers and capabilities change, so keep them configurable and test the actual deployed model. Validate input size and never send secrets by default.
Run one short classification and record model ID, input, output, duration, and current usage units.
Start with one bounded input and inspect the response shape:
const result = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {
messages: [{ role: 'user', content: 'Summarize this alert in one sentence.' }]
})
Pin the model name in configuration, bound input length, and handle provider errors. Save a few representative inputs and expected qualities before changing models. A response returning 200 does not prove the answer is useful or safe.
Lesson completed