Lesson 1 of 6·11 min read

Understanding the Vercel AI SDK

The Vercel AI SDK is the standard library for AI applications in Next.js — a TypeScript-first framework that abstracts the complexity of LLM integration and provides streaming, tool calling, and multi-provider support out of the box.

Core Concepts

Why Vercel AI SDK?

Without the SDK, for every AI integration you need to:

  • Manually parse HTTP streams
  • Abstract provider-specific APIs
  • Manage token streaming in React state
  • Orchestrate tool calls
  • Implement error handling and retries

The Vercel AI SDK solves all of this with a unified API:

import { generateText } from 'ai'
import { openai } from '@ai-sdk/openai'

const { text } = await generateText({
  model: openai('gpt-4.1'),
  prompt: 'Explain Kubernetes in one sentence.',
})

The Three Main Modules

ModuleImportPurpose
AI SDK CoreaiServer-side: generateText, generateObject, streamText
AI SDK UIai/reactClient-side: useChat, useCompletion, useObject
AI SDK Providers@ai-sdk/*Provider abstractions for OpenAI, Anthropic, Google, etc.

Provider Abstraction

Multi-Provider Support

The SDK abstracts provider differences behind a unified API:

import { openai } from '@ai-sdk/openai'
import { anthropic } from '@ai-sdk/anthropic'
import { google } from '@ai-sdk/google'

// Same interface — different providers
const model1 = openai('gpt-4.1')
const model2 = anthropic('claude-sonnet-4-20250514')
const model3 = google('gemini-2.5-pro')

Supported providers (2026):

  • OpenAI (GPT-4.1, GPT-4.1 mini, o3, o4-mini)
  • Anthropic (Claude Opus 4, Claude Sonnet 4)
  • Google (Gemini 2.5 Pro, Gemini 2.5 Flash)
  • Mistral (Large, Medium, Small)
  • Groq, Fireworks, Together AI, Perplexity
  • Self-hosted via OpenAI-compatible APIs (vLLM, Ollama)

Provider Switching Without Code Changes

Thanks to the abstraction layer, you can switch providers without changing application code:

// config.ts
const MODEL = process.env.AI_PROVIDER === 'anthropic'
  ? anthropic('claude-sonnet-4-20250514')
  : openai('gpt-4.1')

// Everywhere in code
const result = await generateText({ model: MODEL, prompt })

Streaming

Why Streaming?

Without streaming, the user waits 3–30 seconds for a complete response. With streaming, they see tokens in real time — perceived latency drops to under 500 ms.

streamText — Server-Side

import { streamText } from 'ai'
import { openai } from '@ai-sdk/openai'

export async function POST(req: Request) {
  const { messages } = await req.json()

  const result = streamText({
    model: openai('gpt-4.1'),
    messages,
  })

  return result.toDataStreamResponse()
}

useChat & useCompletion — Client-Side

useChat Hook

The most important hook for chat interfaces:

'use client'
import { useChat } from 'ai/react'

export function ChatUI() {
  const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat({
    api: '/api/chat',
  })

  return (
    <div>
      {messages.map(m => (
        <div key={m.id}>{m.role}: {m.content}</div>
      ))}
      <form onSubmit={handleSubmit}>
        <input value={input} onChange={handleInputChange} />
        <button disabled={isLoading}>Send</button>
      </form>
    </div>
  )
}

useCompletion Hook

For single-turn completions (no chat history):

  • Text summarization
  • Translation
  • Code generation
  • Content creation

Why Vercel AI SDK for Next.js? It's the only SDK that seamlessly connects Server Components, Route Handlers, Server Actions, and Client Components. The integration with the Next.js ecosystem (Edge Runtime, Streaming, ISR) is unique.

📝

Quiz

Question 1 of 3

Welches sind die drei Hauptmodule des Vercel AI SDK?