Best Practices Advanced
Building AI features is one thing - shipping them to production is another. This lesson covers essential patterns for error handling, rate limiting, caching, cost control, and deployment that will keep your AI app reliable and affordable at scale.
Error Handling
AI API calls fail more often than typical REST APIs. Handle errors gracefully:
import { openai } from '@ai-sdk/openai'; import { streamText } from 'ai'; export async function POST(req: Request) { try { const { messages } = await req.json(); const result = await streamText({ model: openai('gpt-4o'), messages, maxTokens: 1000, // Limit response length }); return result.toDataStreamResponse(); } catch (error) { if (error.statusCode === 429) { return new Response('Rate limited. Please try again later.', { status: 429 }); } return new Response('An error occurred.', { status: 500 }); } }
Rate Limiting
Protect your API keys and budget with rate limiting:
import { Ratelimit } from '@upstash/ratelimit'; import { Redis } from '@upstash/redis'; const ratelimit = new Ratelimit({ redis: Redis.fromEnv(), limiter: Ratelimit.slidingWindow(10, '1 m'), // 10 requests per minute }); export async function POST(req: Request) { const ip = req.headers.get('x-forwarded-for') ?? 'anonymous'; const { success } = await ratelimit.limit(ip); if (!success) { return new Response('Too many requests', { status: 429 }); } // ... proceed with AI call }
Cost Optimization
| Strategy | Implementation | Savings |
|---|---|---|
| Set maxTokens | Limit response length to what you actually need | 30-50% |
| Cache responses | Cache identical prompts with Redis or Vercel KV | 50-80% |
| Use smaller models | Use GPT-4o-mini or Claude Haiku for simple tasks | 80-95% |
| Limit history | Send only recent messages, not the full conversation | 20-40% |
Deployment Checklist
- Store API keys in environment variables, never in code
- Set up rate limiting to prevent abuse
- Add error boundaries around AI components
- Configure maxTokens to control costs
- Set up monitoring for API usage and errors
- Test with streaming disabled to ensure graceful fallbacks
- Add content moderation for user-facing AI features
Course Complete!
Congratulations! You have completed the Next.js + AI course. You can now build, deploy, and scale AI-powered applications with Next.js and the Vercel AI SDK.
← Back to Course OverviewReady to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX