# Vercel AI SDK (/docs/sdk/vercel-ai-sdk)

The [AI SDK](https://ai-sdk.dev) works with OhMyGPT through its standard OpenAI provider. Give `createOpenAI` the OhMyGPT base URL and key, and `generateText`, `streamText` and tool calling run on Claude, GPT, GLM and every other chat model in the catalog. The examples use `ai` 7 and `@ai-sdk/openai` 4.

## Install [#install]

```bash
npm install ai @ai-sdk/openai zod
```

## Create the provider [#create-the-provider]

```ts title="ohmygpt.ts"
import { createOpenAI } from '@ai-sdk/openai';

export const ohmygpt = createOpenAI({
  baseURL: 'https://api.ohmygpt.com/v1',
  apiKey: process.env.OHMYGPT_API_KEY,
});
```

Pass any model ID from `GET /v1/models` to `ohmygpt(...)`. Requests go to OhMyGPT's Responses endpoint, which every chat model accepts.

## Generate text [#generate-text]

Start with the free test model `ohmygpt/mock`, then change the ID:

```ts
import { generateText } from 'ai';
import { ohmygpt } from './ohmygpt';

const { text, usage } = await generateText({
  model: ohmygpt('ohmygpt/mock'),
  prompt: 'Write a haiku about the sea.',
});
console.log(text);
console.log(usage.inputTokens, usage.outputTokens);
```

```text
This is a mock reply from OhMyGPT. Your integration works; switch to a real model when ready.
21 17
```

With `zhipu/glm-5.3-flash`:

```text
Waves crash on the shore—
salt spray kisses the warm sand,
the tide pulls me home.
20 383
```

The output count includes the model's reasoning tokens.

## Stream [#stream]

```ts
import { streamText } from 'ai';
import { ohmygpt } from './ohmygpt';

const result = streamText({
  model: ohmygpt('zhipu/glm-5.3-flash'),
  prompt: 'Write a haiku about the sea.',
});
for await (const chunk of result.textStream) {
  process.stdout.write(chunk);
}
```

## Call tools [#call-tools]

`execute` runs the tool, and `stopWhen` lets the SDK send the result back for a final answer:

```ts
import { generateText, isStepCount, tool } from 'ai';
import { z } from 'zod';
import { ohmygpt } from './ohmygpt';

const { text } = await generateText({
  model: ohmygpt('zhipu/glm-5.3-flash'),
  tools: {
    getWeather: tool({
      description: 'Get the current weather for a city',
      inputSchema: z.object({ city: z.string() }),
      execute: async ({ city }) => ({ city, temperatureC: 18, condition: 'cloudy' }),
    }),
  },
  stopWhen: isStepCount(3),
  prompt: 'What is the weather in Paris?',
});
console.log(text);
```

```text
The current weather in Paris is **cloudy** with a temperature of **18°C** (about 64°F). It's a mild day, so you might want a light jacket if you're heading out!
```

On `ohmygpt/mock`, the same program calls `getWeather` with `city: ""` and then prints the mock reply, so the whole loop runs at no cost. [Tool Calling](/docs/tool-calling) shows the request and response shapes.

## Choose providers [#choose-providers]

To send OhMyGPT's `provider` object, for example to prefer the lowest price, use `@ai-sdk/openai-compatible`. It copies `providerOptions.ohmygpt` into the request body:

```bash
npm install @ai-sdk/openai-compatible
```

```ts
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { generateText } from 'ai';

const ohmygpt = createOpenAICompatible({
  name: 'ohmygpt',
  baseURL: 'https://api.ohmygpt.com/v1',
  apiKey: process.env.OHMYGPT_API_KEY,
});

const { text } = await generateText({
  model: ohmygpt('zhipu/glm-5.3-flash'),
  prompt: 'Say hi.',
  providerOptions: {
    ohmygpt: { provider: { sort: 'price' } },
  },
});
```

[Provider Routing](/docs/provider-routing) lists the fields.

## Anthropic provider [#anthropic-provider]

If your code uses `@ai-sdk/anthropic`, point it at OhMyGPT the same way. Its `baseURL` includes `/v1`:

```ts
import { createAnthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';

const anthropic = createAnthropic({
  baseURL: 'https://api.ohmygpt.com/v1',
  apiKey: process.env.OHMYGPT_API_KEY,
});

const { text } = await generateText({
  model: anthropic('anthropic/claude-haiku-4-5'),
  prompt: 'Say hi.',
});
```
