# Vercel AI SDK (/docs/guides/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](https://console.cheapaimodel.ai/keys) page |
| Model ID | `cheapaimodel(modelId)` | `deepseek-v4-1-flash`, or any ID from the [catalog](/models)                      |

## Set it up [#set-it-up]

```bash title="Terminal"
npm install ai @ai-sdk/openai-compatible
```

```ts title="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
});
```

```ts title="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 [#streaming]

```ts title="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 [#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)`.
