Vercel AI SDK Integration
Use Antbase as the backend for Vercel AI SDK streaming UI components. Get intelligent model routing with built-in React hooks for chat, completion, and streaming interfaces.
What this enables
- Streaming chat UIs with
useChatanduseCompletionhooks - Automatic model routing via Antbase virtual models
- Server-side streaming with Next.js App Router and Route Handlers
- Built-in failover -- if one provider goes down, responses keep flowing
Quick Start
1. Install dependencies
npm install ai @ai-sdk/openai2. Configure environment
# .env.local
OPENAI_API_KEY=your-ant-api-key
OPENAI_BASE_URL=https://antbase.ai/v13. Create a Route Handler
// app/api/chat/route.ts
import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';
export async function POST(req: Request) {
const { messages } = await req.json();
const result = streamText({
model: openai('ant:auto'),
messages,
});
return result.toDataStreamResponse();
}4. Build the chat UI
// app/page.tsx
'use client';
import { useChat } from 'ai/react';
export default function Chat() {
const { messages, input, handleInputChange, handleSubmit } = useChat();
return (
<div>
{messages.map((m) => (
<div key={m.id}>
<strong>{m.role}:</strong> {m.content}
</div>
))}
<form onSubmit={handleSubmit}>
<input value={input} onChange={handleInputChange} />
<button type="submit">Send</button>
</form>
</div>
);
}Configuration
If you need explicit control over the provider instance:
import { createOpenAI } from '@ai-sdk/openai';
const antbase = createOpenAI({
apiKey: process.env.ANTBASE_API_KEY,
baseURL: 'https://antbase.ai/v1',
});
// Use in streamText / generateText
const result = streamText({
model: antbase('ant:auto'),
messages,
});Usage Examples
Streaming completion
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
const { text } = await generateText({
model: openai('ant:fast'),
prompt: 'Summarize the key benefits of edge computing.',
});
console.log(text);Structured output
import { openai } from '@ai-sdk/openai';
import { generateObject } from 'ai';
import { z } from 'zod';
const { object } = await generateObject({
model: openai('ant:best'),
schema: z.object({
summary: z.string(),
sentiment: z.enum(['positive', 'negative', 'neutral']),
topics: z.array(z.string()),
}),
prompt: 'Analyze this customer review: "Great product, fast shipping!"',
});Reasoning model
import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';
const result = streamText({
model: openai('ant:reasoning'),
prompt: 'Solve: If 3x + 7 = 22, what is x?',
});
for await (const chunk of result.textStream) {
process.stdout.write(chunk);
}Virtual Models
| Model | Best for | Notes |
|---|---|---|
| ant:auto | General use | Classifies prompt and picks optimal model |
| ant:fast | Low-latency tasks | Ideal for streaming UIs that need snappy responses |
| ant:best | Complex analysis | Routes to frontier models (GPT-4o, Claude, Gemini) |
| ant:free | Development, testing | Zero cost; uses free-tier provider endpoints |
| ant:reasoning | Multi-step logic | Chain-of-thought reasoning models |
| ant:coder | Code generation | Optimized for code tasks |
You can also pass specific model IDs like gpt-4o or claude-sonnet-4-20250514. See Virtual Models for the full list.
Troubleshooting
Streaming not working?
Make sure you are using streamText with toDataStreamResponse(), not generateText. Also verify your route handler returns the stream response directly.
401 Unauthorized
Check that OPENAI_API_KEY is set in .env.local and restart your dev server. The AI SDK reads this variable automatically.