OpenAI Integration Guide
Model selection, streaming, function calling, RAG pipelines, cost control, and production patterns — everything needed to integrate OpenAI into a SaaS product correctly.
How to Integrate OpenAI into a SaaS Product
Integrating OpenAI into a SaaS product means calling the OpenAI API from your backend — passing a prompt and receiving a completion. The key implementation decisions are which model to use, whether to stream the response to the browser, whether to use function calling for structured outputs, and how to control token costs at scale.
In a Next.js SaaS, the OpenAI API is called from Server Actions, API routes, or Server Components — never from Client Components, to keep the API key secure. The Vercel AI SDK simplifies streaming, and pgvector or Pinecone handle the vector database layer for RAG features.
▸ Model Selection
Which OpenAI Model Should You Use?
Model choice is the biggest impact decision — it affects quality, speed, and cost simultaneously.
GPT-4o
Most SaaS AI features — chat, summarisation, extraction, agents
- ✓Best overall capability
- ✓Excellent tool/function calling
- ✓Multimodal (text + images)
- ✓Fast for interactive features
GPT-4o mini
High-volume, simpler tasks — classification, tagging, short summaries
- ✓30x cheaper than GPT-4o
- ✓Fast for bulk operations
- ✓Good for structured extraction
- ✓Cost-effective at scale
o3 / o1
Complex reasoning — code generation, multi-step analysis, math
- ✓Best complex reasoning
- ✓Ideal for code review/generation
- ✓Handles ambiguous multi-step tasks
- ✓Highest accuracy on hard problems
text-embedding-3-small
Semantic search, RAG, similarity matching, recommendation
- ✓1536-dimension vectors
- ✓Lowest cost embedding model
- ✓Excellent semantic accuracy
- ✓Use with pgvector or Pinecone
▸ Integration Patterns
4 OpenAI Integration Patterns — With Production Code
Basic chat completion with Next.js Server Action
The simplest OpenAI integration — a Server Action that calls GPT-4o and returns the response. Keeps the API key server-side and the call type-safe.
// app/actions/ai.ts
'use server'
import OpenAI from 'openai'
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
})
export async function generateSummary(text: string): Promise<string> {
const response = await openai.chat.completions.create({
model: 'gpt-4o',
max_tokens: 500,
messages: [
{
role: 'system',
content: 'You are a precise summariser. Return a 3-sentence summary.',
},
{
role: 'user',
content: text,
},
],
})
return response.choices[0].message.content ?? ''
}Never pass process.env.OPENAI_API_KEY to a Client Component or include it in any variable prefixed with NEXT_PUBLIC_. It will be exposed in the browser bundle.
Streaming response with Vercel AI SDK
For interactive chat features, stream the OpenAI response token-by-token to the browser. The Vercel AI SDK handles streaming, error recovery, and the client-side hook.
// app/api/chat/route.ts
import { openai } from '@ai-sdk/openai'
import { streamText } from 'ai'
export async function POST(req: Request) {
const { messages } = await req.json()
const result = await streamText({
model: openai('gpt-4o'),
system: 'You are a helpful SaaS assistant.',
messages,
maxTokens: 1000,
})
return result.toDataStreamResponse()
}
// components/Chat.tsx (client component)
'use client'
import { useChat } from 'ai/react'
export function Chat() {
const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat()
return (
<div>
{messages.map(m => (
<div key={m.id}>{m.role}: {m.content}</div>
))}
<form onSubmit={handleSubmit}>
<input value={input} onChange={handleInputChange} />
<button type="submit" disabled={isLoading}>Send</button>
</form>
</div>
)
}Without token limits, a single chat session can consume enormous context — and cost. Always set maxTokens and truncate conversation history beyond a set message count.
Function calling / tool use for structured outputs
Define tools that GPT-4o can invoke to extract structured data or trigger actions. Essential for AI agents, form extraction, and any workflow where you need typed data back from the model.
// Structured extraction with tool use
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'user', content: 'Extract the key details from: John Smith, john@acme.com, wants pricing for 50 seats, timeline: Q3 2026' }
],
tools: [
{
type: 'function',
function: {
name: 'extract_lead',
description: 'Extract structured lead information from the message',
parameters: {
type: 'object',
properties: {
name: { type: 'string' },
email: { type: 'string' },
seats: { type: 'number' },
timeline: { type: 'string' },
},
required: ['name', 'email'],
},
},
},
],
tool_choice: 'required',
})
const toolCall = response.choices[0].message.tool_calls?.[0]
const lead = JSON.parse(toolCall?.function.arguments ?? '{}')
// { name: 'John Smith', email: 'john@acme.com', seats: 50, timeline: 'Q3 2026' }Always validate and sanitise the JSON returned by tool calls before using it — the model occasionally returns malformed JSON or omits required fields under high load.
RAG pipeline with embeddings and pgvector
Build a Retrieval-Augmented Generation pipeline that answers questions based on your product's own documents — using OpenAI embeddings and PostgreSQL pgvector for semantic search.
// 1. Generate embedding for a document chunk
async function embedText(text: string): Promise<number[]> {
const response = await openai.embeddings.create({
model: 'text-embedding-3-small',
input: text,
})
return response.data[0].embedding
}
// 2. Store in PostgreSQL with pgvector
await prisma.$executeRaw`
INSERT INTO documents (content, embedding)
VALUES (${chunk}, ${JSON.stringify(embedding)}::vector)
`
// 3. At query time — find semantically similar chunks
async function retrieveContext(query: string): Promise<string[]> {
const queryEmbedding = await embedText(query)
const results = await prisma.$queryRaw<{ content: string }[]>`
SELECT content
FROM documents
ORDER BY embedding <=> ${JSON.stringify(queryEmbedding)}::vector
LIMIT 5
`
return results.map(r => r.content)
}
// 4. Include retrieved context in the GPT-4o prompt
const context = await retrieveContext(userQuery)
const answer = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'system', content: `Answer based only on this context: ${context.join('\n')}` },
{ role: 'user', content: userQuery },
],
})RAG quality depends on chunk size and overlap. Chunks that are too large dilute relevance; too small lose context. 400–600 token chunks with 10–20% overlap is a reliable starting point.
▸ Cost Control
5 Ways to Control OpenAI API Costs
OpenAI costs can surprise teams at scale — these patterns keep bills predictable from day one.
Choose the right model tier
GPT-4o mini costs 30x less than GPT-4o. Route simple tasks — classification, tagging, short extraction — to the mini model and reserve GPT-4o for complex reasoning.
Cache deterministic responses
For prompts that always produce the same output given the same input — document summaries, fixed classifications — cache results in Redis or a database to avoid paying for the same call twice.
Set max_tokens limits
Without a max_tokens limit, the model will generate until it hits the natural end of its response — which can be thousands of tokens for verbose prompts. Always set an appropriate limit per use case.
Truncate conversation history
Each message in a chat history consumes input tokens on every call. Limit history to the last N messages or summarise older messages to keep context window usage predictable.
Monitor usage per user
Track token consumption per user or organisation in the database. Enforce soft and hard quotas at the application layer to prevent a single high-usage customer from generating unexpected API bills.
▸ Why 4Byte
We Ship OpenAI Integrations — Not Just Demos
4Byte has integrated OpenAI into production SaaS products — chat features, document summarisation, lead qualification agents, RAG knowledge bases, and structured extraction pipelines. Every pattern in this guide is from real shipped code, not API documentation walkthroughs.
If you want to add AI to your SaaS product, our free strategy call gives you an honest assessment of what\'s possible and what it will cost.
Currently accepting new AI projects
Free strategy call · Response in ≤ 4 hours · No obligation
▸ FAQ
OpenAI Integration — Common Questions
How do I integrate OpenAI into a Next.js SaaS product?+
Install the openai npm package, store your API key in an environment variable (never expose it client-side), and call the OpenAI API from Next.js Server Components, API routes, or Server Actions. For streaming responses in the browser, use the Vercel AI SDK which wraps the OpenAI API with streaming utilities designed for Next.js.
Which OpenAI model should I use for my SaaS product?+
GPT-4o is the default recommendation for most SaaS AI features in 2026 — it balances capability, speed, and cost better than any other OpenAI model. Use GPT-4o mini for high-volume, lower-complexity tasks where cost matters. Use o1 or o3 for tasks requiring deep multi-step reasoning. Use text-embedding-3-small for generating embeddings for semantic search and RAG.
What is function calling in OpenAI and when should I use it?+
Function calling (now called tool use) lets the OpenAI model invoke predefined functions — such as querying your database, calling an external API, or triggering a workflow — instead of just generating text. It's the foundation of AI agents and structured output extraction. Use it whenever the AI needs to take actions or return data in a specific format rather than free-form text.
How do I stream OpenAI responses to the browser in Next.js?+
Use the Vercel AI SDK's streamText function with the OpenAI provider, combined with a Next.js Route Handler that returns a StreamingTextResponse. On the client side, the useChat hook from the AI SDK handles the streaming connection and progressively renders the response as it arrives.
How do I control OpenAI API costs in production?+
The most impactful cost controls are: choosing the right model tier (GPT-4o mini instead of GPT-4o for simple tasks), caching responses for identical or similar prompts, setting max_tokens limits, truncating conversation history to stay within a context window budget, and monitoring token usage per user to enforce per-user quotas if needed.
What is RAG and how do I build it with OpenAI?+
RAG (Retrieval-Augmented Generation) combines a vector database with OpenAI to answer questions based on custom documents. The process: chunk documents into passages, generate embeddings with text-embedding-3-small, store in pgvector or Pinecone, then at query time retrieve the most semantically similar passages and include them in the GPT-4o prompt as context. 4Byte builds RAG systems for SaaS products that need AI features grounded in their own data.
▸ Continue Learning
Related Technology Stack Resources
Let's Integrate OpenAI Into Your Product Together.
Book a free 30-minute call with 4Byte. We'll review your use case and design the right OpenAI integration for your SaaS — no pressure, no obligation.

AI Expert
Production integrations
4–6 Weeks
Avg. AI feature delivery
No Obligation
Zero pressure call