Vercel AI SDK
The AI SDK reaches Kaer through its OpenAI-compatible provider, @ai-sdk/openai-compatible. There is no Kaer package to install: point createOpenAICompatible at https://api.kaer.ai/v1 and call janus-1-flash or janus-1-pro with generateText, streamText and tools.
- Package
@ai-sdk/openai-compatibleon npm- Base URL
https://api.kaer.ai/v1- Scope
models- Price
- Each call is billed like
POST /v1/chat/completions, see Models
#Before you begin
- A key with the
modelsscope. See Authentication. - Credit on your account. See Billing.
- Node.js 18 or later, and AI SDK 5 or later.
#Install the packages
npm install ai @ai-sdk/openai-compatible zod
export KAER_API_KEY="kaer_live_..."#Create the provider
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
const kaer = createOpenAICompatible({
name: "kaer",
baseURL: "https://api.kaer.ai/v1",
apiKey: process.env.KAER_API_KEY,
includeUsage: true,
});includeUsage asks for token counts on streamed answers. To record requests under a region, use https://api-eu.kaer.ai/v1, https://api-us.kaer.ai/v1 or https://api-apac.kaer.ai/v1 as baseURL; the same key works on all of them.
#Generate text
import { generateText } from "ai";
const { text, usage } = await generateText({
model: kaer("janus-1-flash"),
prompt: "Explain quantum computing in one paragraph.",
});
console.log(text, usage.inputTokens, usage.outputTokens);#Stream text
import { streamText } from "ai";
const result = streamText({
model: kaer("janus-1-pro"),
prompt: "Write a short poem about the sea.",
});
for await (const textPart of result.textStream) {
process.stdout.write(textPart);
}result.toUIMessageStreamResponse() returns the stream from a Next.js route handler, so useChat works as it does with any other provider.
#Call tools
import { generateText, stepCountIs, tool } from "ai";
import { z } from "zod";
const { text } = await generateText({
model: kaer("janus-1-flash"),
tools: {
getWeather: tool({
description: "Current weather for a city",
inputSchema: z.object({ city: z.string() }),
execute: async ({ city }) => ({ city, tempC: 14, sky: "cloudy" }),
}),
},
stopWhen: stepCountIs(3),
prompt: "What is the weather in London?",
});A request takes up to 64 tools. toolChoice: "none" keeps the model from calling a tool; any other value is answered as "auto".
#Know what is not supported
- Structured output:
Output.objectandgenerateObjectsendresponse_format, which Kaer ignores, so the answer is not held to your schema. Ask for the data as a tool call instead. - Image, audio and file parts in messages are refused with 400.
- Embedding and image models:
kaer.embeddingModel()andkaer.imageModel()get 404unknown_endpoint.
#Read the cost
A call that is not streamed carries its cost in the x-kaer-cost-usd header:
const result = await generateText({ model: kaer("janus-1-flash"), prompt: "Say hi" });
console.log(result.response.headers?.["x-kaer-cost-usd"]);A streamed answer sends its cost in the last chunk. Pass includeRawChunks: true and read it from the raw parts:
const result = streamText({ model: kaer("janus-1-flash"), prompt: "Say hi", includeRawChunks: true });
let costUsd: number | undefined;
for await (const part of result.fullStream) {
if (part.type !== "raw") continue;
const raw = part.rawValue as { kaer?: { cost_usd: number } };
if (raw.kaer) costUsd = raw.kaer.cost_usd;
}#Handle errors
A failed call throws APICallError, with the HTTP statusCode and the API's message, such as Invalid or revoked API key. for a bad key.
| Status | Code | What it means |
|---|---|---|
| 401 | invalid_api_key | The key is malformed, unknown, revoked or expired. |
| 403 | insufficient_scope | The key does not have the endpoint's scope. |
| 402 | insufficient_credits | Your balance cannot cover the call's hold. Free credit never pays for mail. |
| 402 | key_budget_exceeded | The key's monthly budget is used up. Open holds count. |
| 429 | rate_limit_exceeded | Too many requests this minute for the key or the account, or too many invalid keys from your address. |
| 429 | too_many_streams | Your account already has its tier's maximum of open streams. |
| 404 | model_not_found | No such model on the platform. |
| 502 | upstream_error | The model call failed, or the connection to the API dropped. |
Every code, with its fix, is in Errors.