createAI: explicit way to call without a gateway, and an opt-out for automatic stream resume
Hi, thanks for createAI. Context: we build a library for agents that handle sensitive conversations, so we care about exactly which path a prompt takes. This follows the closing note on #2569 ("Open a new issue if you need them") and relates to the automatic resume added in #2589. These are options for you to consider, not demands.
1. No gateway. With [email protected]:
- READ:
resolveOptionsreturnsgateway: { ...gateway, id: gateway.id ?? "default", ... }, andrunOptionspasses that gateway tobinding.run. I found no value ofgateway/idthat means "no gateway". - READ (docs,
models.md): "thedefaultgateway is created for you on first use";id"Defaults to"default", created on first use". - So moving from
binding.run(model, body)tocreateAIchanges where the request goes, and a caller who wants the plain Workers AI call cannot say so.collectLog: falseexists, but it configures a gateway the caller did not ask for. - UNVERIFIED: whether an auto-created
defaultgateway logs by default (the docs describe onlycollectLog).
Possible shapes: createAI({ binding, gateway: false }), or id: null; models that need a gateway (catalog ids, dynamic/...) would error clearly.
2. Stream resume.
- READ:
withResumewraps responses withcontent-type: text/event-streamand acf-aig-run-idheader; the resume callback isbinding.fetch(resumeUrl(...), { method: "GET", signal })against.../ai-gateway/gateways/{id}/run/{runId}/resume?from=n, with no options or headers. - READ (docs): "There is nothing to configure" for stream resume; resume applies to catalog ids and
dynamic/<route>streams, while Workers AI@cf/streams "have no run id, so they are not resumed". - Some callers want a single auditable request per prompt and would prefer to turn the second request off. Possible shape:
createAI({ binding, streamResume: false }).
Repro (synthetic; READ, not RUN)
const received = [];
const binding = { run: async (model, input, options) => { received.push(options); /* return a Response */ } };
const ai = createAI({ binding });
await generateText({ model: ai("@cf/meta/llama-3.1-8b-instruct"), prompt: "synthetic" });
// received[0].gateway is expected to be { id: "default" } (per resolveOptions)
For resume, use a catalog or dynamic model (e.g. anthropic/... or dynamic/<route>) that returns cf-aig-run-id; @cf/ models are not resumed.
If a "no gateway" mode is out of scope, a docs note on what the default gateway logs would also help.
Thanks for the detailed report. I triaged it and I'm closing it as not planned.
Why: createAI routes every call through an AI Gateway by design (packages/agents/src/models/core/settings.ts, resolveOptions defaults id to "default"). A no-gateway mode or a stream-resume opt-out would add API surface that few users need. Callers who want the plain Workers AI path can still call binding.run directly, and collectLog: false controls whether the gateway stores the request.
Next: a maintainer can reopen this if there is new evidence of wider demand.