Frameworks & Platforms
Vercel AI SDK
Use TokenLab models with Vercel AI SDK
Overview
Use the OpenAI-compatible provider for Chat Completions. If the selected model and your application need Responses, use the OpenAI provider with the same TokenLab Base URL.
TokenLab Provider Package
TokenLab also provides a small Vercel AI SDK wrapper:
npm install ai @tokenlabai/ai-sdk-providerimport { generateText } from 'ai';
import { tokenlab } from '@tokenlabai/ai-sdk-provider';
const { text } = await generateText({
model: tokenlab.chatModel('gpt-5.6-terra'),
prompt: 'Explain TokenLab in one sentence.',
});
console.log(text);Recommended Default: OpenAI-Compatible Provider
npm install ai @ai-sdk/openai-compatible zodimport { 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
import { generateText } from 'ai';
import { tokenlab } from './tokenlab';
const { text } = await generateText({
model: tokenlab.chatModel('gpt-5.6-terra'),
prompt: 'Explain TokenLab in one sentence.',
});
console.log(text);Stream Text
import { streamText } from 'ai';
import { tokenlab } from './tokenlab';
const result = await streamText({
model: tokenlab.chatModel('gpt-5.6-terra'),
prompt: 'Write a short poem about coding.',
});
for await (const textPart of result.textStream) {
process.stdout.write(textPart);
}Tool Calling
import { generateText, stepCountIs, tool } from 'ai';
import { z } from 'zod';
import { tokenlab } from './tokenlab';
const result = await generateText({
model: tokenlab.chatModel('gpt-5.6-terra'),
prompt: 'What is the weather in San Francisco?',
stopWhen: stepCountIs(2),
tools: {
weather: tool({
description: 'Get weather in a location',
inputSchema: z.object({
location: z.string(),
}),
execute: async ({ location }) => ({
location,
temperature: 72,
condition: 'sunny',
}),
}),
},
});
console.log(result.text);Structured Output
import { generateObject } from 'ai';
import { z } from 'zod';
import { tokenlab } from './tokenlab';
const { object } = await generateObject({
model: tokenlab.chatModel('gpt-5.6-terra'),
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
npm install ai @ai-sdk/openaiimport { createOpenAI } from '@ai-sdk/openai';
export const tokenlabResponses = createOpenAI({
apiKey: process.env.TOKENLAB_API_KEY,
baseURL: 'https://api.tokenlab.sh/v1',
});import { generateText } from 'ai';
import { tokenlabResponses } from './tokenlab-responses';
const { text } = await generateText({
model: tokenlabResponses('gpt-5.6-terra'),
prompt: 'Explain TokenLab in one sentence.',
});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.
Environment Variables
# .env.local
TOKENLAB_API_KEY=sk-your-tokenlab-key