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

> OpenAI互換またはResponsesネイティブプロバイダーを使用して、TokenLabをVercel AI SDKに統合します

## 概要

<Note>
  **タイプ**: フレームワークまたはプラットフォーム

  **推奨パス**: OpenAI互換デフォルト

  **サポートの信頼性**: 推奨される統合パターン
</Note>

TokenLabにおいて、Vercel AI SDKで最も安定したデフォルトは **OpenAI互換プロバイダー** です。

**Responsesネイティブ** の動作が特に必要な場合は、OpenAIプロバイダーに切り替えて、同じTokenLabのベースURLを使用することができます。

このページは推奨される統合パターンを示すものであり、Vercel AI SDKのすべてのヘルパーがTokenLabでサポートされていることを保証するものではありません。

## TokenLabプロバイダーパッケージ

TokenLabは、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);
```

## 推奨されるデフォルト: 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',
});
```

## テキスト生成

```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);
```

## ストリームテキスト

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

## ツール呼び出し

```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);
```

## 構造化出力

```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);
```

## 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>
  プロキシ形式の統合には、安全なデフォルトとして `@ai-sdk/openai-compatible` を使用してください。`/v1/responses` に基づくプロバイダーパスが明示的に必要な場合にのみ、`@ai-sdk/openai` に切り替えてください。
</Note>

## 環境変数

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

## ベストプラクティス

<AccordionGroup>
  <Accordion title="まずはopenai-compatibleを優先する">
    サードパーティのゲートウェイやプロキシバックエンドの場合、通常 `@ai-sdk/openai-compatible` が最もトラブルの少ない出発点となります。
  </Accordion>

  <Accordion title="必要な場合にのみResponsesへ移行する">
    `/v1/responses` に紐付いたプロバイダーの動作が必要な場合は、1つのクライアント内で両方のパターンを混在させるのではなく、意図的にプロバイダーパッケージを切り替えてください。
  </Accordion>

  <Accordion title="シークレットはサーバーで管理する">
    TokenLab APIキーをクライアント側のコードで公開しないでください。プロバイダーの設定はサーバーファイルまたはAPIルート内で行ってください。
  </Accordion>
</AccordionGroup>
