Back to blog
October 23, 2026•64 views

Next.js and AI: Building a Personalized Recommendation Engine with OpenAI Embeddings

Next.jsOpenAI EmbeddingsRecommendation EngineAI PersonalizationpgvectorReactVector Search
Next.js and AI: Building a Personalized Recommendation Engine with OpenAI Embeddings

Most "AI recommendation engine" tutorials stop at cosine similarity in a notebook. That is the easy 20%. The hard part is making it fast, cheap, and correct inside a real Next.js app where a user is waiting 200ms for a page to render.

I have shipped recommendation and ranking features for ecommerce, content platforms, and internal tools. This is the architecture I keep coming back to: a Next.js AI recommendation engine built on OpenAI embeddings, a Postgres vector column, and a caching layer that keeps your OpenAI bill from becoming a line item your CFO emails you about.

Everything below is code you can run. I will also be honest about where this approach breaks down, because the alternatives matter.

Why embeddings instead of a classic recommender

Traditional collaborative filtering needs a lot of interaction data before it says anything useful. If you have 400 users and 90 products, matrix factorization is going to give you noise dressed up as math.

Embeddings flip that. You describe your items in text (title, description, tags, category), convert them to vectors, and now you have semantic similarity from day one. A cold-start catalog works. A brand new user with one click already has a signal.

Where embeddings lose: they cannot learn "people who bought X also bought Y" from behavior alone. That signal has to come from your own data. In practice I run both. Embeddings handle content similarity and cold start, then I blend in a lightweight behavioral score once I have enough events. More on that blend later.

The architecture at a glance

Here is the shape of the system before we write a line of code.

Layer Choice Why
Framework Next.js App Router (14/15) Server components, route handlers, edge caching
Embedding model text-embedding-3-small (1536 dims) Cheap, fast, good enough for most catalogs
Vector store Postgres + pgvector You already have Postgres. One less vendor.
Cache Upstash Redis or Vercel KV Recommendation payloads are read-heavy
Background jobs Route handler + cron, or a queue Embedding generation should not block a request
Hosting Vercel for the app, managed Postgres (Neon/Supabase/RDS) Edge functions plus a real database

If your catalog is under roughly 50,000 items, pgvector on a decent Postgres instance will be fine. Above that, or if you need hybrid keyword plus vector search at scale, look at a dedicated vector database. I have used Pinecone and Qdrant on client work and both are solid. I just do not reach for them by default anymore.

If you are still deciding on the framework itself, my breakdown of Next.js vs Remix vs Astro for AI-enhanced web apps walks through the tradeoffs for exactly this kind of workload.

Step 1: Set up the schema

Assuming Postgres with the vector extension enabled:

CREATE EXTENSION IF NOT EXISTS vector;

CREATE TABLE items (
  id          BIGSERIAL PRIMARY KEY,
  slug        TEXT UNIQUE NOT NULL,
  title       TEXT NOT NULL,
  description TEXT,
  category    TEXT,
  embedding   vector(1536),
  updated_at  TIMESTAMPTZ DEFAULT now()
);

-- HNSW index for fast approximate nearest neighbor search
CREATE INDEX items_embedding_idx
  ON items
  USING hnsw (embedding vector_cosine_ops)
  WITH (m = 16, ef_construction = 64);

Two things worth knowing. First, vector_cosine_ops is the operator class for cosine distance, which is a sensible default for OpenAI embeddings (they are normalized to unit length, so cosine and inner-product rankings coincide). Second, the HNSW index parameters are a real tradeoff: higher m and ef_construction mean better recall and slower index builds. Start where I put them and tune only if you measure a recall problem.

Step 2: Generate embeddings without blocking requests

Never call the OpenAI embeddings API inside a user-facing render. Generate embeddings when content is created or updated, and store them.

// lib/embeddings.ts
import OpenAI from "openai";

const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

export async function embedText(input: string): Promise<number[]> {
  const cleaned = input.replace(/\s+/g, " ").trim().slice(0, 8000);

  const res = await openai.embeddings.create({
    model: "text-embedding-3-small",
    input: cleaned,
  });

  return res.data[0].embedding;
}

export function itemToText(item: {
  title: string;
  description?: string | null;
  category?: string | null;
}) {
  return [item.title, item.category, item.description]
    .filter(Boolean)
    .join(" | ");
}

A few practical notes from production:

  • Batch your calls. The embeddings endpoint accepts an array input. Sending 100 items in one request is dramatically cheaper on latency than 100 requests.
  • Truncate deliberately. I slice to 8000 characters. Longer text rarely improves similarity for short catalog items and you pay per token.
  • Store the model name. When you upgrade from text-embedding-3-small to something else, old and new vectors are not comparable. You will need to re-embed everything. Keep a model column so you know which rows are stale.

Step 3: The recommendation query

This is the core. Given a user's recent activity (a set of item IDs), find items that are semantically close to what they have engaged with, excluding what they have already seen.

