How do I build a multi-turn chat in JavaScript?
The API is stateless, so your code holds the conversation.
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 messages = [{ role: "system", content: "You are a friendly tutor." }];
async function ask(text) {
messages.push({ role: "user", content: text });
const r = await client.chat.completions.create({ model: "supercosmos-flash", messages });
const reply = r.choices[0].message.content;
messages.push({ role: "assistant", content: reply });
return reply;
}
console.log(await ask("What is a prime number?"));
console.log(await ask("Is 51 one?"));
History is re-sent and billed as input each turn. Requests are limited to 800 messages and 3 MB.
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
- Each key has its own limit: 10, 30 (default), 60 or 120 requests per minute. Rate limits
Still need help?#
- General support: support@clatterlabs.com or team@clatterlabs.xyz
- Infrastructure questions, or permanent deletion of your data (your whole account or one service): infrastructure@clatterlabs.xyz
We usually reply within the first hour, and always within 24 hours. A full support team is with you.
Related articles
- How do I set up the OpenAI JavaScript SDK for the Model API?
- How do I use tool calling in JavaScript?
- How do I cancel a Model API request in JavaScript?
- How do I keep a conversation going in Python?
- How long does Cosmos keep my chat history?Cosmos on Telegram
- Do I pay for my system prompt on every request?SuperCosmos models
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.xyzFor deleting your whole account or the data of one service.