Getting Started with Vercel AI SDK
The Vercel AI SDK provides a unified API for streaming LLM responses, tool use, and structured output across multiple providers.
Installation
```bash
npm install ai @ai-sdk/openai @ai-sdk/anthropic @ai-sdk/google
```
Basic Streaming Chat
```typescript
import { streamText } from 'ai'
import { openai } from '@ai-sdk/openai'
const result = streamText({
model: openai('gpt-4o'),
prompt: 'Explain quantum computing in simple terms',
})
for await (const chunk of result.textStream) {
process.stdout.write(chunk)
}
```
Multi-Provider Support
Switch between providers with one line:
```typescript
import { anthropic } from '@ai-sdk/anthropic'
import { google } from '@ai-sdk/google'
// Same API, different providers
const result = streamText({
model: anthropic('claude-sonnet-4-6'),
prompt: 'Write a haiku about coding',
})
```
Tool Use
```typescript
import { generateText, tool } from 'ai'
import { z } from 'zod'
const result = await generateText({
model: openai('gpt-4o'),
tools: {
weather: tool({
description: 'Get weather for a location',
parameters: z.object({ city: z.string() }),
execute: async ({ city }) => fetchWeather(city),
}),
},
prompt: 'What is the weather in London?',
})
```