Docs / SDKs
Use Cent AI with the OpenAI JavaScript SDK
Point the openai npm package at Cent AI from Node.js or TypeScript and read the cost of each call.
Last updated
Your setup
Fills in YOUR_API_KEY in the code on this page. The key stays in this tab: it is never sent and never saved.
Prerequisites
- A Cent AI account with balance. See Balance.
- An API key from Keys. It looks like
cai_<secret>and is shown once. - Node.js with the
openaipackage:
npm install openai
Set up the client
Use the base URL https://api.cent-ai.dev/v1 and your Cent AI key. The SDK adds /chat/completions and /responses itself.
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.cent-ai.dev/v1",
apiKey: process.env.CENT_AI_KEY,
});
The SDK also reads both values from environment variables, so code that calls new OpenAI() without options works unchanged. Replace YOUR_API_KEY with your key:
export OPENAI_BASE_URL=https://api.cent-ai.dev/v1
export OPENAI_API_KEY="YOUR_API_KEY"
Call Cent AI from your server. A key placed in browser code can be read by anyone who opens the page; see Authentication.
Send a request
Chat Completions and Responses both work with the same client. Use a model name from the models page.
const r = await client.chat.completions.create({
model: "glm-5.3",
messages: [{ role: "user", content: "Hello" }],
});
console.log(r.choices[0].message.content);
Parameters such as temperature, tools and response_format are sent to the model as is. A parameter the model does not support comes back as an error from the model.
Read the cost
Cent AI adds cost to the standard usage object: the amount charged for the request in USD, as a string with 9 decimal places. The SDK does not strip extra fields, but its types do not list cost, so widen the type when you read it:
type CentUsage = { cost?: string };
const r = await client.chat.completions.create({
model: "glm-5.3",
messages: [{ role: "user", content: "Hello" }],
});
console.log(r.usage?.prompt_tokens, r.usage?.completion_tokens);
console.log((r.usage as CentUsage | undefined)?.cost); // "0.000363000"
In plain JavaScript, r.usage.cost works as is. On Responses, the cost is in r.usage the same way.
Streaming
For Chat Completions, ask for usage in the stream with stream_options. The last chunk before the stream ends has an empty choices array and carries usage, including cost.
const stream = await client.chat.completions.create({
model: "glm-5.3",
messages: [{ role: "user", content: "Hello" }],
stream: true,
stream_options: { include_usage: true },
});
for await (const chunk of stream) {
if (chunk.choices.length > 0) {
process.stdout.write(chunk.choices[0].delta.content ?? "");
}
if (chunk.usage) {
console.log("\ncost:", (chunk.usage as CentUsage).cost);
}
}
Without stream_options.include_usage, a Chat Completions stream has no usage chunk and no cost. The charge is still recorded on the Requests page. Requests that can take longer than 100 seconds must stream; see Long streaming.
Check the request
Open the Requests page in the dashboard. Each request shows its tokens per rate, the rates used and the cost. The x-request-id response header is the same ID. To read it from the SDK:
const { data, response } = await client.chat.completions
.create({ model: "glm-5.3", messages: [{ role: "user", content: "Hello" }] })
.withResponse();
console.log(response.headers.get("x-request-id"));
Troubleshooting
| Status and code | Usual cause | What to do |
|---|---|---|
401 invalid_api_key | Wrong or revoked key, or the key is still an OPENAI_API_KEY from elsewhere | Check the key, or create a new one in Keys |
402 insufficient_balance | No balance available for this model | Top up or buy a budget pack, see Balance |
403 ip_not_allowed, model_not_allowed | The key's IP or model allowlist blocks the request | Change the allowlist on the key |
404 model_not_found | A model name Cent AI does not have | Use a name from GET /v1/models |
| 404 without that code | The base URL is missing /v1 | Set the base URL to https://api.cent-ai.dev/v1 |
429 rate_limited | Over 600 requests per minute for the key | Wait for Retry-After, see Rate limits |
429 key_limit_exceeded, project_budget_exceeded | A daily or monthly limit of the key or project was reached | Raise the limit or wait until the time in the message |
501 not_available_yet | An endpoint that is not open yet, such as embeddings | Use chat models; see Errors |
503 upstream_timeout or a stream that stops | The model did not start in time, or a stream hit its maximum duration | See Stream timeout |
The SDK throws OpenAI.APIError subclasses for these statuses, with status and code, and retries some of them on its own. All codes are listed in Errors.