MODULE 14  ยท  Real-World Architectures

How Recommendation Systems Use AI

You open Netflix. The perfect show is already on your home screen. That's not luck โ€” it's a recommendation engine powered by AI.

๐Ÿ“… Mar 2026
โฑ 10 min read
๐ŸŽฏ Episode 97 of 98
Recommendation SystemsCollaborative FilteringMLPersonalization
In this episode

You open Netflix. The perfect show is already on your home screen.

You open YouTube. A video you've never heard of is exactly what you wanted.

You open Spotify. The "Discover Weekly" playlist feels like it read your mind.

This isn't coincidence. It's recommendation systems โ€” AI that knows what you want before you do.

And they're everywhere. 35% of Amazon purchases. 70% of Netflix viewing time. 40% of YouTube watch time. All from recommendations.

Let's understand how they work.

The Goal: Predict Preference

At its core, recommendation is a prediction problem:

Given: User U, Item I, Context C

Predict: How much will U like I in context C?

The answer is a score. Rank all items by score. Show the top N.

โšก

Approach 1: Collaborative Filtering

The Insight: People who agreed in the past will agree in the future.

User-User Collaborative Filtering

You liked: {Inception, Interstellar, The Matrix}

User X liked: {Inception, Interstellar, The Matrix, Tenet}

example
code
โ†‘
                    Similar taste!

Recommend: Tenet

python

from sklearn.metrics.pairwise import cosine_similarity

User-item matrix (1 = liked, 0 = not seen)

snippet
code
user_item_matrix = [
[1, 1, 1, 0],  # You
    [1, 1, 1, 1],  # User X
    [1, 0, 1, 0],  # User Y

]

Find similar users

snippet
code
similarities = cosine_similarity([user_item_matrix[0]], user_item_matrix[1:])
similarities = [0.87, 0.82] - User X is most similar

Item-Item Collaborative Filtering

People who bought X also bought Y.

Bought phone case โ†’ Also bought screen protector

Watched Inception โ†’ Also watched Interstellar

More scalable than user-user. Items are fewer than users. Item relationships are more stable.

The Cold Start Problem

Collaborative filtering fails for new users and new items:

New user: No history to find similar users

New item: No interactions to find similar items

Solutions:

Ask preferences during onboarding

Use content-based features for new items

Default to popular items temporarily

๐Ÿ”ง

Approach 2: Content-Based Filtering

The Insight: Recommend items similar to what you liked before.

Feature Extraction

For movies:

python

snippet
code
movie_features = {
"genre": ["sci-fi", "action"],
    "director": "Christopher Nolan",
    "actors": ["Leonardo DiCaprio"],
    "year": 2010,
    "keywords": ["dream", "subconscious", "heist"]

}

For products:

python

snippet
code
product_features = {
"category": "electronics",
    "brand": "Sony",
    "price": 299.99,
    "attributes": ["wireless", "noise-cancelling", "bluetooth"]

}

Similarity Scoring

python

from sklearn.feature_extraction import DictVectorizer

from sklearn.metrics.pairwise import cosine_similarity

Vectorize features

snippet
code
vectorizer = DictVectorizer()
movie_vectors = vectorizer.fit_transform([movie1_features, movie2_features])

Calculate similarity

snippet
code
similarity = cosine_similarity(movie_vectors[0:1], movie_vectors[1:2])

High similarity = recommend

๐Ÿ“Š

Approach 3: Embeddings (The Modern Way)

See Episode 15 for how embeddings work.

User and Item Embeddings

Represent users and items in the same vector space:

User embedding: [0.2, -0.5, 0.8, ...] # 128-512 dimensions

Movie embedding: [0.3, -0.4, 0.7, ...]

snippet
code
Similarity = dot_product(user_emb, movie_emb)

Learning Embeddings

Matrix Factorization:

python

import numpy as np

Decompose user-item matrix into user and item embeddings

R โ‰ˆ U ร— I^T

R: user-item matrix (n_users ร— n_items)

U: user embeddings (n_users ร— n_factors)

I: item embeddings (n_items ร— n_factors)

from sklearn.decomposition import TruncatedSVD

snippet
code
svd = TruncatedSVD(n_components=50)
user_embeddings = svd.fit_transform(user_item_matrix)
item_embeddings = svd.components_.T

Predict rating

snippet
code
predicted = np.dot(user_embeddings[user_id], item_embeddings[item_id])

