> ## 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-native 제공자를 사용하여 TokenLab을 Vercel AI SDK와 통합하세요

## 개요

<Note>
  **유형**: 프레임워크 또는 플랫폼

  **기본 경로**: OpenAI 호환 기본값

  **지원 신뢰도**: 권장 통합 패턴
</Note>

TokenLab의 경우, Vercel AI SDK에서 가장 안정적인 기본값은 \*\*OpenAI 호환 제공자(OpenAI-compatible provider)\*\*입니다.

만약 **Responses-native** 동작이 특별히 필요한 경우, 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',
});
```

## 텍스트 생성 (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);
```

## Responses-native 동작이 명시적으로 필요한 경우

```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`와 연결된 제공자 동작이 필요한 경우, 하나의 클라이언트에서 두 패턴을 혼용하지 말고 의도적으로 제공자 패키지를 전환하십시오.
  </Accordion>

  <Accordion title="비밀 키는 서버에 보관">
    TokenLab API 키를 클라이언트 측 코드에 노출하지 마십시오. 제공자 설정은 서버 파일이나 API 라우트에 배치하십시오.
  </Accordion>
</AccordionGroup>
