Vercel AI SDK
Use cheapaimodel models in the Vercel AI SDK through the OpenAI-compatible provider.
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 setting | Value | |
|---|---|---|
| Base URL | baseURL | https://api.cheapaimodel.ai/v1 |
| API key | apiKey | Your key from the console's API Keys page |
| Model ID | cheapaimodel(modelId) | deepseek-v4-1-flash, or any ID from the catalog |
Set it up
npm install ai @ai-sdk/openai-compatibleimport { 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
});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
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 asmodel: "deepseek-v4-1-flash". The AI SDK sends plain model strings to the Vercel AI Gateway, not to cheapaimodel. Passcheapaimodel("deepseek-v4-1-flash")instead.Not FoundorInvalid URL (POST /v1/chat/completions/chat/completions).baseURLis missing/v1, or it includes the endpoint path. Set it to exactlyhttps://api.cheapaimodel.ai/v1; the provider adds/chat/completionsitself.- Token usage is
undefinedon streamed calls. The OpenAI-compatible provider asks for usage in streams only whenincludeUsage: trueis set. Keep it in the provider settings. - Multi-turn chats through
@ai-sdk/openai. ItscreateOpenAIalso accepts our base URL, but the defaultopenai(modelId)calls the Responses API, and when you pass the SDK's earlier replies back inmessagesit sends them asitem_referenceIDs instead of their text. That only works if the server stored those responses. Use the OpenAI-compatible provider above, oropenai.chat(modelId).