agents icon indicating copy to clipboard operation
agents copied to clipboard

createAI: explicit way to call without a gateway, and an opt-out for automatic stream resume

Open MRCORD opened this issue 1 month ago • 1 comments

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: resolveOptions returns gateway: { ...gateway, id: gateway.id ?? "default", ... }, and runOptions passes that gateway to binding.run. I found no value of gateway / id that means "no gateway".
  • READ (docs, models.md): "the default gateway is created for you on first use"; id "Defaults to "default", created on first use".
  • So moving from binding.run(model, body) to createAI changes where the request goes, and a caller who wants the plain Workers AI call cannot say so. collectLog: false exists, but it configures a gateway the caller did not ask for.
  • UNVERIFIED: whether an auto-created default gateway logs by default (the docs describe only collectLog).

Possible shapes: createAI({ binding, gateway: false }), or id: null; models that need a gateway (catalog ids, dynamic/...) would error clearly.

2. Stream resume.

  • READ: withResume wraps responses with content-type: text/event-stream and a cf-aig-run-id header; the resume callback is binding.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.

MRCORD avatar Nov 02 '26 14:11 MRCORD

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.

github-actions[bot] avatar Nov 03 '26 15:11 github-actions[bot]