Why Next.js for AI?
Next.js provides the perfect foundation for AI apps: server-side rendering for SEO, API routes for backend logic, and streaming support for real-time responses.
Setting Up
- Install the Vercel AI SDK: `npm install ai @ai-sdk/openai`
- Create an API route at `app/api/chat/route.ts`
- Use the `streamText` helper for real-time token streaming.
Core Patterns
### Streaming Chat
Use the `useChat` hook on the client side. It handles streaming, message state, and abort automatically.
### RAG Integration
- Store documents in a vector database (Pinecone, Qdrant, or pgvector).
- On user query, retrieve relevant chunks.
- Include chunks in the system prompt as context.
### Image Generation
- Use DALL-E or Stable Diffusion APIs.
- Display loading states with optimistic UI updates.
Performance Tips
- Edge Runtime: Deploy API routes to the edge for lower latency.
- Caching: Cache embeddings and common responses.
- Batching: Group multiple API calls when possible.
Deployment
- Deploy on Vercel for automatic edge deployment.
- Use environment variables for API keys.
- Set up rate limiting to control costs.
Streaming Responses
The Vercel AI SDK provides excellent streaming support. Use `useChat` hook for chat interfaces and `streamText` on the server for efficient token-by-token delivery. Always show a loading indicator and allow users to stop generation mid-stream.
State Management
- Conversation history: Store in database (PostgreSQL recommended) with proper indexing on conversation_id and created_at.
- Optimistic updates: Show user messages immediately, then reconcile with actual AI response.
- Caching: Cache completed conversations to reduce API costs. Use Redis for session-level caching.
Performance Tips
- Edge runtime: Deploy API routes on Vercel Edge or Cloudflare Workers for lower latency.
- Parallel tool calls: When using function calling, execute independent tools in parallel.
- Image optimization: Use Next.js Image component with proper sizing. AI-generated images should be compressed before display.
- Rate limiting: Implement per-user rate limiting to prevent abuse and control costs.