WITH seed AS (
  SELECT embedding
  FROM items
  WHERE id = ANY($1::bigint[])
),
centroid AS (
  SELECT AVG(embedding) AS embedding FROM seed
)
SELECT
  i.id,
  i.slug,
  i.title,
  1 - (i.embedding <=> c.embedding) AS similarity
FROM items i, centroid c
WHERE i.id <> ALL($1::bigint[])
  AND i.embedding IS NOT NULL
ORDER BY i.embedding <=> c.embedding
LIMIT $2;

The trick here is the centroid. Instead of comparing against every item the user touched, I average their embeddings into a single vector that represents their taste. Then I rank the whole catalog against that one vector. One query, one index scan.

The <=> operator is cosine distance in pgvector. 1 - distance gives you a similarity score you can display or threshold.

Blending in behavioral signal

Pure semantic similarity will happily recommend three near-identical products. That is bad UX. I add a small re-rank after the vector search:

type Candidate = {
  id: number;
  slug: string;
  title: string;
  similarity: number;
  popularity: number; // 0..1 normalized from views/purchases
};

export function rerank(candidates: Candidate[], weight = 0.25) {
  return candidates
    .map((c) => ({
      ...c,
      score: c.similarity * (1 - weight) + c.popularity * weight,
    }))
    .sort((a, b) => b.score - a.score);
}

Keep the behavioral weight low at first, maybe 0.15 to 0.25. If you crank it up too early, you just rebuild a popularity list and personalization disappears.

Step 4: Wire it into a Next.js route handler

I keep the recommendation logic in a route handler so it can be cached independently of the page, and so a client component can refresh it without a full navigation.

// app/api/recommendations/route.ts
import { NextRequest, NextResponse } from "next/server";
import { sql } from "@/lib/db";

export const runtime = "nodejs";
export const revalidate = 300;

export async function GET(req: NextRequest) {
  const userId = req.nextUrl.searchParams.get("userId");
  if (!userId) {
    return NextResponse.json({ error: "missing userId" }, { status: 400 });
  }

  const recent = await sql<{ item_id: number }[]>`
    SELECT item_id FROM events
    WHERE user_id = ${userId}
    ORDER BY created_at DESC
    LIMIT 10
  `;

  if (recent.length === 0) {
    const fallback = await sql`
      SELECT id, slug, title FROM items
      ORDER BY popularity_score DESC
      LIMIT 8
    `;
    return NextResponse.json({ items: fallback, source: "popular" });
  }

  const seedIds = recent.map((r) => r.item_id);

  const rows = await sql`
    WITH seed AS (
      SELECT embedding FROM items WHERE id = ANY(${seedIds})
    ),
    centroid AS (SELECT AVG(embedding) AS embedding FROM seed)
    SELECT i.id, i.slug, i.title,
           1 - (i.embedding <=> c.embedding) AS similarity
    FROM items i, centroid c
    WHERE i.id <> ALL(${seedIds})
      AND i.embedding IS NOT NULL
    ORDER BY i.embedding <=> c.embedding
    LIMIT 20
  `;

  return NextResponse.json({ items: rows, source: "personalized" });
}

Notice the fallback path. New users get popular items. That is not a cop-out, it is the correct behavior, and it also protects you from an empty centroid when the seed set is tiny.

Step 5: Caching, because this query is expensive

A vector search over 50k rows is not free. Doing it on every page view for every user will show up in your Postgres metrics within a week.

My caching strategy, in order of impact:

  1. Cache per user for 5 to 15 minutes. Recommendation sets do not need to be real-time. A short TTL cuts the vast majority of duplicate work.
  2. Cache the popular fallback separately with a longer TTL. It changes slowly.
  3. Precompute for high-traffic users. If you have identifiable power users, run a cron job that refreshes their recommendations every 30 minutes and serve straight from cache.
  4. Use revalidate and cache tags on the route so a content update can invalidate cleanly.
// lib/cache.ts
import { Redis } from "@upstash/redis";

const redis = Redis.fromEnv();
const TTL = 60 * 10;

export async function cachedRecommendations<T>(
  userId: string,
  compute: () => Promise<T>
): Promise<T> {
  const key = `recs:${userId}`;
  const hit = await redis.get<T>(key);
  if (hit) return hit;

  const fresh = await compute();
  await redis.set(key, fresh, { ex: TTL });
  return fresh;
}

If you only remember one thing from this article: the embedding call is not your bottleneck. The vector search plus the database round trip is. Cache accordingly.

For a deeper look at how Next.js handles caching across the App Router, including revalidate and cache tags, see my Next.js 15 caching guide.

Cost math you should actually do

People ask me if this is expensive. Here is the honest breakdown for text-embedding-3-small.

