Every SaaS founder I talk to wants to add AI. Most of them are thinking about it wrong. They want a chat bubble in the corner that answers questions about their product. What users actually want is AI that reduces the work they came to the app to do — generating the first draft, summarizing the imported document, extracting the data from the uploaded file, writing the email. The difference is whether AI is a feature or a workflow accelerator. This guide covers how I integrate both patterns into Next.js SaaS applications using the Vercel AI SDK and OpenAI.
TL;DR — Use the Vercel AI SDK for streaming chat in Next.js. Add AI where it reduces the user's core workflow, not just as a sidebar chatbot. The patterns here cover streaming, document AI, and rate limiting.
The Vercel AI SDK vs. Direct OpenAI Client
You could call the OpenAI SDK directly from a Next.js API route. The reason to use the Vercel AI SDK instead: it solves streaming responses in Next.js properly. Streaming a long LLM response over HTTP requires Server-Sent Events or a streaming ReadableStream, and the Vercel AI SDK handles this correctly across Edge Runtime, Node.js runtime, and React Server Components. It also provides useChat and useCompletion hooks that handle the client-side state management.
npm install ai @ai-sdk/openaiStreaming Chat Endpoint
// app/api/chat/route.ts
import { openai } from "@ai-sdk/openai"
import { streamText } from "ai"
import { auth } from "@/lib/auth"
import { rateLimit } from "@/lib/rate-limit"
export const runtime = "edge"
export async function POST(req: Request) {
const session = await auth()
if (!session?.user) {
return new Response("Unauthorized", { status: 401 })
}
// Rate limit: 20 messages per user per minute
const { success } = await rateLimit.limit(session.user.id)
if (!success) {
return new Response("Too many requests", { status: 429 })
}
const { messages } = await req.json()
const result = streamText({
model: openai("gpt-4o"),
system: `You are an assistant for ${PRODUCT_NAME}.
You help users with their ${PRODUCT_DOMAIN} tasks.
Today's date is ${new Date().toISOString().split("T")[0]}.
User: ${session.user.name} (${session.user.email})`,
messages,
maxTokens: 1000,
})
return result.toDataStreamResponse()
}Client-Side Chat with useChat
// components/ai-chat.tsx
"use client"
import { useChat } from "ai/react"
import { useState } from "react"
export function AIChat() {
const { messages, input, handleInputChange, handleSubmit, isLoading, error } = useChat({
api: "/api/chat",
onError: (err) => {
if (err.message.includes("429")) {
setRateLimitHit(true)
}
},
})
const [rateLimitHit, setRateLimitHit] = useState(false)
return (
<div className="flex flex-col h-full">
<div className="flex-1 overflow-y-auto space-y-4 p-4">
{messages.map((m) => (
<div key={m.id} className={`flex ${m.role === "user" ? "justify-end" : "justify-start"}`}>
<div className={`max-w-[80%] rounded-lg px-4 py-2 text-sm ${
m.role === "user"
? "bg-zinc-900 text-white"
: "bg-zinc-100 dark:bg-zinc-800 text-zinc-900 dark:text-white"
}`}>
{m.content}
</div>
</div>
))}
{isLoading && (
<div className="flex justify-start">
<div className="bg-zinc-100 dark:bg-zinc-800 rounded-lg px-4 py-2 text-sm text-zinc-500">
<span className="animate-pulse">Thinking...</span>
</div>
</div>
)}
</div>
{rateLimitHit && (
<div className="px-4 py-2 text-xs text-amber-600 bg-amber-50 border-t border-amber-200">
Slow down — you have hit the rate limit. Try again in a moment.
</div>
)}
<form onSubmit={handleSubmit} className="border-t p-4 flex gap-2">
<input
value={input}
onChange={handleInputChange}
placeholder="Ask anything..."
className="flex-1 rounded-lg border px-3 py-2 text-sm outline-none focus:ring-2 focus:ring-zinc-900"
disabled={isLoading}
/>
<button
type="submit"
disabled={isLoading || !input.trim()}
className="px-4 py-2 bg-zinc-900 text-white text-sm rounded-lg disabled:opacity-50"
>
Send
</button>
</form>
</div>
)
}Document Summarization: The High-Value Feature
For SaaS apps with document uploads, AI summarization is one of the highest-retention features you can add. The pattern: user uploads a file → extract text server-side → stream a summary back. Here is the full implementation:
// app/api/summarize/route.ts
import { openai } from "@ai-sdk/openai"
import { streamText } from "ai"
import { auth } from "@/lib/auth"
import pdf from "pdf-parse"
export async function POST(req: Request) {
const session = await auth()
if (!session?.user) return new Response("Unauthorized", { status: 401 })
const formData = await req.formData()
const file = formData.get("file") as File
if (!file) return new Response("No file", { status: 400 })
if (file.size > 10 * 1024 * 1024) return new Response("File too large (max 10MB)", { status: 413 })
const buffer = Buffer.from(await file.arrayBuffer())
let text: string
if (file.type === "application/pdf") {
const parsed = await pdf(buffer)
text = parsed.text.slice(0, 40000) // Budget tokens — first ~30 pages
} else if (file.type === "text/plain") {
text = buffer.toString("utf-8").slice(0, 40000)
} else {
return new Response("Unsupported file type", { status: 415 })
}
const result = streamText({
model: openai("gpt-4o"),
messages: [
{
role: "user",
content: `Summarize the following document. Structure your response as:
**Executive Summary** (2-3 sentences)
**Key Points** (bulleted list, 5-7 items)
**Action Items** (if any are explicit in the document)
Document:
${text}`,
},
],
maxTokens: 800,
})
return result.toDataStreamResponse()
}Rate Limiting AI Endpoints
AI endpoints are expensive per call. Without rate limiting, a single abusive user can cost you hundreds of dollars in one night. I use Upstash Redis with the @upstash/ratelimit package because it works on Edge Runtime and has sliding window support:
// lib/rate-limit.ts
import { Ratelimit } from "@upstash/ratelimit"
import { Redis } from "@upstash/redis"
export const rateLimit = new Ratelimit({
redis: Redis.fromEnv(),
limiter: Ratelimit.slidingWindow(20, "1 m"), // 20 requests per minute
analytics: true,
prefix: "ai_ratelimit",
})
// Per-plan limits: upgrade your rate limit based on user plan
export function getRateLimitForPlan(plan: "free" | "pro" | "enterprise") {
const limits = {
free: Ratelimit.slidingWindow(10, "1 m"),
pro: Ratelimit.slidingWindow(60, "1 m"),
enterprise: Ratelimit.slidingWindow(300, "1 m"),
}
return new Ratelimit({
redis: Redis.fromEnv(),
limiter: limits[plan],
prefix: `ai_ratelimit:${plan}`,
})
}Logging AI Usage for Cost Tracking
You need to know which users are consuming AI tokens. The Vercel AI SDK returns usage data in the onFinish callback of streamText. Log this to your database to build a usage dashboard:
const result = streamText({
model: openai("gpt-4o"),
messages,
onFinish: async ({ usage, finishReason }) => {
await db.insert(aiUsageLogs).values({
userId: session.user.id,
feature: "chat",
promptTokens: usage.promptTokens,
completionTokens: usage.completionTokens,
totalTokens: usage.totalTokens,
finishReason,
createdAt: new Date(),
})
},
})Building a SaaS and want AI features that users actually pay for? I integrate LLM features into Next.js apps with streaming, rate limiting, and cost controls. See prayagbagthariya.vercel.app/hire-me.
Frequently Asked Questions
Should I use the Vercel AI SDK or call OpenAI directly in Next.js?
Use the Vercel AI SDK for any feature involving streaming responses. It handles the ReadableStream → Server-Sent Events plumbing correctly across Edge Runtime and Node.js, provides useChat and useCompletion React hooks that manage client-side state, and abstracts over multiple model providers so you can switch from OpenAI to Anthropic without rewriting your API routes. Call OpenAI directly only for batch processing jobs (cron-based summarization, background enrichment) where streaming is not needed.
How do I handle OpenAI errors in a Next.js streaming endpoint?
With streamText, errors that occur during streaming are harder to surface than non-streaming errors because the response headers are already sent. Use the onError callback in streamText to log the error server-side. On the client side, the useChat hook's onError callback receives a structured error you can display to the user. For 429 rate limit errors from OpenAI, add retry logic with exponential backoff in your API route before streaming begins.
How do I add context about the user's data to an AI chat in my SaaS?
Include a system prompt that contains relevant user context: their name, account type, current page/feature, and any data the AI needs to be helpful. For data-heavy context (like 'here are the user's last 10 transactions'), summarize or filter it before including — do not dump raw database records into the system prompt. For large context needs (like letting the AI reason over the user's full document library), use RAG to retrieve relevant chunks rather than including everything.
What is the best way to prevent prompt injection attacks in a SaaS AI chat?
Never trust user-provided strings to form your system prompt. Keep the system prompt entirely server-side and server-controlled. Sanitize user messages before including them in any context you pass to the model. Set a maxTokens limit so users cannot force the model to generate arbitrarily long outputs. Use output parsing and validation for any AI response that is used programmatically (like JSON extraction) — never eval() or directly execute AI-generated code without sandboxing.
How should I structure AI feature access across free and paid plans in a SaaS?
Enforce plan limits at the API route level, not just in the UI. Check the user's plan on every request and apply appropriate rate limits (e.g., 10 AI calls/minute on free, 60 on pro). Track usage in a database table with userId, feature, and timestamp. Show users their remaining AI credits in the UI and gate premium features (like document summarization or GPT-4o access) behind the paid plan. Use the logged usage data to optimize which AI features drive conversions from free to paid.
Continue reading

Why "Vibe Coding" Feels Like Flying Until You Hit Production: A Brutally Honest Field Guide for 2026

Why Insurtech Still Runs on Java: What Building US Retirement Engines Taught Me About Architecture, Rust, and Why India Got It Right
Get updates directly in your Google Search feed
Add this publication as a preferred source to highlight articles in Top Stories and AI Overviews.
