Skip to main content
Integrations

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.

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 models scope. See Authentication.
  • Credit on your account. See Billing.
  • Node.js 18 or later, and AI SDK 5 or later.

#Install the packages

Shell
npm install ai @ai-sdk/openai-compatible zod

export KAER_API_KEY="kaer_live_..."

#Create the provider

TypeScript
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

TypeScript
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

TypeScript
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

TypeScript
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.object and generateObject send response_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() and kaer.imageModel() get 404 unknown_endpoint.

#Read the cost

A call that is not streamed carries its cost in the x-kaer-cost-usd header:

TypeScript
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:

TypeScript
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.

StatusCodeWhat it means
401invalid_api_keyThe key is malformed, unknown, revoked or expired.
403insufficient_scopeThe key does not have the endpoint's scope.
402insufficient_creditsYour balance cannot cover the call's hold. Free credit never pays for mail.
402key_budget_exceededThe key's monthly budget is used up. Open holds count.
429rate_limit_exceededToo many requests this minute for the key or the account, or too many invalid keys from your address.
429too_many_streamsYour account already has its tier's maximum of open streams.
404model_not_foundNo such model on the platform.
502upstream_errorThe model call failed, or the connection to the API dropped.

Every code, with its fix, is in Errors.