Scenario Volume Rough cost profile
Initial catalog embed 10,000 items One-time, small. Cents to low dollars.
Daily new/updated content 200 items/day Negligible.
Query-time embeddings Avoid entirely Cache the centroid or precompute it.
Vector search Every uncached request Your real cost is Postgres compute, not OpenAI.

The mistake I see is embedding on every request. Do not. Embed at write time, search at read time. That single decision is the difference between a $20/month feature and a $400/month feature.

If you want to see this pattern applied to a real business problem, my AI automation case study shows how a small team cut 16 hours a week by embedding intelligence into existing workflows rather than bolting on a new tool.

Where this architecture breaks

I want to be straight with you, because a lot of tutorials pretend their approach has no ceiling.

  • Under ~1,000 items, you barely need a vector database. A Postgres tsvector search plus some tags may be simpler and good enough.
  • Very large catalogs (millions of items) need a dedicated vector store with sharding and quantization. pgvector will start to hurt.
  • Real-time behavioral personalization needs a streaming pipeline (Kafka, or a managed equivalent). Embeddings alone will not capture it.
  • Heavy multimodal content (video, audio) needs a different embedding model. Text embeddings on video metadata only gets you so far.

If your product is a content site or a mid-size store, the architecture above is more than sufficient. Do not over-engineer it.

Deploying and monitoring

Ship the app on Vercel and the database on Neon, Supabase, or RDS. Keep the embedding generation in a background route triggered by your CMS webhook, not in the request path.

For monitoring, track three numbers:

  • Cache hit rate on recommendations. Below 70% means your TTL is too short or your keys are wrong.
  • p95 latency on the recommendations route. If it climbs past 300ms, index or cache problem.
  • Click-through on recommended items versus your fallback. If personalized is not beating popular, something is broken in your seed logic.

Run an A/B test before you trust any of it. I have seen "obviously better" personalization lose to a simple bestseller list more than once, usually because the seed signal was noisy.

A quick note on hiring for this

If you are a founder reading this and thinking "I want this but I do not want to build it", that is a reasonable position. This is a two to four week feature for someone who has done it before, and considerably longer for someone learning vector search and Next.js caching on your dime.

When you evaluate a developer for this kind of work, ask them how they handle the cold-start case, how they cache, and what their embedding refresh strategy is. Anyone who has actually shipped one will answer those three questions without hesitation. If you want to talk through your project, get in touch and I will tell you honestly whether you need this or something simpler.

If you are hiring rather than building, my guide on how to hire an automation developer in 2026 covers the exact skills and interview questions that separate people who have shipped this from people who have only read about it.

FAQ

Q: Do I need a dedicated vector database like Pinecone for a Next.js recommendation engine?

A: For most apps, no. Postgres with pgvector handles catalogs into the tens of thousands of items comfortably, and it means one less service to operate. Reach for Pinecone, Qdrant, or Weaviate when you outgrow pgvector's index performance, need heavy hybrid search, or are operating at millions of vectors.

Q: How much does it cost to run OpenAI embeddings for recommendations?

A: The embedding generation itself is cheap with text-embedding-3-small. The real cost is your database compute for vector search and any query-time embedding you fail to cache. Embed at write time, cache recommendation payloads per user, and the feature stays inexpensive.

Q: Can I do this with the Next.js App Router and React Server Components?

A: Yes, and it works well. Put the recommendation query in a route handler or a server function, cache it, and stream the results into a server component. Client components fetch from the route handler when you need to refresh without a full page navigation.

Q: How do I handle brand new users with no history?

A: Serve a popularity or editorial fallback, then start blending in personalization as soon as the user has two or three interactions. Do not try to personalize from zero signals. The fallback is not a failure state, it is part of the design.

Frequently asked questions

Do I need a dedicated vector database like Pinecone for a Next.js recommendation engine?

For most apps, no. Postgres with pgvector handles catalogs into the tens of thousands of items comfortably and means one less service to operate. Reach for Pinecone, Qdrant, or Weaviate when you outgrow pgvector's index performance, need heavy hybrid search, or are operating at millions of vectors.

How much does it cost to run OpenAI embeddings for recommendations?

The embedding generation with text-embedding-3-small is cheap. The real cost is database compute for vector search and any query-time embedding you fail to cache. Embed at write time, cache recommendation payloads per user, and the feature stays inexpensive.

Can I build this with the Next.js App Router and React Server Components?

Yes. Put the recommendation query in a route handler or server function, cache it, and stream results into a server component. Client components fetch from the route handler when you need to refresh without a full page navigation.

How do I handle brand new users with no history?

Serve a popularity or editorial fallback, then blend in personalization once the user has two or three interactions. Do not try to personalize from zero signals. The fallback is part of the design, not a failure state.

Share this post

Native Web Share is available on mobile, so that share sheet includes Instagram and any other app you have installed.

Let's Connect

Get In Touch

Let's build something premium together.

Contact info

Follow me
Chat on WhatsApp