Back to Articles

AI Content Generation at Scale: GPT-4, DALL-E 3, and Next.js

Complete guide to building an enterprise multi-tenant AI content engine with Next.js App Router, OpenAI GPT-4, DALL-E 3 image generation, and Prisma row-level security.

AI Content Generation at Scale: GPT-4, DALL-E 3, and Next.js

Modern content marketing demands high velocity without sacrificing technical accuracy or visual identity. When engineering an AI content engine for agencies managing multiple client brands, a simple wrapper over the OpenAI API is not enough. You need multi-tenant isolation, streaming completions, automated image synthesis with prompt enrichment, and direct headless CMS publishing pipelines.

In this deep dive, I walk through how I architected a production-ready, multi-tenant AI content generation system using Next.js 15 App Router, OpenAI GPT-4 Turbo, DALL-E 3, and Prisma ORM with PostgreSQL.


High-Level System Architecture

The content pipeline decouples prompt orchestration, structured outline extraction, section synthesis, and media generation into discrete asynchronous phases:

typescript
[User Topic & Brand Voice] │ ▼ [Phase 1: GPT-4 Outline & Keyword Extraction] │ ┌─────┴────────────────────────┐ ▼ ▼ [Phase 2: Section Streaming] [Phase 3: DALL-E 3 Image Generation] │ │ ▼ ▼ [Phase 4: Content Assembly & Markdown Compilation] │ ▼ [Multi-Tenant Database Storage & Webhook Export] (WordPress / Webflow / Shopify REST Endpoints)

1. Multi-Stage Text Generation Pipeline

Generating long-form, 2,000+ word articles directly in a single prompt often results in repetitive prose, hallucinated outlines, and truncated responses. Instead, our pipeline breaks generation into two sequential steps:

  1. Strategic Outline Generation: Identifies search intent, semantic keywords, heading hierarchy (H2/H3), and word count budgets.
  1. Sequential Section Synthesis: Passes the cumulative article outline to GPT-4 while writing each section in isolation. This maintains narrative consistency across the entire document.

Next.js Route Handler for Streaming Article Sections

Here is the core API handler leveraging the official OpenAI SDK and the Web Streams API for real-time progress:

typescript
// app/api/generate/section/route.ts import { NextRequest } from "next/server"; import OpenAI from "openai"; const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY, }); export const runtime = "nodejs"; export async function POST(req: NextRequest) { try { const { sectionTitle, outline, brandTone, targetAudience } = await req.json(); const systemPrompt = \` You are an authoritative, world-class technical copywriter. Brand Tone: \${brandTone || "Authoritative, practical, and data-driven"}. Target Audience: \${targetAudience || "Senior engineers and decision makers"}. Follow these strict instructions: 1. Provide actionable takeaways and concrete implementation details. 2. Avoid generic corporate buzzwords or repetitive introductory clauses. 3. Use formatted markdown with bullet points, callout blocks, and code blocks where relevant. \`; const userPrompt = \` Write a comprehensive, exhaustive section for an article. Full Article Outline: \${outline} Current Section to Write: ## \${sectionTitle} \`; const response = await openai.chat.completions.create({ model: "gpt-4-turbo", stream: true, temperature: 0.7, max_tokens: 1500, messages: [ { role: "system", content: systemPrompt }, { role: "user", content: userPrompt }, ], }); const stream = new ReadableStream({ async start(controller) { const encoder = new TextEncoder(); for await (const chunk of response) { const content = chunk.choices[0]?.delta?.content || ""; if (content) { controller.enqueue(encoder.encode(content)); } } controller.close(); }, }); return new Response(stream, { headers: { "Content-Type": "text/event-stream; charset=utf-8", "Cache-Control": "no-cache, no-transform", }, }); } catch (error: any) { return new Response(JSON.stringify({ error: error.message }), { status: 500, headers: { "Content-Type": "application/json" }, }); } }

2. Automated Visual Synthesis with DALL-E 3

Articles without high-impact visual assets suffer from lower dwell time and poorer engagement metrics. To eliminate manual stock photo curation, the system generates custom header illustrations calibrated to the topic.

Prompt Refinement for Brand Consistency

DALL-E 3 produces significantly better imagery when supplied with detailed art style constraints rather than simple keywords:

typescript
// lib/ai/generateImage.ts import OpenAI from "openai"; const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY }); export async function generateHeaderIllustration(topic: string, stylePreset: string) { const refinedPrompt = \` High-end modern tech editorial illustration depicting "\${topic}". Art style: \${stylePreset || "Isometric 3D glassmorphism with vibrant neon cyan and violet gradients, dark matte background, minimalist UI elements"}. Composition: Horizontal banner format (16:9), clean negative space for typography overlay, professional SaaS marketing visual. No text or misspelled words inside the image. \`; const response = await openai.images.generate({ model: "dall-e-3", prompt: refinedPrompt, n: 1, size: "1792x1024", quality: "hd", response_format: "url", }); const tempUrl = response.data[0]?.url; if (!tempUrl) throw new Error("Image generation failed"); return tempUrl; }

3. Multi-Tenant Architecture with Prisma

Agencies require complete data isolation between their clients. Storing API keys, custom system prompts, brand voice guidelines, and generated drafts under separate workspace tenants ensures compliance and security.

Prisma Schema Definition

prisma
datasource db { provider = "postgresql" url = env("DATABASE_URL") } model Organization { id String @id @default(cuid()) name String slug String @unique openaiKey String? // Optional: Bring-Your-Own-Key per tenant brandTone String @default("Authoritative, engaging, technical") articles Article[] users User[] createdAt DateTime @default(now()) } model Article { id String @id @default(cuid()) organizationId String title String slug String markdown String @db.Text headerImageUrl String? status ArticleStatus @default(DRAFT) seoTitle String? seoDescription String? organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@index([organizationId, status]) } enum ArticleStatus { DRAFT REVIEWING PUBLISHED }

4. Cost Optimization & Performance Metrics

Running GPT-4 Turbo and DALL-E 3 at scale can quickly escalate API expenses if unmonitored. Here are three architectural techniques we implemented to keep unit economics under control:

  1. Model Hierarchy: Use gpt-4o-mini for initial outline extraction and metadata tagging ($0.15 / 1M input tokens), reserving gpt-4-turbo solely for deep content generation.
  1. Aggressive Redis Response Caching: Cache generated topic outlines keyed by prompt hash so duplicate keyword exploration incurs zero token overhead.
  1. Optimized DALL-E Sizing: Generate standard definition drafts during the preview stage and only trigger high-definition (HD) image rendering upon final publication.

Results Delivered:

  • Generation Time: 1,800+ word technical guide generated in under 42 seconds.
  • Cost Reduction: 63% reduction in OpenAI spend compared to unoptimized single-prompt architectures.
  • Publishing Efficiency: 1-click export to WordPress and headless webhooks.

Need a Custom AI Application for Your Business?

I design, build, and deploy custom full-stack AI applications, RAG chatbots, automated content engines, and SaaS dashboards.

👉 [Explore 50+ Shipped Projects](https://www.freelanceayush.com/projects)

👉 [Calculate Your Project Cost (Free Estimator)](https://www.freelanceayush.com/estimator)

👉 [Get 50% OFF — Contact Ayush Kumar](https://www.freelanceayush.com/#contact)