cheapaimodel

Vercel AI SDK

Use cheapaimodel models in the Vercel AI SDK through the OpenAI-compatible provider.

Copy

The AI SDK reaches OpenAI-compatible services through the @ai-sdk/openai-compatible provider. Create one provider for cheapaimodel and pass its models to generateText, streamText and the other AI SDK functions.

AI SDK settingValue
Base URLbaseURLhttps://api.cheapaimodel.ai/v1
API keyapiKeyYour key from the console's API Keys page
Model IDcheapaimodel(modelId)deepseek-v4-1-flash, or any ID from the catalog

Set it up

Terminal
npm install ai @ai-sdk/openai-compatible
cheapaimodel.ts
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";

export const cheapaimodel = createOpenAICompatible({
  name: "cheapaimodel",
  baseURL: "https://api.cheapaimodel.ai/v1",
  apiKey: process.env.CHEAPAIMODEL_API_KEY,
  includeUsage: true, // token usage on streamed responses
});
generate.ts
import { generateText } from "ai";
import { cheapaimodel } from "./cheapaimodel";

const { text } = await generateText({
  model: cheapaimodel("deepseek-v4-1-flash"),
  prompt: "Explain recursion in one sentence.",
});
console.log(text);

The provider sends requests to /v1/chat/completions. It works the same way in a Next.js route handler: pass cheapaimodel("deepseek-v4-1-flash") as the model.

Streaming

stream.ts
import { streamText } from "ai";
import { cheapaimodel } from "./cheapaimodel";

const result = streamText({
  model: cheapaimodel("deepseek-v4-1-flash"),
  prompt: "Write a haiku about rivers.",
});

for await (const chunk of result.textStream) {
  process.stdout.write(chunk);
}
console.log("\nTokens:", (await result.usage).totalTokens);

Common mistakes

  • Unauthenticated request to AI Gateway. The model was passed as a plain string, such as model: "deepseek-v4-1-flash". The AI SDK sends plain model strings to the Vercel AI Gateway, not to cheapaimodel. Pass cheapaimodel("deepseek-v4-1-flash") instead.
  • Not Found or Invalid URL (POST /v1/chat/completions/chat/completions). baseURL is missing /v1, or it includes the endpoint path. Set it to exactly https://api.cheapaimodel.ai/v1; the provider adds /chat/completions itself.
  • Token usage is undefined on streamed calls. The OpenAI-compatible provider asks for usage in streams only when includeUsage: true is set. Keep it in the provider settings.
  • Multi-turn chats through @ai-sdk/openai. Its createOpenAI also accepts our base URL, but the default openai(modelId) calls the Responses API, and when you pass the SDK's earlier replies back in messages it sends them as item_reference IDs instead of their text. That only works if the server stored those responses. Use the OpenAI-compatible provider above, or openai.chat(modelId).
Copy
Open in

On this page