> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tokenlab.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# Vercel AI SDK

> Integrate TokenLab with Vercel AI SDK using OpenAI-compatible or Responses-native providers

## Overview

<Note>
  **Type**: Framework or Platform

  **Primary Path**: OpenAI-compatible default

  **Support Confidence**: Recommended integration pattern
</Note>

For TokenLab, the most stable default in Vercel AI SDK is the **OpenAI-compatible provider**.

If you specifically need **Responses-native** behavior, you can switch to the OpenAI provider and keep the same TokenLab base URL.

Treat this page as a recommended integration pattern, not as a guarantee that every helper in Vercel AI SDK is supported by TokenLab.

## TokenLab Provider Package

TokenLab also maintains a lightweight provider wrapper for Vercel AI SDK:

```bash theme={null}
npm install ai @tokenlabai/ai-sdk-provider
```

```typescript theme={null}
import { generateText } from 'ai';
import { tokenlab } from '@tokenlabai/ai-sdk-provider';

const { text } = await generateText({
  model: tokenlab.chatModel('gpt-5.4'),
  prompt: 'Explain TokenLab in one sentence.',
});

console.log(text);
```

## Recommended Default: OpenAI-Compatible Provider

```bash theme={null}
npm install ai @ai-sdk/openai-compatible
```

```typescript theme={null}
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';

export const tokenlab = createOpenAICompatible({
  name: 'tokenlab',
  apiKey: process.env.TOKENLAB_API_KEY,
  baseURL: 'https://api.tokenlab.sh/v1',
});
```

## Generate Text

```typescript theme={null}
import { generateText } from 'ai';
import { tokenlab } from './tokenlab';

const { text } = await generateText({
  model: tokenlab.chatModel('gpt-5.4'),
  prompt: 'Explain TokenLab in one sentence.',
});

console.log(text);
```

## Stream Text

```typescript theme={null}
import { streamText } from 'ai';
import { tokenlab } from './tokenlab';

const result = await streamText({
  model: tokenlab.chatModel('gpt-5.4'),
  prompt: 'Write a short poem about coding.',
});

for await (const textPart of result.textStream) {
  process.stdout.write(textPart);
}
```

## Tool Calling

```typescript theme={null}
import { generateText, tool } from 'ai';
import { z } from 'zod';
import { tokenlab } from './tokenlab';

const result = await generateText({
  model: tokenlab.chatModel('gpt-5.4'),
  prompt: 'What is the weather in San Francisco?',
  tools: {
    weather: tool({
      description: 'Get weather in a location',
      parameters: z.object({
        location: z.string(),
      }),
      execute: async ({ location }) => ({
        location,
        temperature: 72,
        condition: 'sunny',
      }),
    }),
  },
});

console.log(result.text);
```

## Structured Output

```typescript theme={null}
import { generateObject } from 'ai';
import { z } from 'zod';
import { tokenlab } from './tokenlab';

const { object } = await generateObject({
  model: tokenlab.chatModel('gpt-5.4'),
  schema: z.object({
    name: z.string(),
    role: z.string(),
  }),
  prompt: 'Generate a fake developer profile.',
});

console.log(object);
```

## If You Explicitly Need Responses-Native Behavior

```bash theme={null}
npm install ai @ai-sdk/openai
```

```typescript theme={null}
import { createOpenAI } from '@ai-sdk/openai';

export const tokenlabResponses = createOpenAI({
  apiKey: process.env.TOKENLAB_API_KEY,
  baseURL: 'https://api.tokenlab.sh/v1',
});
```

```typescript theme={null}
import { generateText } from 'ai';
import { tokenlabResponses } from './tokenlab-responses';

const { text } = await generateText({
  model: tokenlabResponses('gpt-5.4'),
  prompt: 'Explain TokenLab in one sentence.',
});
```

<Note>
  Use `@ai-sdk/openai-compatible` as the safe default for proxy-style integrations. Switch to `@ai-sdk/openai` only when you explicitly want a provider path built on `/v1/responses`.
</Note>

## Environment Variables

```bash theme={null}
# .env.local
TOKENLAB_API_KEY=sk-your-tokenlab-key
```

## Best Practices

<AccordionGroup>
  <Accordion title="Prefer openai-compatible first">
    For third-party gateways and proxy backends, `@ai-sdk/openai-compatible` is usually the least surprising starting point.
  </Accordion>

  <Accordion title="Move to Responses only when needed">
    If you need provider behavior tied to `/v1/responses`, switch the provider package deliberately instead of mixing both patterns in one client.
  </Accordion>

  <Accordion title="Keep secrets on the server">
    Never expose your TokenLab API key in client-side code. Put provider setup in server files or API routes.
  </Accordion>
</AccordionGroup>
