Skip to main content

Abort Signal

Model calls can be expensive and long running. In interactive user interfaces such as chatbots, users may want to cancel interactions. You can implement the backend of this and save costs by using the standard AbortController API.


const abortController = new AbortController();

prompt: "Write a short story about a robot learning to love:\n\n",
run: { abortSignal: abortController.signal },
.then(/* ... */)
.catch((error) => {
if (error instanceof AbortError) {
console.log("the run was aborted");

abortController.abort(); // aborts the running generate text call