Skip to main content

@rtorcato/cf-common

Functions​

runAi()​

runAi<Name>(ai, model, inputs, options?): Promise<AiModels[Name]["postProcessedOutputs"]>

Defined in: index.ts:19

Run a Workers AI model through its binding, with input and output types picked by model name. Every model family (text generation, embeddings, image, speech, …) is typed by the AiModels map in @cloudflare/workers-types, so a wrong input shape fails at compile time.

A failed call (quota, model error, upstream outage) throws a non-exposed 502 with code AI_RUN_FAILED. The model is upstream of the client, so its details stay in logs and out of the response.

Example​

const ai = getBinding<Ai>(env, 'AI')
const { response } = await runAi(ai, '@cf/meta/llama-3.2-3b-instruct', {
prompt: 'Name one Cloudflare product.',
})

Type Parameters​

Name​

Name extends keyof AiModels

Parameters​

ai​

Ai

model​

Name

inputs​

AiModels[Name]["inputs"]

options?​

AiOptions

Returns​

Promise<AiModels[Name]["postProcessedOutputs"]>