Clatter LabsHelp Center

How do I estimate the cost of a call in JavaScript?

How-toUpdated

Read usage from the response and apply the price of the model id you called.

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 PRICES = { "supercosmos-flash": [1.6, 3.4], "supercosmos-max": [1.8, 3.8], "supercosmos-heavy": [2.0, 4.4] };

const r = await client.chat.completions.create({ model: "supercosmos-max", messages: [{ role: "user", content: "Hi" }] });
let [pin, pout] = PRICES[r.model];
if (r.usage.prompt_tokens > 262_000) pin *= 1.5; // long-context input
const cost = (r.usage.prompt_tokens * pin + r.usage.completion_tokens * pout) / 1e6;
console.log(`$${cost.toFixed(6)}`);

Billing rounds each call up to the next millionth of a dollar. The same call appears in Console › Logs with its price.

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#

  • If calls fail unexpectedly, check status.clatterlabs.com before debugging further.
  • Your key's account is responsible for your end users' text under the Abuse policy. What it means for apps
  • New accounts get $5 of free credit; top up from $1 to $800 per purchase in Console › Billing › Credits. Billing

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.