Clatter LabsHelp Center

How do I handle Model API errors in JavaScript?

How-toUpdated

The SDK throws OpenAI.APIError subclasses with status, code and headers.

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 },
});

try {
  await client.chat.completions.create({ model: "supercosmos-flash", messages: [{ role: "user", content: "Hi" }] });
} catch (e) {
  if (e instanceof OpenAI.APIError) {
    console.log(e.status, e.code, e.message, e.headers?.["x-request-id"]);
    if (e.status === 429) console.log("retry after", e.headers?.["retry-after"], "s");
    if (e.status === 402) console.log("out of credits: top up in Console › Billing › Credits");
    if (e.status === 410) console.log("retired model id: switch to a supercosmos id");
  } else throw e;
}

See every code in What does a Model API error look like?.

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.

Good to know#

  • New accounts get $5 of free credit; top up from $1 to $800 per purchase in Console › Billing › Credits. Billing
  • Each key has its own limit: 10, 30 (default), 60 or 120 requests per minute. Rate limits
  • Every call shows in Console › Logs and Usage as Model API, with its price. See what you spent

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.