Docs / SDKs
Use Cent AI with the Anthropic TypeScript SDK
Point the @anthropic-ai/sdk package at Cent AI, send Messages requests 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
@anthropic-ai/sdkpackage:
npm install @anthropic-ai/sdk
Set up the client
Use the base URL https://api.cent-ai.dev, without /v1, and your Cent AI key. The SDK adds /v1/messages itself. Cent AI accepts the key in x-api-key and in Authorization: Bearer.
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
baseURL: "https://api.cent-ai.dev",
apiKey: process.env.CENT_AI_KEY,
});
The SDK also reads both values from environment variables, so code that calls new Anthropic() without options works unchanged. Replace YOUR_API_KEY with your key:
export ANTHROPIC_BASE_URL=https://api.cent-ai.dev
export ANTHROPIC_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
Use a model name from the models page. max_tokens is required by the Messages format.
const m = await client.messages.create({
model: "claude-opus-5",
max_tokens: 1024,
messages: [{ role: "user", content: "Hello" }],
});
for (const block of m.content) {
if (block.type === "text") console.log(block.text);
}
system, tools, thinking and cache_control are sent to the model as is. See Messages and Prompt caching.
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 };
console.log(m.usage.input_tokens, m.usage.cache_read_input_tokens, m.usage.output_tokens);
console.log((m.usage as CentUsage).cost); // "0.003210000"
In this format input_tokens does not include cache tokens.
Streaming
In a stream, the cost is in the usage of the last message_delta event. Read it from the raw events of create({ ..., stream: true }):
const stream = await client.messages.create({
model: "claude-opus-5",
max_tokens: 1024,
messages: [{ role: "user", content: "Hello" }],
stream: true,
});
let cost: string | undefined;
for await (const event of stream) {
if (event.type === "content_block_delta" && event.delta.type === "text_delta") {
process.stdout.write(event.delta.text);
} else if (event.type === "message_delta") {
cost = (event.usage as CentUsage).cost ?? cost;
}
}
console.log("\ncost:", cost);
The client.messages.stream(...) helper builds a final message for you, but it is not guaranteed to copy cost into it. When you need the cost, iterate over the events and read message_delta as above. 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.messages
.create({ model: "claude-opus-5", max_tokens: 1024, messages: [{ role: "user", content: "Hello" }] })
.withResponse();
console.log(response.headers.get("x-request-id"));
Troubleshooting
Errors on /v1/messages use the Anthropic shape: the error type follows the HTTP status and the message says what happened.
| Status | Usual cause | What to do |
|---|---|---|
| 401 | Wrong or revoked key, or the key is still an ANTHROPIC_API_KEY from elsewhere | Check the key, or create a new one in Keys |
| 402 | No balance available for this model | Top up or buy a budget pack, see Balance |
| 403 | The key's IP or model allowlist blocks the request, or its project is archived | Change the allowlist on the key, or restore the project |
| 404 | A model name Cent AI does not have, or a base URL that ends in /v1 | Use a name from GET /v1/models and the base URL https://api.cent-ai.dev |
| 429 | Over 600 requests per minute for the key, or a daily or monthly limit of the key or project | Read the message; see Rate limits |
| 503 or a stream that stops | The model is unavailable, did not start in time, or a stream hit its maximum duration | See 5xx errors and Stream timeout |
The SDK throws Anthropic.APIError subclasses for these statuses, with status and headers, and retries some of them on its own. All codes are listed in Errors.