Neural Collaborative Filtering:

python

import torch.nn as nn

class NCF(nn.Module):

example
code
def __init__(self, n_users, n_items, embedding_dim):
        super().__init__()
        self.user_embedding = nn.Embedding(n_users, embedding_dim)
        self.item_embedding = nn.Embedding(n_items, embedding_dim)
        self.fc = nn.Sequential(
            nn.Linear(embedding_dim * 2, 128),
            nn.ReLU(),
            nn.Linear(128, 1),
            nn.Sigmoid()
        )
example
code
def forward(self, user_ids, item_ids):
        user_emb = self.user_embedding(user_ids)
        item_emb = self.item_embedding(item_ids)
        concat = torch.cat([user_emb, item_emb], dim=1)
        return self.fc(concat)

Two-Tower Architecture (Production Scale)

Used by YouTube, Netflix, and everyone at scale:

snippet
code
User Features โ†’ User Tower โ†’ User Embedding (64-256 dims)
โ†“
                                        Dot Product โ†’ Score
                                               โ†‘
Item Features โ†’ Item Tower โ†’ Item Embedding (64-256 dims)

Why Two Towers?

Pre-compute item embeddings (millions of items)

At serving time, just encode user + dot product

Use approximate nearest neighbors (ANN) for fast retrieval (see Episode 17)

๐Ÿ’ก

Sequence-Aware Recommendations

Your recent history matters more than your history from 5 years ago.

Session-Based Recommendations

python

Transformer-based session model

class SessionRec(nn.Module):

example
code
def __init__(self, n_items, d_model=128):
        super().__init__()
        self.embedding = nn.Embedding(n_items, d_model)
        self.transformer = nn.TransformerEncoder(
            nn.TransformerEncoderLayer(d_model, nhead=4),
            num_layers=2
        )
        self.output = nn.Linear(d_model, n_items)
example
code
def forward(self, session_items):
        # session_items: [batch, seq_len] - item IDs in sequence
        emb = self.embedding(session_items)
        encoded = self.transformer(emb)
        # Predict next item
        return self.output(encoded[:, -1, :])

๐Ÿ”ฌ

Multi-Objective Optimization

Real systems optimize for more than just clicks:

Objective Why It Matters

Click-through rate Immediate engagement

Watch timeQuality of engagement (YouTube)
ConversionPurchase (Amazon)
RetentionCome back tomorrow
DiversityDon't show the same thing repeatedly
FreshnessSurface new content

Pareto Frontier

There's a tradeoff between exploration (new items) and exploitation (known preferences). Too much exploration: users bounce. Too much exploitation: echo chamber.

๐Ÿ›ก๏ธ

The Netflix Example

Netflix's recommendation pipeline:

  1. Candidate Generation (1000s of titles)
  1. Ranking (100s of titles)
  1. Re-ranking (Top 10)

๐Ÿ“ฆ

Evaluation Metrics

Metric Measures Good For

Precision@KOf top K, how many relevant?Quality of recommendations
Recall@KOf all relevant, how many in top K?Coverage
NDCGRanking quality (higher = better position)Order matters
MAPMean average precisionOverall performance
DiversityHow different are recommendations?Avoid filter bubbles
Coverage% of catalog recommendedLong-tail items

๐Ÿš€

Practical Takeaways

Collaborative filtering = wisdom of crowds โ€” Users similar to you liked this

snippet
code
Content-based = match features โ€” Items similar to what you liked

Embeddings unify both โ€” Same vector space for users and items

Two-tower architectures scale โ€” Pre-compute items, fast serving

Sequence matters โ€” Recent behavior > old behavior

Multiple objectives โ€” CTR, watch time, retention, diversity

Cold start is hard โ€” New users and items need special handling

๐Ÿ”„

What's Next?

Episode 98: Building Your Own AI SaaS โ€” The grand finale. Full architecture walkthrough: auth, billing, model serving, scaling. Everything we've learned, tied together.

โ† Previous

Ep 96: How Voice Assistants Work

Next โ†’

Ep 98: Building Your Own AI SaaS

Next: Episode 98 โ€” Building Your Own AI SaaS

You've made it. 98 episodes. Now let's put it all together and build a production AI SaaS from scratch.

This is part of a 98-episode series covering AI engineering from tokens to production deployment.

โ† Previous Ep 96: How Voice Assistants Work: STT โ†’ LLM โ†’ TTS Pipeline