Intelligence Augmented: Integrating OpenAI and Streaming Responses in Next.js
Karl Gusta
Instructor & Founder
The AI Frontier: Adding Brains to Your SaaS
We are living in the era of the "Intelligent SaaS." Users no longer want just a place to store data; they want a partner that helps them process it. Whether it is an automated SEO auditor, an AI powered CRM, or a document summarizer, adding Large Language Models (LLMs) to your stack is the fastest way to increase the perceived value of your application.
However, AI integration brings a new set of technical challenges. LLMs are slow. A high-quality response can take 10 to 30 seconds to generate. If your UI just shows a loading spinner for that long, the user will think the app is broken. Furthermore, AI is expensive. Without proper usage limits and caching, a single viral user could cost you hundreds of dollars in API fees.
In this lesson, we are going to bridge the gap between your Next.js backend and OpenAI. We will implement real-time streaming to make your UI feel instantaneous and build a robust token-tracking system to protect your margins.
The Problem: The Latency Bottleneck
The standard request-response cycle is the enemy of a good AI experience. If you wait for the entire AI response to finish before sending it to the client, the "Time to First Token" will be unacceptably high.
Moreover, managing AI prompts in a scalable way is difficult. Hardcoding prompts in your Server Actions makes them hard to test and iterate on. We need a way to combine your user's data from MongoDB with a system prompt and send it securely to OpenAI.
The Shift: Streaming and Edge Functions
The shift is moving from "Waiting for the result" to "Streaming the process." Using the Vercel AI SDK and OpenAI's streaming API, we can begin showing the user the AI's response as it is being generated, character by character.
To handle these long-running requests efficiently, we will use Next.js Edge Functions. Edge Functions are lighter than standard Serverless Functions and can stay open for the duration of a stream without the same cold-start or timeout penalties.
Learn Full Stack SaaS Development discusses how this real-time feel is essential for modern user expectations.
Deep Dive: The AI Workflow
1. The Secure API Route
You must never call OpenAI directly from the frontend. This would expose your API key to the world. Instead, we create an Edge Route in Next.js that acts as a secure proxy.
The Workflow:
- The frontend sends a prompt to
/api/chat. - The route verifies the user's session and subscription status.
- The route fetches any relevant context from MongoDB (e.g., the user's previous notes).
- The route calls OpenAI and streams the result back.
2. Implementing Streaming with the AI SDK
The Vercel AI SDK simplifies the complex logic of handling readable streams in React. It provides a useChat hook that manages the message history, loading states, and the stream itself.
typescript// app/api/chat/route.ts import { OpenAIStream, StreamingTextResponse } from 'ai'; import OpenAI from 'openai'; export const runtime = 'edge'; const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY }); export async function POST(req: Request) { const { messages } = await req.json(); const response = await openai.chat.completions.create({ model: 'gpt-4-turbo', stream: true, messages, }); const stream = OpenAIStream(response); return new StreamingTextResponse(stream); }
3. Usage Tracking and Token Limits
To make your AI SaaS profitable, you must track usage. OpenAI charges per token, so you should too.
The Reasoning:
After a stream completes, you can use the onCompletion callback in the AI SDK to save the final response to MongoDB and update the user's "Token Balance." If a user's balance hits zero, your middleware should block further calls to the /api/chat route until they upgrade or their month resets.
4. Prompt Engineering and Context Injection
The quality of your AI features depends on the context you provide. Instead of just sending the user's prompt, you should inject "System Instructions" and "Relevant Data."
For example, if you are building an AI Email Assistant, you would fetch the last five emails from MongoDB and inject them into the prompt as "Context" so the AI knows the history of the conversation.
Key Benefits and Learning Outcomes
- Premium Feel: Streaming makes your app feel alive and significantly faster than competitors.
- Profitability: Token tracking ensures you never spend more on API fees than you earn in subscriptions.
- Security: Edge functions keep your keys safe and your logic hidden.
- Scalability: By using Edge runtime, your AI features can handle massive traffic without hitting standard serverless timeouts.
Common Mistakes to Avoid
- Hardcoding API Keys: Always use environment variables. A leaked OpenAI key can be drained of thousands of dollars in minutes.
- Missing Error Boundaries: AI models occasionally go down or return errors. Ensure your UI has a "Try Again" state.
- Long System Prompts: Every word in your system prompt costs tokens. Keep your instructions concise and focused.
- No Rate Limiting: Even with a paid plan, implement Redis rate limiting on your AI routes to prevent accidental loops or malicious bot activity.

Pro Tips and Best Practices
- The "Stop" Button: Always provide a way for users to stop a long AI generation. This saves you tokens and gives the user control.
- Streaming UI Components: Don't just show text. Use the stream to render markdown, code blocks, or even DaisyUI components in real-time.
- Semantic Search: Combine OpenAI with MongoDB Atlas Vector Search. This allows you to find "Similar" documents based on meaning, rather than just keywords, providing much better context to the AI.
- Fallback Models: If GPT-4 is slow or down, have your code automatically fall back to GPT-3.5 or a local Llama model to maintain service.
How This Fits Into the Zero to SaaS Journey
We have built a professional, multi-tenant, billing-ready SaaS. Now, we are adding the "Magic." In the Zero to SaaS journey, AI integration is the differentiator. It's the feature that moves you from a commodity tool to a specialized solution.
By following this workflow, you aren't just "calling an API"; you are building a secure, scalable, and profitable AI business.
Real-World Use Case: The AI Legal Assistant
Imagine a SaaS that helps lawyers summarize long contracts.
- The Flow: The lawyer uploads a PDF (stored in MongoDB).
- The Process: A Server Action extracts the text, sends it to an Edge Route with a system prompt: "You are a senior legal counsel. Summarize the following contract risks."
- The UI: The summary streams onto the screen. A DaisyUI "Stat" component updates in the corner to show the lawyer how many tokens they have left for the month.

Action Plan: What to Build Next
Add AI to your stack today:
- Secure: Get your OpenAI API key and add it to your Vercel project settings.
- Route: Create a basic Edge API route for streaming text.
- Frontend: Use the
useChathook to build a simple chat interface in your dashboard. - Limit: Add a
tokenUsagefield to your User document in MongoDB. - Refine: Implement a "System Prompt" that gives your AI a specific personality or domain expertise.
The future of SaaS is intelligent.
Building the Next Generation of Software
You have the foundation of a modern SaaS. By adding AI, you are positioning yourself at the forefront of the industry. The combination of Next.js speed, MongoDB flexibility, and OpenAI intelligence is a formidable stack for any founder.
If you want to see a full implementation of an "AI Content Engine" with vector search and automated billing, join our advanced AI modules.
Ready to grow your audience? Check out the Zero to SaaS Next.js Launch Guide and learn how to market your new AI features to the world.