> ## 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

> Integrasikan TokenLab dengan Vercel AI SDK menggunakan penyedia yang kompatibel dengan OpenAI atau native-Responses

## Gambaran Umum

<Note>
  **Tipe**: Framework atau Platform

  **Jalur Utama**: Default yang kompatibel dengan OpenAI

  **Tingkat Dukungan**: Pola integrasi yang direkomendasikan
</Note>

Untuk TokenLab, default yang paling stabil di Vercel AI SDK adalah **penyedia yang kompatibel dengan OpenAI**.

Jika Anda secara khusus memerlukan perilaku **native-Responses**, Anda dapat beralih ke penyedia OpenAI dan tetap menggunakan base URL TokenLab yang sama.

Anggap halaman ini sebagai pola integrasi yang direkomendasikan, bukan sebagai jaminan bahwa setiap helper di Vercel AI SDK didukung oleh TokenLab.

## Paket Penyedia TokenLab

TokenLab juga mengelola wrapper penyedia yang ringan untuk 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);
```

## Default yang Direkomendasikan: Penyedia yang Kompatibel dengan OpenAI

```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',
});
```

## Menghasilkan Teks (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);
```

## Streaming Teks (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);
}
```

## Pemanggilan Tool (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);
```

## Output Terstruktur (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);
```

## Jika Anda Secara Eksplisit Memerlukan Perilaku Native-Responses

```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>
  Gunakan `@ai-sdk/openai-compatible` sebagai default yang aman untuk integrasi bergaya proxy. Beralihlah ke `@ai-sdk/openai` hanya jika Anda secara eksplisit menginginkan jalur penyedia yang dibangun di atas `/v1/responses`.
</Note>

## Variabel Lingkungan (Environment Variables)

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

## Praktik Terbaik

<AccordionGroup>
  <Accordion title="Utamakan openai-compatible terlebih dahulu">
    Untuk gateway pihak ketiga dan backend proxy, `@ai-sdk/openai-compatible` biasanya merupakan titik awal yang paling minim kendala.
  </Accordion>

  <Accordion title="Beralih ke Responses hanya jika diperlukan">
    Jika Anda memerlukan perilaku penyedia yang terikat pada `/v1/responses`, ubahlah paket penyedia secara sengaja alih-alih mencampur kedua pola tersebut dalam satu klien.
  </Accordion>

  <Accordion title="Simpan rahasia di server">
    Jangan pernah mengekspos kunci API TokenLab Anda dalam kode sisi klien (client-side). Letakkan pengaturan penyedia di file server atau rute API.
  </Accordion>
</AccordionGroup>
