AI Integration
AI Integration
Section titled “AI Integration”Introduction
Section titled “Introduction”The Vercel AI SDK simplifies integrating AI models into Next.js. It handles streaming, error handling, and type safety so you can focus on the user experience.
npm install ai @ai-sdk/openaiEnvironment Variables
Section titled “Environment Variables”OPENAI_API_KEY=sk-your-key-hereChat Server Action
Section titled “Chat Server Action”'use server'
import { openai } from '@ai-sdk/openai'import { generateText, streamText } from 'ai'import { createStreamableValue } from 'ai/rsc'import { db } from '@/lib/db'
export async function continueConversation( conversationId: string, userMessage: string) { const stream = createStreamableValue()
;(async () => { const { textStream } = await streamText({ model: openai('gpt-4o-mini'), prompt: userMessage, })
let fullResponse = ''
for await (const delta of textStream) { fullResponse += delta stream.update(delta) }
stream.done()
// Save messages to database await db.message.createMany({ data: [ { conversationId, role: 'user', content: userMessage, }, { conversationId, role: 'assistant', content: fullResponse, }, ], }) })()
return { stream: stream.value }}Key points:
streamTextfrom the AI SDK returns an async iterable of text chunkscreateStreamableValuecreates a client-consumable stream- The stream is consumed in a background async function so it doesn’t block
- Messages are saved to the database after streaming completes
Simple Text Generation
Section titled “Simple Text Generation”For non-streaming responses (e.g., generating summaries):
export async function generateSummary(text: string) { const { text: summary } = await generateText({ model: openai('gpt-4o-mini'), prompt: `Summarize this in 3 sentences: ${text}`, })
return summary}Best Practices
Section titled “Best Practices”- Use
gpt-4o-minifor chat — it’s fast and cost-effective - Handle API errors gracefully with retry logic
- Store messages for conversation context
- Never expose your API key to the client
- Use environment variables for model configuration
Common Mistakes
Section titled “Common Mistakes”- Blocking the response — Always use streaming for chat interfaces
- Not saving messages — Without persistence, users lose their conversation on refresh
- Exposing the API key — Server Actions run on the server, keeping keys safe
- No error handling — AI APIs can fail — show a retry button
Summary
Section titled “Summary”The Vercel AI SDK makes AI integration straightforward. Use streamText for streaming chat responses and generateText for one-off completions. Always save messages to a database for conversation history.