Clatter LabsHelp Center

How do I cancel a Model API request in JavaScript?

How-toUpdated

Pass signal in the request options and call abort() when you no longer need the answer (for example, the user closed the page).

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.gateway.clatterlabs.xyz/v1",
  apiKey: process.env.CLATTER_API_KEY,
  defaultHeaders: { "X-Clatter-Account": process.env.CLATTER_ACCOUNT_ID },
});

const ac = new AbortController();
setTimeout(() => ac.abort(), 5000);
try {
  const stream = await client.chat.completions.create(
    { model: "supercosmos-heavy", messages, stream: true },
    { signal: ac.signal },
  );
  for await (const c of stream) process.stdout.write(c.choices[0]?.delta?.content ?? "");
} catch (e) {
  if (e.name === "AbortError" || e instanceof OpenAI.APIUserAbortError) console.log("\ncancelled");
  else throw e;
}

The docs don't promise how much of an aborted request is billed. Check the call in Console › Logs if you need the exact charge.

Every JavaScript example uses the same client (base URL, key and the X-Clatter-Account header); see Set up the JavaScript SDK. Full parameter list: Create a chat completion.

Still need help?#

We usually reply within the first hour, and always within 24 hours. A full support team is with you.

Related articles

Still need help?

Tried the steps above and still stuck? Email us. We usually reply within the first hour, and always within 24 hours. A full support team is with you.

Infrastructure questions, or permanent deletion of your data
infrastructure@clatterlabs.xyz
For deleting your whole account or the data of one service.