KnowML

Designing YouTube Recommendations

Follow Arjun's evening on YouTube down to the machinery that picks a dozen videos for him out of billions. We build it from the obvious version, fix each failure in turn, and look at the choices YouTube published along the way, including the ones it later changed.

⏱ 66 min readAssumes: recommenders (16), reinforcement learning (15)
Start reading

Arjun is lying in bed in Pune a little before ten at night, holding his phone above his face. He has just finished a seven-minute video on fixing a slipped bicycle chain, and a small countdown on the screen says the next video will start in five seconds. He doesn't touch anything. It starts, a different channel but the same sort of repair, and he watches that too. Afterwards he goes back to the home screen, where a dozen thumbnails are already waiting: a cricket highlight, a Marathi cooking video, a long talk about monsoon cloud formation, and a few others he hadn't asked for. He taps the one about clouds.

Behind those dozen thumbnails there were billions of candidates, and the choice had to be made in a fraction of a second, for him in particular, using nothing but what he had watched before. There's a second problem hiding in the scene. This system can't see what Arjun wanted. It can see that he tapped, that he stayed for four minutes, that he didn't press dislike, and from those traces it has to guess. Software that makes this kind of guess, picking a few items from a huge catalogue for one particular person, is a recommender system, and the catalogue it picks from is the corpus. That guess matters more than it first seems, because whatever the system learns to reward, millions of people who make videos will learn to produce.

So the question this chapter answers is: how does YouTube choose a dozen videos for one person out of billions, in a fraction of a second, when it can only measure what people do and not what they wanted?

We'll build the design the way an engineer would, from the most obvious version, "show what's popular and what's similar", and fix each failure in turn. That takes us through candidate generation framed as a classification problem with millions of classes, a two-tower retrieval model and the correction that makes its training honest, nearest-neighbour search, and a ranker that predicts watch time. After that come the multitask model that separates what people do from what they value, the history of YouTube changing its objective, and the feedback loop in which every model's choices become the next model's training data. We use YouTube's own published papers and blog posts throughout, and say plainly where its internals are unpublished.

01What we're building, and how big

1.1 What it has to do

YouTube recommends in many places: the home feed, the "Up next" list beside and below a playing video, Shorts, notifications, and the end screens that suggest what comes next. We'll concentrate on the first two. On the home feed there's no current video, so the system has to infer what Arjun wants from his history and the time of day. On Up next the video he's watching is itself a strong clue, so the question changes to "given this video and this person, what comes next?" The two surfaces share most of their machinery and differ in what they feed it.

The system has to put a ranked list of about a dozen videos in front of someone within a fraction of a second of opening the app. Those videos should be ones the person is glad they watched, which differs from the ones they clicked, and a whole section below deals with that gap. New uploads need a fair chance within hours, the list shouldn't be a dozen near-copies, and the system must behave sensibly for a person it has never seen and for a video nobody has watched.

1.2 How big is it?

Published numbers come from different years, so we'll date each one. In 2016, the YouTube team wrote that its recommendations were "helping more than a billion users discover personalized content" (Covington, Adams and Sargin, RecSys 2016). The same paper described models with about a billion parameters trained on hundreds of billions of examples. By 2019, a YouTube ranking paper cited 1.9 billion monthly active users and hundreds of billions of user-interaction log entries a day (Zhao et al., RecSys 2019). In January 2018 YouTube's chief product officer, Neal Mohan, said at CES that more than 70% of watch time came from recommendations, as press reports of the talk put it. In September 2021 a YouTube blog post by Cristos Goodrow, then VP of engineering, said the system learns from "over 80 billion pieces of information" it calls signals, and that recommendations drive a significant share of all viewing.

Corpus size is harder to pin down. YouTube's press page in 2026 says that on average over 20 million videos are uploaded every day, and that Shorts alone average over 200 billion views a day. YouTube hasn't published the size of the full corpus, but 20 million uploads a day adds up to roughly 7 billion in a year, so billions of videos is probably the right order of magnitude. In 2016 the paper described a corpus with "many hours" of video uploaded each second, and that rate makes freshness a hard constraint.

Predict before you read on

Suppose we scored every video in a one-billion-video corpus with a big ranking model for every request. This model costs half a millisecond of CPU per video (an illustrative figure, since YouTube doesn't publish its model costs). How many CPU-seconds is one request, and how many cores would you need to answer in 100 milliseconds? Then compare with scoring just 500 candidates.

Show the answer

One billion videos at 0.0005 seconds each is 500,000 CPU-seconds for a single request. To finish in 0.1 seconds you'd spread that across 5 million cores, for one person's one page load. Scoring 500 candidates costs 0.25 CPU-seconds, which two or three cores can do in 100 ms. At an illustrative 10,000 requests a second that's about 2,500 cores, a size you can build.

That conclusion gives the whole design its shape. Only a few hundred videos can reach the expensive model, so something much cheaper must choose those few hundred from the billions, and "cheaper" has to mean a cost that barely grows with the size of the corpus.

We also have no published figure for YouTube's end-to-end latency budget, and we won't invent one. That 2016 paper says only that scoring millions of items needs to happen "under a strict serving latency of tens of milliseconds" for the candidate stage. We'll treat that as the guide and reason about the rest from what a request has to do.

02Version 1: popular videos and similar videos

Our first design has two lists, and it's the obvious one. One is whatever is most watched right now, shown to everybody. The other is videos similar to the one you're watching, for Up next. We can make "similar" precise without any machine learning at all. If lots of people who watched video A also watched video B within the same day, then A and B are related, and counting those pairs gives a score. That count is the co-visitation count, and it's what the first published description of YouTube's recommender used, in a 2010 RecSys paper by James Davidson and colleagues.

The 2010 system works in three steps. For each pair of videos, it counts how many times the pair was watched by the same signed-in user within a window of roughly 24 hours, and turns the count into a relatedness score by dividing by a measure of each video's overall popularity, so a video that everyone watches isn't related to everything. Then, for a given person, it takes a seed set of videos from their own history (watched, liked, favourited) and looks up each seed's most related videos. Last, it expands outward: related videos of related videos, a few steps along the graph, so the list isn't limited to direct neighbours. Candidates are then ranked using signals about the video's overall quality, the user's own habits, and a diversity step that stops one seed from filling the whole list.

Version 1: a hand-built relatedness graph
HISTORY Arjun's watches = seed videos CO-VISIT GRAPH pairs watched by one user within ~24 h EXPAND neighbours of neighbours RANK + DIVERSIFY quality, user habits, variety Built offline from logs; a lookup at request time is nearly free. A video uploaded an hour ago has no pairs yet, so it has no edges and can never be a candidate. The graph sees a bag of seeds: no order, no time of day, no learned idea of what keeps someone watching.
Follow the arrows left to right, then read the two lines underneath. Every box is cheap, which is why this design lasted, and the amber line is the first thing that breaks.

This design has real strengths, and that's why it lasted. It needs no content understanding, no labels and no deep learning, lookups on a precomputed graph cost almost nothing, and it works in any language for any video that has been watched alongside others.

It breaks in ways that tell us exactly what to fix. A video uploaded an hour ago has no co-visitation counts, so it can't be recommended, and for a platform whose product is partly "what's happening now" that's a serious hole. The graph treats Arjun's history as an unordered bag of seeds, so it can't tell that he has finished the bicycle series and is moving on, or that his tastes at ten at night differ from his tastes at noon. It has one idea of relatedness, a count, and nothing in it learns what makes a person stay. It can also only reach videos linked to the person's seeds through the graph, so surprises are hard to introduce.

Each of these points toward the same remedy: replace the hand-built graph and its hand-written scoring with a model that learns from every watch, takes many kinds of signal as input, and can score a video it has never seen. YouTube published exactly that move in 2016, replacing a matrix-factorization system with a neural network, and that's where we go next.

Candidate sourcePlusMinus
Most popularTrivial, never empty, good for brand-new usersIdentical for everyone, and popular things get more popular
Co-visitation graph (2010)Cheap lookups, no content understanding, explainableBlind to new videos, ignores order and context, one hand-made notion of relatedness
Learned model over historyUses any signal, generalises to unseen combinationsNeeds a training pipeline, serving index and careful label design

None of these choices is permanent. In 2016 the paper says its neural generator was designed so that candidates from other sources, "such as those described in an earlier work", the 2010 system among them, could still be blended in. In 2019, Zhao and colleagues describe a candidate stage that pools several generators: one matching topics of the playing video, one built on how often videos are watched together, and a sequence model over the user's history.

03Candidate generation as classification

We now want one model that looks at Arjun's history and proposes a few hundred videos worth ranking. A natural way to set the task up is the way a language model predicts the next word. Take a moment in some user's past, show the model everything that came before it, and ask it to name the video that was watched next. Its answer is a probability for every video in the corpus, and the candidates are the ones with the highest probability. Covington, Adams and Sargin described YouTube's version of this in 2016 and called it "extreme multiclass classification", where each of millions of videos is a class.

Formally, a neural network turns the user and their context into a vector , and each video has its own learned vector . The probability that the next watch is video is a softmax over the dot products:

Here is the user, the context, and the whole corpus. The network that produces is simple. Each video the user watched is a row in an embedding table, which is a learned list of numbers per video. This paper averages the embeddings of the last 50 watches into one vector, did the same for the last 50 search queries (split into one-word and two-word tokens), and added coarse features such as geography, device, age and gender. These are concatenated and passed through ReLU layers whose widths halve at each step, 1024 then 512 then 256, so the output matches the 256-dimensional video vectors. Averaging beat sum and component-wise max in the authors' tests. Vocabulary size was 1 million videos and 1 million search tokens, each embedded with 256 numbers.

The 2016 candidate generator
WATCH HISTORY last 50 IDs, embedded then averaged SEARCH HISTORY last 50 queries, tokens embedded, averaged EXAMPLE AGE 0 at serving time CONTEXT geography, device, age, gender, logged-in CONCATENATE ReLU 1024 ReLU 512 ReLU 256 = user vector u TRAINING softmax over ~1M video classes, several thousand sampled negatives SERVING nearest neighbours of u among video vectors v (dot product)
Read top to bottom. Sparse histories become fixed-width vectors by averaging, the stack of layers halves in width until it matches the 256-number video vectors, and the same user vector u is used two ways: against sampled negatives in training and as a search query in serving.

3.1 Why the softmax breaks, and the fix

At this scale the softmax has a problem. Its denominator sums over every video, so each training example costs a pass over millions of classes, and training sees hundreds of billions of examples. The standard escape is sampled softmax. For each example, keep the true next video and draw only a few thousand other videos at random as stand-ins for "everything else". Loss is computed over that small set only. Drawing the stand-ins at random from the training data would favour popular videos, so the sampled scores are corrected with importance weights. In terms of scores (logits, the numbers fed into the softmax) the correction subtracts the log of the probability with which each negative was drawn:

Here is the sampled set (the positive plus the negatives) and is how likely video was to be drawn. That paper reports several thousand negatives per example, "more than 100 times speedup over traditional softmax". It also tried hierarchical softmax, which arranges the classes in a tree so you only evaluate one path, and couldn't match the accuracy, because each node in the tree has to separate groups of videos that have little in common. We'll meet this equation again in section 04, where the choice of turns out to matter a great deal.

3.2 Three decisions about the training examples

In 2016 the paper spends as much time on which examples to train on as on the architecture, and says the choice has "outsized importance" in A/B tests while being hard to measure offline. Three of its choices show how much room there is to go wrong.

First, which watches count. Training examples come from all YouTube watches, "even those embedded on other sites", and not only from watches that followed a recommendation. If the model learned only from clicks on its own suggestions it would keep reinforcing itself and new content would struggle to surface, so a video discovered through search or a friend's link is fed back into collaborative filtering too. Second, weighting. Here the paper generates a fixed number of examples per user, which weights users equally and stops a small group of heavy viewers from dominating the loss. Third, and least obvious, is the label itself.

A common recipe in collaborative filtering is to hide a random item from a person's history and predict it from the others. This paper argues this leaks the future: if you know what Arjun watched after the video you're predicting, you can almost read the answer off. Watching patterns are also asymmetric. People watch a series in order, and they find a genre's famous videos before its niche ones. So instead of a random hold-out, the paper "rolls back" a user's history to a random moment and feeds the model only what came before, then asks for the next watch. Withholding information matters in the other direction too. If Arjun has just searched for a cricket player and the model sees that search as an ordered event, it will predict the videos on that search results page, since those are what people watch next. Reproducing the last search page as a home feed performs very poorly, as the authors put it, so search queries are fed in as an unordered bag of tokens that hides where the label came from.

3.3 Telling the model what day it is

One more problem comes from how training works. Our model learns from watches over a window of several weeks, so the probabilities it produces reflect average popularity across that whole window. But video popularity is far from constant: a video uploaded yesterday may already be the most watched thing in the corpus, while one that peaked three weeks ago has faded. Left alone, the model "has an implicit bias towards the past".

The paper's fix is a feature called example age. During training, each example is given the age of the example (how long before the end of the training window the watch happened) as an input. The network can then learn that watching probability for a given video rises and falls with time. At serving time the feature is set to zero, or slightly negative, which tells the model "predict as if it's the very end of the training window", meaning right now. This paper's Figure 4 shows a video where the model with example age follows the actual upload spike, and the model without it predicts a flat average. They report that the feature improved offline precision and "increased the watch time dramatically on recently uploaded videos in A/B testing".

Why predict the next watch and not a random held-out one?

Because the thing we do at serving time is predict the future from the past. A random hold-out gives the model information about later events that won't exist when it serves, so offline accuracy goes up while live performance doesn't. Rolling back makes training look like serving. It also keeps the model honest about order, so it can learn that a person who has watched episode two will want episode three.

Way to train over millions of classesPlusMinus
Full softmaxExact gradientEvery example costs a pass over the whole corpus
Hierarchical softmaxCost grows with the log of the corpusTree nodes compare unrelated videos, which hurt accuracy in YouTube's tests
Sampled softmax with importance correctionA few thousand negatives per example, over 100 times fasterQuality depends on the sampling distribution and on getting the correction right

YouTube chose sampled softmax in the 2016 paper, and it still uses a variant of it in 2019, as the next section shows. Depth helped in a clean way: holdout mean average precision rose as hidden layers were added, from zero (a linear factorization that performed like the predecessor system) up to four, with shrinking returns, and adding search history on top of watch history helped as well. This system is still collaborative filtering, but now other signals can take part.

Predicting the next item with a softmax over a huge vocabulary, is how language models work, with tokens as the classes. Sampled softmax is also a standard tool for large-vocabulary speech and translation models, and the 2016 paper cites that literature for its importance weighting.

04Two towers, and training them honestly

Our 2016 candidate generator has a limitation that shows up as soon as you try to use it for the fresh-content problem. Its video vectors are learned per video ID, in an embedding table, and a video that has just been uploaded has no row that has ever received a gradient. This model can only represent a video through watches, which a new video hasn't had. We'd like each video's vector to be computed from the video's features instead: its title, its topics, its channel, its length, whatever the system knows about it at upload time.

That points to the design on page 16, the two-tower model. One network, the user tower, maps everything known about the user and context to a vector. A second network, the item tower, maps everything known about a video to a vector of the same length. A pair's score is a dot product:

Here is the user-and-context features, the video features, and and the two towers. Because the video vector depends only on the video, every vector can be computed ahead of time and stored in an index, and a request needs only one run of the user tower. A video that was uploaded a minute ago has features, so it gets a vector the moment its features are known. YouTube's 2019 paper by Xinyang Yi and colleagues (RecSys 2019) describes this as Neural Deep Retrieval, a retrieval system that serves personalised suggestions from a corpus of tens of millions of videos. (YouTube doesn't say how the "tens of millions" relates to the whole corpus. It's probably the subset eligible for that surface.)

4.1 Where the negatives come from

Training this model is again a softmax over the corpus, and again we can't afford the full denominator. Section 03's sampled softmax drew negatives from a separate sampler, but there's a cheaper source that needs no extra work. Take a batch of training examples, each a user with the video they watched. Score every user against every video in the batch. That's a table. The diagonal holds the true pairs, and every off-diagonal cell is a pair that didn't happen, so the other videos in the batch serve as the negatives for each user. These are in-batch negatives. They cost nothing extra, because the item tower has already encoded every video in the batch.

There's a catch, and it's the point of the 2019 paper. Videos in a batch are there because someone watched them, so a popular video shows up as a negative far more often than a rare one. Sampling follows the watch distribution itself. A model trained with plain in-batch softmax learns that popular videos are usually wrong answers, because it keeps seeing them in the wrong-answer role. It penalises popularity that is, in fact, real.

In-batch negatives: a batch of 4, scored as a 4 x 4 table
video 1 video 2 video 3 video 4 (popular) user 1 true pair negative negative negative user 2 negative true pair negative negative user 3 negative negative true pair negative user 4 negative negative negative true pair Every off-diagonal cell is a free negative. Column 1 is a popular video, so it is a negative for every other user. Plain softmax learns to push that column down. Subtracting log p(video) from each column's score cancels it. Corrected score: s_c(x, y_j) = s(x, y_j) - log p_j (serving uses the uncorrected s)
Look at the amber column. Videos that people watch a lot show up in the batch more often, so they are negatives more often, which is the bias that the logQ term removes.
Predict before you read on

We train a retrieval model with plain in-batch negatives, no correction, on data where popular videos are in fact watched more. Then we retrieve the top 10 videos for each kind of user. Compared with the truth, will the retrieved videos be more popular, less popular, or about the same?

  1. More popular, since popular videos are everywhere in the data
  2. Less popular, since popular videos appear as negatives most often
  3. About the same
Show the answer

B, less popular. A popular video is the "wrong answer" in many more rows of the batch than a rare one, so the loss keeps pushing its score down. The experiment below measures it: the uncorrected model's picks are far less popular than the true top 10, and overlap with the truth is low.

4.2 The logQ correction

We fix it with the importance correction from sampled softmax, applied to the batch. If video appears in a batch with probability proportional to , its frequency, then we subtract from its score before the softmax, so a frequent negative counts for less in proportion to how often it turns up. Yi et al. write the corrected score as

and the batch loss for example is the softmax over of those corrected scores, with the true pair on the diagonal. After training, serving uses the uncorrected score , because the correction only exists to undo how we picked negatives. That paper also divides scores by a temperature and weights examples by a reward, such as watch time, but the correction is the part that carries the idea.

That leaves how to know . In a stationary dataset you'd count. On YouTube the corpus changes constantly and there's no fixed vocabulary of video IDs to count over, so the paper proposes estimating item frequency from the stream. It keeps arrays indexed by a hash of the video ID, and for each hash bucket it tracks, with a moving average, the number of training steps between consecutive appearances of that item. The frequency is the inverse of that gap: an item that appears every 100 steps has per sample. This estimate adapts when popularity shifts, works for videos the system has never counted before, and the paper analyses it as producing unbiased estimates. Hashing keeps the memory fixed, at the price of occasional collisions between items. (A 2025 follow-up from other authors argues the standard formula can be improved by excluding the positive item from the denominator, which shows that this part of the recipe is still being refined.)

Here is the correction measured in a toy world. Twenty groups of users each have their own taste over 300 videos, with a long-tailed popularity built in, and positives are drawn from the true preferences. We train a table of scores with the in-batch loss, once plain and once with the term, and compare the learned top 10 with the true top 10.

Try it Train a two-tower-style retrieval loss with and without the logQ correction
import numpy as np, torch
torch.manual_seed(0); rng = np.random.default_rng(0)
C, N, B = 20, 300, 128                 # user groups, videos, batch size
pop = 1 / np.arange(1, N + 1) ** 1.1   # a few videos dominate
pop /= pop.sum()
a = rng.normal(0, 1.5, (C, N)) + np.log(pop)    # true affinity logits
P = np.exp(a - a.max(1, keepdims=True))
P /= P.sum(1, keepdims=True)                    # true P(j | group c)
q = (P.mean(0))                                  # marginal item frequency
logq = torch.tensor(np.log(q), dtype=torch.float32)

def train(correct, steps=3000):
    S = torch.zeros(C, N, requires_grad=True)    # score table s(c, j)
    opt = torch.optim.Adam([S], lr=0.05)
    for _ in range(steps):
        c = rng.integers(0, C, B)
        j = np.array([rng.choice(N, p=P[k]) for k in c])
        logits = S[c][:, j]                       # B x B, in-batch
        if correct:
            logits = logits - logq[j][None, :]    # minus log Q
        loss = torch.nn.functional.cross_entropy(logits, torch.arange(B))
        opt.zero_grad(); loss.backward(); opt.step()
    return S.detach().numpy()

for name, flag in [("plain in-batch", False), ("with logQ", True)]:
    S = train(flag)
    top = np.argsort(-S, 1)[:, :10]
    true = np.argsort(-P, 1)[:, :10]
    rec = np.mean([len(set(t) & set(r)) / 10 for t, r in zip(top, true)])
    print(f"{name:15} overlap with true top-10: {rec:.2f}, "
          f"mean log-pop of picks: {np.log(q[top]).mean():.2f}")
tt = np.argsort(-P, 1)[:, :10]
print(f"{'truth':15} {'':27} mean log-pop of picks: "
      f"{np.log(q[tt]).mean():.2f}")
With plain in-batch negatives the learned top 10 overlaps the true top 10 by only 0.26, and the mean log-popularity of its picks is -6.82 against -4.27 for the truth, so it has learned to avoid popular videos. Subtracting log Q from the scores lifts the overlap to 0.87 and brings the picks to -4.33, almost exactly the truth. Only the loss changed between the two runs.

The plain model's picks sit roughly two and a half log-units lower in popularity than the truth, and only a quarter of its top 10 overlaps with the real top 10. With the correction the picks are almost exactly as popular as the truth and 87% of the top 10 matches. Nothing else changed between the runs, so the sampling choice alone changes what the model recommends. (The toy uses score tables where the paper uses neural towers, because the correction acts on the scores either way.)

Retrieval modelPlusMinus
Per-ID embeddings (2016 style)Strong for videos with plenty of watchesNew videos have no trained vector; a long tail gets noisy ones
Two towers, plain in-batch negativesFree negatives, video vectors from contentLearns to avoid popular videos
Two towers, in-batch negatives with logQFree negatives, content-based, and the popularity bias is removedNeeds a streaming frequency estimate

YouTube published the per-ID model in 2016 and the two-tower design in 2019. Yi's paper reports offline gains on two datasets and live A/B improvements in recommendation quality on YouTube, but gives no headline percentage that we can quote, and we won't make one up. Whether the older model still runs beside the newer one is unpublished. That 2019 ranking paper does say the candidate stage pools several generators, and a per-ID model remembers well-watched videos better while a content-based tower covers the new ones, so keeping both seems a sensible design.

Towers for cheap retrieval plus a frequency correction for in-batch negatives are the standard recipe for dense retrieval in search, ads and app stores, and in the retrieval half of retrieval-augmented generation on page 11. It matters wherever item popularity follows a power law.

05Searching a billion vectors in a few milliseconds

With the towers trained, every video has a vector and Arjun's request has produced a user vector. Next we need to find the videos whose vectors have the largest dot product with his. Computing the dot product against every vector is the linear scan we ruled out in section 01. We need something that gets close to the right answer while looking at a small fraction of the corpus. That's approximate nearest-neighbour search, and because the score is a dot product and not a distance, the technical name is maximum inner product search.

In 2016 the paper handled this in a sentence. Since the softmax's calibrated probabilities aren't needed at serving time, "the scoring problem reduces to a nearest neighbor search in the dot product space for which general purpose libraries can be used". They also report that "A/B results were not particularly sensitive to the choice of nearest neighbor search algorithm". So the quality of the vectors matters far more than the last few percent of search accuracy, and a good-enough index is a good place to save effort.

What does a good-enough index look like at a billion vectors? Storing the vectors is the main cost. A billion vectors of 256 numbers at four bytes each is roughly a terabyte, so you can't keep full vectors in memory on one machine. Standard practice is product quantization. Cut each vector into short pieces, learn a small codebook of typical pieces for each position, and store each piece as the index of its nearest codebook entry. A vector then costs a few bytes, and a dot product against a stored vector becomes a handful of table lookups. Google's open-source ScaNN library (Guo et al., ICML 2020) improves on plain quantization with one idea about what the quantization error should measure.

Ordinary quantization chooses codebooks to minimise how far the compressed vector lands from the original, the reconstruction error. But for retrieval the only errors that matter are the ones that change which vectors have the highest inner product with a query. If a stored vector is replaced by , the residual splits into a part parallel to and a part orthogonal to it. Error parallel to changes the inner product with queries that point the same way as , and those are exactly the queries for which is a top result. Orthogonal error mostly moves the score of queries for which was never going to win. ScaNN's score-aware loss weights the two differently:

The paper proves for any weighting that gives larger inner products more weight, with equality only when all inner products are weighted equally. The result is called anisotropic quantization because the penalty depends on direction. In the paper's tests it reaches state-of-the-art results on the public ANN-benchmarks suite, for example on Glove-1.2M. We don't know whether YouTube's production retrieval uses ScaNN, since that's unpublished, though ScaNN was built at Google Research and is probably the kind of tool this stage needs.

One operational detail follows from the design even though YouTube hasn't published it. The index is built from the item tower's output, so the user tower that's serving must come from the same training run as the item vectors in the index, or the dot products mean nothing. They have to be deployed as a pair. How often YouTube rebuilds or updates the index is unpublished, and that schedule sets a floor on how quickly a brand-new video can appear in retrieval.

Vector search of this kind also runs retrieval-augmented generation, image search and ad targeting. Index details vary (graph indexes such as HNSW trade memory for speed, quantized ones such as ScaNN or IVF-PQ trade accuracy for memory), but the shape is constant: precompute vectors, compress them, probe a small part of the index per query.

06Ranking: predicting expected watch time

Retrieval has handed us a few hundred candidates for Arjun, and now we can afford to look at each one carefully. Section 02's list was ordered by relatedness, and section 03's by the probability of the next watch. Neither says which of these videos he'd be happiest to see first. A ranking stage assigns each candidate a score for a chosen objective and sorts by it. In 2016 the paper explains why the stage exists: candidate generation uses coarse features, and showing a few best videos "requires a fine-level representation to distinguish relative importance among candidates with high recall".

First we have to decide what to score. YouTube's early answer was click probability, and the bicycle-chain example shows why that fails. Imagine two candidates for Arjun. One has a thumbnail built to be irresistible, and 30% of people who see it click, but they leave after twenty seconds. Another is a tutorial that only 10% click, though they watch five minutes. A click model puts the first on top. The objective YouTube moved to, in 2012, was watch time, and the 2016 paper describes how to train a model whose output is the expected watch time of a candidate. We'll look at the motivation in section 08 and the mechanism now.

6.1 Weighted logistic regression

Watch time is awkward to predict directly. Most impressions are never clicked, so for most of them it's exactly zero. Clicked ones have a long-tailed watch time, from three seconds to three hours. Fitting a regression to that mixture is hard. Here the paper uses a trick: train an ordinary logistic regression, the kind that predicts a click with a sigmoid, but change the weight of each training example. A clicked impression counts with a weight equal to the seconds the person then watched. An unclicked impression counts with weight 1. Under the standard cross-entropy loss the objective becomes

Here is 1 if impression was clicked, is its watch time, and is the model's output before the sigmoid. Why does this give expected watch time? A logistic regression's output is the fraction of positive weight, so its odds, , equal the total positive weight divided by the total negative weight. Say we have impressions and of them were clicked. The positive weight adds up to and the negative weight is , so the odds the model learns are

where is the expected watch time of an impression (zero when it isn't clicked) and is the click probability. Because is small on a page of recommendations, is close to 1, and the odds are close to the expected watch time. So at serving time the model applies , the exponential, as its final activation and reads the result as an estimate of expected watch time. That 2016 paper gives this derivation and reports that it "performed much better on watch-time weighted ranking evaluation metrics compared to predicting click-through rate directly".

Our experiment below builds four kinds of video with a known click probability and watch time, trains the weighted regression and an ordinary click model on simulated impressions, and compares what each learns.

Try it Click model versus watch-time-weighted logistic regression
import numpy as np
rng = np.random.default_rng(1)
# Four kinds of video: click probability and mean watch time (seconds)
kinds = ["bait thumbnail", "news clip", "tutorial", "long lecture"]
p_click = np.array([0.30, 0.15, 0.10, 0.05])
mean_T = np.array([20.0, 90.0, 300.0, 900.0])
n = 400_000
k = rng.integers(0, 4, n)                         # impression -> kind
click = rng.random(n) < p_click[k]
T = np.where(click, rng.exponential(mean_T[k]), 0.0)

X = np.eye(4)[k]                                  # one-hot features
def fit(weights, steps=4000, lr=2.0):
    w = np.zeros(4)
    for _ in range(steps):
        p = 1 / (1 + np.exp(-X @ w))
        g = X.T @ (weights * (p - click)) / n
        w -= lr * g / weights.mean()
    return w

w_ctr = fit(np.ones(n))                           # plain click model
wt = np.where(click, T, 1.0)                      # watch time weights
w_wt = fit(wt)                                    # weighted logistic reg
print(f"{'kind':15}{'CTR':>7}{'exp(w)':>9}{'E[T]':>8}{'P*E[T]':>9}")
for i in range(4):
    print(f"{kinds[i]:15}{1/(1+np.exp(-w_ctr[i])):7.2f}"
          f"{np.exp(w_wt[i]):9.1f}{mean_T[i]:8.0f}"
          f"{p_click[i]*mean_T[i]:9.1f}")
print("rank by CTR      :", [kinds[i] for i in np.argsort(-w_ctr)])
print("rank by exp(w)   :", [kinds[i] for i in np.argsort(-w_wt)])
The click model ranks the four kinds bait thumbnail, news clip, tutorial, long lecture and the weighted model ranks them in exactly the reverse order. The learned odds track the formula: for the bait thumbnail, expected watch time per impression is 6.0 seconds, and 6.0 / (1 - 0.30) is about 8.6, against the 8.5 the model learned.

Here the click model ranks the bait thumbnail first and the long lecture last, an exact reversal of the watch-time ranking. The learned odds also land close to the formula: for the bait thumbnail, 0.30 × 20 seconds is 6 seconds of expected watch time per impression, and dividing by gives 8.6, close to the 8.5 the model learned. Whether the lecture should be on top for Arjun is another matter, and section 08 returns to it.

Predict before you read on

In the experiment above, suppose we drop the watch-time weights and only keep the click label, but we keep the same data. Which video does the model rank last, and does anything in the training data change?

Show the answer

Ranking flips. With unit weights the model learns click-through rates, 0.30, 0.15, 0.10 and 0.05, so the long lecture comes last even though it has the highest expected watch time per impression. Data is identical, since the same impressions were logged. Only the loss changed, and the 2016 paper reports that weighting positives equally increased the watch-time-weighted loss by 4.1%.

6.2 The model and its features

Behind the logistic output sits a deep network. Its version feeds in hundreds of features, split about evenly between categorical ones (video ID, language, channel) and continuous ones (time since the last watch, number of previous impressions). Categorical features are embedded, with the embedding size growing roughly with the logarithm of how many distinct values there are. Features that refer to videos share one embedding table (the impression video, the last video watched, the video that seeded the recommendation), which improves generalisation and cuts memory, and each still enters the network separately. Most parameters sit in these tables: one million IDs in 32 dimensions have seven times more parameters than fully connected layers 2048 units wide.

Continuous features get unusual care. Neural networks are sensitive to the scale of their inputs, so each continuous value is replaced by its position in its own distribution: its percentile, computed from quantiles collected in one pass over the data. The network receives that value and also and , which makes it easy to build super-linear and sub-linear responses. Offering the powers lowered the offline loss by 0.2% in the paper's test.

Features that mattered most are the ones that connect the person to the specific candidate. This paper says the strongest signals "describe a user's previous interaction with the item itself and other similar items", for example how many videos Arjun has watched from this channel, and when he last watched something on this topic. Two other kinds of feature are easy to overlook. One is which candidate generators nominated this video, and with what scores, so retrieval's opinion flows into ranking as an input. Another is how often the video was already shown to this person. If a video was recommended to Arjun last time and he scrolled past it, the model sees a high impression count with no watch, and demotes it. That paper calls this "churn": successive page loads shouldn't return identical lists.

Architecture was tuned against a budget. Table 1 of the paper reports a watch-time-weighted pairwise loss on next-day holdout data: 41.6% with no hidden layers, 36.9% with one 256-wide ReLU layer, 35.8% with a 1024-wide one, and 34.6% for the three-layer 1024, 512, 256 stack that the authors chose because it stayed inside the serving CPU budget. This loss takes one clicked and one unclicked impression from the same page. If the unclicked one scores higher, the clicked impression's watch time counts as mispredicted, and the loss is the mispredicted share of total watch time.

What the ranker predictsPlusMinus
Click probabilityCalibrated, simple, plenty of labelsRewards thumbnails that win clicks and lose viewers
Watch time as a regressionTargets the quantity directlyMostly zeros plus a heavy tail, so fits poorly
Weighted logistic regression (2016)Stable training, odds approximate expected watch timeFavours long videos, and says nothing about whether people were glad they watched
Several predictions combined (2019)Can include likes, dismissals and survey scoresCombination weights need hand tuning

YouTube chose the weighted regression in 2016 and had moved to the last row by 2019, the subject of the next section. Weighting travels well: expected revenue per ad impression, or basket size per search, also mix zeros with a heavy tail and can use sample weights on a logistic loss in the same way.

07Many goals at once: multitask ranking and position bias

By 2019 the question had grown. Arjun could click a video, watch half of it, press like, share it, or tap "not interested", and each of those says something different. Watch time alone treats a video he left on in the background and one he loved as the same thing. A paper by Zhe Zhao and colleagues at YouTube (RecSys 2019) describes the ranking system for Up next that results, and it makes two changes to the single-objective ranker of section 06. It predicts several things at once, and it separates how good a video is from where it was shown.

7.1 Several objectives

The paper groups the labels into two families. Engagement objectives are about behaviour: clicks and watches, where the label is a yes or no, and time spent, where the label is a number. Satisfaction objectives are about judgement: pressing like, leaving a rating, or dismissing a recommendation. Each objective is its own prediction task, trained with cross-entropy for yes-or-no labels and squared error for numeric ones. At serving time each candidate has a handful of predictions, and the paper combines them "in the form of weighted multiplication", with weights "manually tuned to achieve best performance on both user engagements and user satisfactions".

Sharing work between tasks usually starts with a shared-bottom network: one stack of layers feeds a separate output head for each task. It works when the tasks are closely related, and it hurts when they're not. Predicting a like and predicting watch time pull the shared layers in different directions, and a single set of weights has to serve both. Here the paper adopts Multi-gate Mixture-of-Experts (MMoE), an architecture from Ma et al. (KDD 2018), to let tasks decide how much to share.

An MMoE layer holds several small networks called experts, each a plain ReLU network of its own. For every task there's a gate: a linear layer followed by a softmax that gives one weight per expert. A task's input is the gate-weighted sum of the experts' outputs. With experts , a lower-level shared embedding , and a tower for task :

All tasks share the experts, but each task chooses its own mixture. Two related tasks, such as click and watch, can lean on the same experts. Two conflicting ones can each pick experts the other ignores. Cost stays small, because the gates are tiny compared with the experts. In the paper the experts sit on top of one shared hidden layer, since putting them straight on the very wide input layer would make training and serving much more expensive.

The 2019 ranker: experts, gates and a side tower
FEATURES -> shared hidden layer -> experts Expert 1 Expert 2 Expert 3 Expert 4 gate: click click head gate: watch time watch time head gate: like like head gate: survey survey head engagement tasks (amber) satisfaction tasks (pink) all experts are shared COMBINE weighted product of the predictions, with hand-tuned weights SHALLOW TOWER inputs: position, device added to the logit in training position missing at serving Each task chooses its own mixture of experts through its gate.
Follow one task, such as watch time. Its gate picks a mixture of the shared experts, its head turns the mixture into a prediction, and the shallow tower on the right adds a position term to the logit during training only.

The paper's live YouTube experiment compares MMoE to a shared-bottom model at matched cost, measured as the number of multiplications in the model. With 3.7 million multiplications, a four-expert MMoE improved engagement by 0.20% and the satisfaction metric (survey ratings) by 1.22% over a shared-bottom baseline of the same size. With 6.1 million, an eight-expert MMoE gained 0.45% engagement and 3.07% satisfaction. A shared-bottom model enlarged to 6.1 million multiplications gained only 0.1% and 1.89%, so most of the improvement seems to come from the architecture and not from extra capacity.

The paper also reports a training problem. In distributed training the gates sometimes collapse so that one expert gets nearly all the weight and the others get none. They saw this "gating network polarization" about 20% of the time. They fixed it with dropout on the gates: with probability 10%, each expert's weight is set to zero and the softmax is renormalised, which forces the gate to keep several experts in play. After that, polarization disappeared in their models.

7.2 Position bias and the shallow tower

Our second change handles a problem from the data. Imagine the video in the first slot of Up next gets clicked three times as often as the one in slot five. Is the first video three times as good? Not necessarily: people look at the top of a list first, so a mediocre video in slot one gets clicked more than a great one in slot five. If the ranker trains on raw clicks, it learns that videos the old system ranked highly are good, which is the old system's opinion handed back to it. This is position bias, and it's one case of selection bias, since the logs only record how people reacted to what they were shown, in the order they were shown it.

One common remedy is to feed the position in as a feature during training and set it to a fixed value at serving. This paper compares it with two other designs and settles on a shallow tower, a small side network taking only bias-related features, such as the position and the device, whose output is added to the main model's logit:

During training, the shallow tower soaks up whatever click probability position explains, so the main tower is free to learn how good the video is. At serving time the position is treated as missing and only the main tower is used. That paper drops the position feature in training with 10% probability so the model doesn't lean on it entirely, and crosses it with device because position effects differ between phones and desktops. This structure is a Wide & Deep model with the shallow tower as the "wide" half, and it learns the bias terms jointly with the main model, with no random experiment needed to measure propensities, meaning how likely each slot was to be examined.

We can test the idea in a toy world. Two hundred videos have a true appeal. An old ranker puts videos in eight slots using their appeal plus its own unrelated taste, and clicks happen with probability that depends on both appeal and slot. We then train one model with no position term and one with a position term, and ask how well each recovers the true appeal.

Try it Recover true appeal from clicks with and without a position term
import numpy as np
rng = np.random.default_rng(2)
N, K, n = 200, 8, 600_000
r = rng.normal(0, 1, N)                         # true appeal
b_true = -0.7 * np.arange(K)                    # penalty per slot
vid = rng.integers(0, N, n)
# the old ranker ranks by appeal plus a taste of its own, plus noise
bad = rng.normal(0, 1.0, N)                      # ranker's own taste
est = r[vid] + bad[vid] + rng.normal(0, 0.5, n)
rank = np.argsort(np.argsort(-est)) / n         # 0 = best
pos = np.minimum((rank * K).astype(int), K - 1)
p = 1 / (1 + np.exp(-(r[vid] - 2.5 + b_true[pos])))
click = rng.random(n) < p

def fit(use_pos, steps=3000, lr=1.0):
    s, b = np.zeros(N), np.zeros(K)
    for _ in range(steps):
        z = s[vid] + (b[pos] if use_pos else 0)
        g = 1 / (1 + np.exp(-z)) - click
        s -= lr * np.bincount(vid, g, N) / (n / N)
        if use_pos:
            b -= lr * np.bincount(pos, g, K) / (n / K)
    return s, b
for name, use in [("no position term", False), ("position tower", True)]:
    s, b = fit(use)
    top = set(np.argsort(-s)[:20]) & set(np.argsort(-r)[:20])
    slope = np.polyfit(r, s, 1)[0]
    print(f"{name:17} slope on true appeal {slope:.2f}, "
          f"top-20 overlap {len(top)}/20")
ctr = [click[pos == k].mean() for k in range(K)]
print("CTR by slot :", np.round(ctr, 3))
print("learned bias:", np.round(b - b[0], 2))
print("true bias   :", np.round(b_true, 2))
Without a position term the learned scores have a slope of 1.82 against true appeal, inflated by the old ranker's taste, and only 13/20 of the true top 20 are found. With the position term the slope is 1.05 and 19/20 match. The learned slot biases follow the true ones closely for the first six slots (-3.31 against -3.5 at slot six) and drift for the last two, where almost nothing is clicked.

Without a position term, the learned scores are inflated by the old ranker's taste, so their slope against true appeal is 1.8 and only 13 of the true top 20 videos land in the model's top 20. With a position term the slope is close to 1 and 19 of 20 match, and the learned slot biases follow the true ones closely for the first five slots, drifting for the last few where almost nothing is clicked.

Handling position biasPlusMinusLive result (2019)
Position as an ordinary input featureTrivial to addThe model can leak it into everything it learns−0.07% engagement
Adversarial lossPushes the main model to ignore positionHarder to train, an extra task+0.01%
Shallow towerSmall, learns propensity jointly, no randomisationAssumes bias adds in logit space+0.24%

YouTube picked the shallow tower in 2019 on the strength of the +0.24% engagement gain in a live experiment, against −0.07% for the plain feature. Learned biases decreased monotonically with slot, as expected. They list limits too: other biases exist that aren't known or modelled, and an elaborate model adds latency, which "can decrease user satisfaction and live metrics", so they prefer simpler architectures.

Search engines, ad auctions, e-commerce results and app stores all train click models with a position term or a propensity weight. Forms vary, and the goal is always to estimate how good the item is separately from where it was shown.

Why is the position set to "missing" at serving time and not to slot one?

At serving we're choosing the order, so there's no position yet to supply. The aim is a score that reflects the video alone, and the main tower learns exactly that once the shallow tower takes responsibility for position. Setting the feature to slot one is the older recipe from click-model research, where you evaluate "as if" every item were at the top. Treating it as missing does the same job in a way that fits the 10% training dropout, since the model has seen what its output looks like with the position absent.

08What are we optimising? Clicks, watch time, satisfaction

Everything so far assumed we knew what to predict. The biggest decisions in this system were about the objective, and they changed over time. Here's the history YouTube has told in its own words.

Goodrow's 2021 post and the 2012 creator blog both describe a first version that optimised views. "Our video discovery features were previously designed to drive views," the 2012 post says. A video that earned many clicks was a good video. By 2011, according to the 2021 post, YouTube had learned that clicks alone don't mean a video was watched, and it began favouring videos that people stayed with. On August 10, 2012, Eric Meyerson announced the change on the creator blog under the title "YouTube Now: Why We Focus on Watch Time". Search and suggested videos would now favour the videos viewers stay with over the ones they click on and then abandon, and recommendations would aim to increase the total time a person spends watching. The stated rationale was that if people are watching more YouTube, "it signals to us that they're happier with the content they've found". Goodrow's 2021 post adds that when watch time was first incorporated, views fell by about 20%, which suggests that a large share of the earlier views were click-and-leave.

Watch time fixed one failure and has weaknesses of its own, which explain the next two changes. It favours long videos, and the 2012 post saw that coming, telling creators that padding a video wouldn't help since the system optimises "how a video contributes to a longer viewing session". It can't tell whether time was well spent, since a person hooked by something sensational looks the same as one enjoying a documentary. And it measures what people do, which only correlates with what they'd say if asked.

Next came asking, which the 2021 post describes. YouTube's surveys ask viewers to rate a video from one to five stars. That post says "only videos that you rate highly with four or five stars are counted as valued watchtime". So the quantity YouTube wants to maximise is closer to valued watch time than raw watch time. It also lists shares, likes and dislikes as signals ("people are more likely to be satisfied by videos that they share or like"), and adds that each signal's weight differs by person: someone who shares every video they see gets less weight on shares. Zhao et al. show how the model side works in 2019: the satisfaction family of objectives includes a survey rating predicted as a regression task, and live satisfaction is measured with survey responses.

Surveys have a statistical problem, though. They reach a small fraction of impressions, and the people who answer aren't a random sample of viewers. YouTube hasn't published response rates or how it adjusts for non-response. The multitask model of section 07 predicts the rating for impressions nobody was asked about, using the behaviour logged for all of them.

ObjectivePlusMinusYouTube
Views and clicksPlentiful, immediate, easy to measureRewards bait; click-and-leave looks like successUntil about 2011–2012
Watch timeHard to fake by clicking; correlates with enjoyingFavours length; blind to regret; rewards whatever holds attentionFrom August 2012
Survey-weighted, multi-objectiveCloser to what people say they valuedSparse labels, hand-tuned combination, survey biasDescribed 2019–2021

Each objective in that table stood in for something we can't measure directly, and the system's incentives followed the stand-in. When it was clicks, creators and the model both learned to produce clicks, and when it became watch time they learned to produce watch time. Surveys bring in a measurement closer to the real target, with their own noise. Every product that optimises engagement, from news feeds to streaming services, faces the same pattern.

Why not predict the survey rating directly and rank by it?

There aren't enough labels. A survey covers a tiny slice of impressions, so a ranker trained only on surveys would be sparse and noisy. Behaviour labels exist for every impression, so the model learns from those and treats the survey as one task among several.

09Serving: one request through the funnel

Now we can assemble the stages and follow Arjun's request. YouTube's own diagram of the system, in the 2016 paper, is a funnel: the video corpus narrows to "millions", then "hundreds", then "dozens". That's the same shape as the arithmetic in section 01, which required the expensive model to see only a few hundred videos.

One home-feed request: how many videos each stage sees
CORPUS millions+ videos you could recommend CANDIDATES hundreds one cheap score each SHOWN ~a dozen ranked, then reranked candidate generation ranking + rerank Stage sizes follow the 2016 paper (millions, hundreds, dozens); YouTube has not published exact counts. Each stage sees far fewer videos than the one before, so it can afford far more work per video.
Read the big numbers across the top. The expensive model only ever meets the last box, and the first stage has to be sublinear in the size of the corpus.

Here's the path of one home-feed request, with the details that are published and a flag where they're not.

  1. RequestArjun opens the app. A server looks up his recent watches, searches, and the videos already shown to him. In 2016 the paper calls serving up-to-the-second impression and watch history "an engineering feat onto itself", and says it's vital for responsive recommendations.
  2. CandidatesSeveral generators run: the user tower against the vector index, a sequence model over his history, co-watch and topic lookups. Each nominates videos. That 2019 paper pools their output into one set of "a few hundred".
  3. FeaturesFor each candidate the system assembles features about the video, about Arjun, and about the pair: how often he's watched this channel, when he last watched this topic, how many times this video was already shown to him, and which generators nominated it.
  4. ScoreThe ranker predicts several outputs for every candidate (clicks, expected watch time, likes, dismissals, a survey rating). This paper's combination is a weighted product with manually tuned weights, with position treated as missing.
  5. RerankThe sorted list gets final passes for variety and policy. That 2021 post says borderline content is demoted and authoritative sources promoted for news and information, using signals from human evaluators. Where exactly in the pipeline these happen is unpublished.
  6. LogThe dozen videos shown, their positions, and what Arjun does next (taps, watches, likes, dismisses, answers a survey) are logged. This log is tomorrow's training data.

Two published facts shape how this runs. Models train in temporal order and keep training: the 2019 paper describes going "through training data of past days following a temporal order" and consuming newly arriving data, so the model tracks recent behaviour (it trains on TPUs and serves with TensorFlow Serving). And model cost is a design variable. The 2016 ranker's 1024, 512, 256 tower was chosen to stay "within our serving CPU budget", and the 2019 paper warns that an overly complicated model can add enough latency to lower live metrics.

Up next differs in what feeds the first steps. There's a playing video, so one generator matches its topic, another follows what's often watched alongside it, and the ranker receives the playing video's features as part of the query. In 2019 the multitask paper describes this surface, and the 2016 paper describes the home feed, from the same two-stage design.

10Training data and the feedback loop

Each stage so far learns from logs, and the logs record what the previous system chose to show. We've already seen pieces of this: position bias in section 07, and the choice of watches from all of YouTube in section 03. Now we look at the loop as a whole, because it's the structural problem in recommendation. Arjun's model trains on videos that earlier models put in front of people. A video that was never shown has no clicks, so it gets no credit and stays unshown. Videos that were shown a lot get clicks, so they get more credit and are shown even more. This system is learning from a world it partly created.

We can say the same thing in the language of reinforcement learning, which page 15 introduced. The recommender is a policy, a rule that picks a video given what we know about the user. Logged data was generated by older policies, called behaviour policies, and we want to train a new policy on it. Learning about one policy from data produced by another is off-policy learning, and the standard repair is importance weighting. A 2019 WSDM paper by Minmin Chen and colleagues applied this to a candidate generator in production at YouTube.

The setup models the user's state as a vector summarised by a recurrent network that reads the sequence of videos watched. The policy is a softmax over a corpus of millions of videos, the same shape as section 03. The policy gradient with a reward (for example, watch time) normally assumes the actions were sampled from itself. Here they were sampled from an ensemble of earlier policies , so the gradient is reweighted by the ratio of the probabilities:

That ratio is large when the new policy likes an action the old policies rarely took, and those rare actions can swamp the gradient. Here the paper handles this with a cap: the ratio is clipped at in production, and the authors report that weight capping gave the biggest online gain of the variance-reduction techniques they tried. A second issue is that the logs don't contain . This paper learns it, with a separate head trained to predict the logged action.

A recommender shows a page, not a single video, and the paper's second contribution accounts for that. If the page holds videos and a person might pick any of them, the chance that video appears on the page is not but . Differentiating that adds a multiplier to the gradient, the top-K correction:

When is near zero the multiplier is near , so a video the policy hardly ever picks gets a bigger push. When is near one the multiplier goes to zero, because a video that's already certain to be on the page needs no more weight. Smoother policies result. Without the correction, a policy trained to get the single best video right becomes extremely peaked on it, and the rest of the page is neglected. In the paper's live experiments with , setting , which reduces the method to the standard correction, lowered ViewTime by 0.66% compared with the production setting, and lowered it by 0.35%. That paper doesn't claim that this is how all of YouTube's recommendations are trained. It describes one production candidate generator, and says it's one of many.

The loop-breaking tool that's easiest to understand is exploration, which is where section 11 goes next. Chen's paper uses a stochastic policy, drawing videos from the softmax with some randomness instead of always taking the top ones, so the data contains actions the old system wouldn't have chosen. In one live test the authors split users 90%, 5% and 5%, serving the first two groups a deterministic policy and the last a stochastic one, with the model trained on all of it. ViewTime, the paper's name for time spent watching, rose by 0.07% in the test group. They note it's a small gain from a small amount of exploration, since only 5% of users saw the stochastic policy, and expect more once it's launched widely.

Ad ranking uses inverse propensity weights, and search engines use randomised slots to collect unbiased clicks, for the same reason: you only observe the consequences of what you did.

11New videos, new users, and how much to explore

A brand-new video has no history, a brand-new person has none either, and a system that only acts on what it already knows would never find out when it's wrong. Several techniques from earlier sections address these, and here they sit side by side.

For a new video, the item tower of section 04 produces a vector from content features such as title, topic and channel, so retrieval can nominate it before anyone has watched it. The example-age feature of section 03 lets the 2016 generator represent the upload-time spike in popularity instead of averaging it away, and the paper adds that fresh content matters for "bootstrapping and propagating viral content". Training on all watches, not only recommended ones, means a video discovered through search or a link starts earning signal early. In ranking, impression-count features push down a video that has been shown repeatedly and ignored.

For a new person, the 2016 paper says demographic features "are important for providing priors so that the recommendations behave reasonably for new users". Geography and device are embedded, and binary or continuous features such as logged-in state and age go in directly. That's a coarse starting point that real history replaces within a few watches. YouTube hasn't published a dedicated cold-start model for viewers or for creators.

Exploration is the deliberate version. Chen's paper rules out the classic approach, noting that brute-force exploration such as epsilon-greedy "is not viable in a production system like YouTube where this could, and mostly likely would, result in inappropriate recommendations and a bad user experience". It uses Boltzmann exploration instead, drawing from the model's own softmax with a temperature. With the temperature at 1, a live test found no statistically significant change in ViewTime, so the added randomness probably didn't hurt users directly.

Exploration strategyPlusMinus
None (always show the top)Best immediate quality on known tastesUnshown items never get evidence; the loop closes
Epsilon-greedyEasy to implement and reason aboutRandom videos can be bad or inappropriate; YouTube judged it not viable
Sample from the model's softmax (Boltzmann)Exploration is concentrated on plausible videos; no measurable loss at T=1The model must be calibrated enough for its probabilities to mean something

12How YouTube knows a change helped

Everything above is a guess about what will make people glad they watched, so we need a way to check. Published practice has two layers. Offline, you replay logged data. Online, you run an experiment on live traffic. That 2016 paper states the division of labour: "During development, we make extensive use of offline metrics (precision, recall, ranking loss, etc.) to guide iterative improvements to our system. However for the final determination of the effectiveness of an algorithm or model, we rely on A/B testing via live experiments." And then the sentence that matters most: "live A/B results are not always correlated with offline experiments".

Offline metrics are specific to each stage. Retrieval is judged on holdout mean average precision for next-watch prediction, ranking in 2016 on the watch-time-weighted pairwise loss of section 06, and the 2019 paper monitors AUC (area under the ROC curve) for yes-or-no tasks and squared error for numeric ones. They're quick and cheap, so they tune hyperparameters and discard bad ideas.

Offline numbers can't settle the question, for a reason we now understand. They're computed on logs from the old system, so a new model gets credit only for videos the old one chose to show, and whatever it would do differently has no click to be right or wrong about.

In a live experiment, a fraction of users are served by the new model and compared with a control group on engagement and satisfaction metrics. The papers name the ones they watch: click-through rate and watch time in 2016, time spent on YouTube in 2019, alongside satisfaction measures such as the rate of dismissals and user survey responses. The 2019 position-bias result, +0.24% engagement, and the MMoE result, +3.07% on the satisfaction metric, are numbers of this kind. Changes of a few tenths of a percent can be detected because the traffic is so large.

The Chen paper adds a caution that anyone running a sequence of experiments should know. Each new production system provides the training data for the next experiment, so once a new approach has launched, later results can't be compared with earlier ones. Each experiment in the paper is therefore reported against whatever the previous system was, and, in the authors' words, "should be taken as the analysis for each component individually".

Beyond the experiment metrics, the satisfaction survey of section 08 tries to measure what a click-based metric can't. Another measure is the policy-level number YouTube reported in 2021: the share of views that come from borderline content in recommendations, which the post says is "significantly below 1%", with a goal of keeping it under 0.5% of all views on YouTube. It describes a property of the whole system, computed with human evaluators and classifiers whose workings are unpublished.

Why can't we skip A/B tests and trust a good offline metric?

Because the offline set was produced by the system we're trying to improve. A ranker that's better at something the old one never surfaced can look worse offline, and one that imitates the old ranker looks great. Only the experiment shows the new model's own choices to real people.

13What went wrong, and what it cost

YouTube's own accounts describe several rounds of fixing a recommender that had learned to chase the wrong thing. We list them as the posts and papers present them, and we say where a claim can't be checked from the outside.

First came the clickbait era. When discovery was built on views, a thumbnail and title that won a click were enough, whether or not the video delivered. The 2012 change to watch time was the response, and it cost views: roughly 20% when watch time was first incorporated, in Goodrow's account.

Second came tabloid-style content. Goodrow's 2021 post says that demoting it in 2015 coincided with a 0.5% increase in watch time over 2.5 months. That post also lists earlier policy changes in recommendations: classifiers that kept racy or violent videos out in 2011, removal of videos showing minors in risky situations in 2016, and the start of fairness evaluations across protected groups in 2017.

Third came borderline content. In a January 25, 2019 post, YouTube described content that "comes close to, but doesn't quite cross the line of" violating its Community Guidelines, and said it would reduce recommendations of it, with human evaluators across the United States training the machine-learning systems that identify it. It stated that this applied to less than 1% of content, would roll out gradually starting in the United States, and would change only what's recommended and not what's available: videos stay on the site and may still appear to subscribers and in search. In 2021 the post reports the result: demoting borderline content in 2019 cut watch time of non-subscribed, recommended borderline content in the United States by 70%, and consumption of borderline content from recommendations is now significantly below 1% of views.

Those figures are YouTube's own, measured with its own definition of "borderline", and outsiders probably can't reproduce them because the classifiers and evaluator guidelines aren't published. The structural reason the problem arises is clear from the published material, though. A model trained to maximise a behaviour will find whatever content maximises it, including content people regret, and human evaluators, surveys and demotion rules are how the system tries to put the missing term back. How well that works is something the published numbers can't settle alone.

Across these episodes, the cost of a wrong objective was paid in what millions of people saw and what creators made. Technical fixes such as the weighted regression and the shallow tower dealt with bias in the data. The changes that mattered most to the product were about the target: views, then watch time, then valued watch time, then valued watch time minus content YouTube decided not to amplify.

14The whole system

We can now draw the full design. On the left, the online path is Arjun's request, running in a fraction of a second. On the right, the offline path is the loop that keeps the models current: every impression and reaction is logged, turned into labels, and used to train the next version of each model, which is then tested in a live experiment before it replaces the old one.

The whole design, online and offline
ONLINE: Arjun's request, a fraction of a second OFFLINE: the loop that keeps models current APP home feed / Up next REQUEST recent watches, searches, impressions Two-tower + ANN Sequence model Co-watch, topics POOL: a few hundred candidates RANKER multitask MMoE + position tower RERANK variety and policy rules FEED: about a dozen videos LOGS impressions, slots, clicks, watch time, likes, dismissals, survey answers LABELS + FEATURES next-watch labels, example age, per-user example caps TRAINING, IN TIME ORDER towers, ranker, policy heads; keeps consuming new data LIVE A/B EXPERIMENT engagement + satisfaction metrics; promote only what wins promote what was shown and what he did Arjun's taps become tomorrow's labels. Whatever the model shows is what it will learn from next, and that is the feedback loop.
Start at the app on the left and follow the request down to the feed. Then follow the log across to the right, down through training and the experiment, and back to the ranker.

Arjun's request fans out to several candidate generators, whose results are pooled. The ranker predicts several things per candidate, and the reranker applies variety and policy rules. What he sees is logged with each position and his reaction, and that log feeds the labels and the training of every model, in time order. A candidate model, or a new setting of the combination weights, goes to a live experiment, and only wins on the engagement and satisfaction metrics get promoted. The loop closes when the promoted model changes what Arjun sees tomorrow.

Several parts of this picture are unpublished: how many candidate generators run today, how the reranker applies its rules, how often indexes and models refresh, and the end-to-end latency budget. The sources describe the system as of 2016, 2019 and 2021, and the current system has certainly moved since.

15Summary

  1. A billion videos can't be scored per request, so the system narrows in stages: millions, then hundreds, then a dozen. Each stage is cheaper per video and sees fewer videos than the one after it.
  2. The first design was co-visitation. Counting videos watched together (Davidson et al., 2010) is cheap and needs no content understanding, but it can't see new videos and ignores order and context.
  3. Candidate generation is classification over millions of videos (Covington et al., 2016). Sampled softmax with importance correction makes it trainable, about 100 times faster than the full softmax.
  4. Label design matters as much as architecture. Predicting the next watch from history before it, hiding the search page's ordering, equal weight per user, and the example-age feature each fix a specific bias.
  5. Two towers let new videos have vectors, and in-batch negatives make training cheap. But popular videos appear as negatives too often, and the logQ correction (Yi et al., 2019) removes the resulting bias.
  6. Retrieval is approximate nearest-neighbour search. Product quantization compresses billions of vectors, and ScaNN's anisotropic loss spends accuracy where the largest inner products are.
  7. The ranker predicts expected watch time by weighting clicked examples with their watch time in a logistic regression, so that the odds approximate expected watch time per impression.
  8. Multitask ranking separates behaviour from judgement. MMoE lets engagement and satisfaction objectives share experts selectively, and a shallow tower removes position bias (+0.24% engagement live in 2019).
  9. Each objective was a stand-in for something unmeasurable: views, then watch time (2012), then survey-weighted valued watch time. Creators and models both followed whichever was chosen.
  10. The logs record the old system's choices, so training on them is a feedback loop. Off-policy correction with capped weights, the top-K multiplier, and a small amount of exploration are the published remedies.
  11. Offline metrics filter and live experiments decide. YouTube says offline and live results aren't always correlated, and policy changes such as the 2019 borderline demotion are judged on system-level measures as well.

16Build this

The corrections in this chapter are easy to nod along to and easy to get wrong. You can rebuild the heart of the system in a day with synthetic data and no GPU, and watch each fix change what gets recommended.

Project A two-stage recommender with honest negatives and a position tower ~6 hours · numpy and torch

Build a small world where you know the truth: 2,000 users in 20 taste groups, 5,000 videos with a long-tailed popularity and a true appeal per user group, and a simulated old ranker that places videos in eight slots. Everything you train will be judged against that truth, which no real system can do.

  1. Generate impressions from the old ranker, with a click model whose probability depends on true appeal and slot, and a watch time drawn for each click. Log impressions and clicks, not only clicks.
  2. Train a two-tower retrieval model on clicked pairs with in-batch negatives, once plain and once with the logQ correction. Estimate frequencies by streaming, with the hash-and-gap method from section 04, and compare with exact counts.
  3. Index the item vectors and retrieve 200 candidates per user with brute force, then with an approximate index such as FAISS or ScaNN. Report recall of the exact top 200 and the speedup.
  4. Train a ranker on those candidates with three variants: click-through rate, watch-time-weighted logistic regression, and a two-head model with a shallow position tower. Check each against the true appeal and the true expected watch time.
  5. Close the loop. Retrain every round on the logs the previous round produced, for 15 rounds, and plot what fraction of the 5,000 videos is ever shown. Repeat with 5% of impressions sampled from the model's softmax.
You'll know it worked when the plain in-batch model recommends visibly less popular videos than the truth and the logQ version matches it, the click model and the watch-time model disagree about the same videos in the way section 06 predicts, the model with a position tower recovers true appeal where the one without it inflates what the old ranker liked, and the explored run keeps a larger share of the catalogue alive.
A harder variant. Add a second label that behaves like a survey, available for only 2% of impressions and correlated with true appeal but not with clicks. Train a multitask model with an MMoE layer and see whether the sparse label improves the ranking of videos that clicks alone would have ranked too high.

17Interview questions

BeginnerWhy does YouTube use candidate generation followed by ranking instead of one model?

A model that looks at the user and a video together is too slow to run over a billion videos for every request. If it costs half a millisecond per video, one request would take 500,000 CPU-seconds. So a cheap first stage selects a few hundred candidates using a cost that barely grows with the corpus, and an expensive ranker, which can use rich features about the pair, scores only those. In 2016 the YouTube paper describes this funnel: millions, hundreds, dozens. A useful side effect is that candidates from several sources can be pooled before ranking.

BeginnerWhy did YouTube move from optimising views to watch time?

Views reward the click, and a title or thumbnail can win a click without the video being any good. YouTube's August 2012 creator post said the discovery features had been designed to drive views and would now favour videos people watch to the end over those they click on and abandon, with the stated rationale that more watching signals happier viewers. Goodrow's 2021 post says views dropped about 20% at first. Watch time has its own weaknesses, such as favouring length and being blind to regret, so surveys and satisfaction signals were added later.

IntermediateExplain weighted logistic regression for expected watch time.

Train a normal logistic regression on click versus no click, but weight each clicked example by its watch time and each unclicked example by 1. The model's odds then equal the total watch time of clicked examples divided by the number of unclicked ones. With N impressions, k clicks and click probability P, that's E[T]/(1-P), which is approximately E[T](1+P) and close to E[T] when clicks are rare. At serving the exponential of the logit is read as expected watch time. The weighting changes the loss and not the data, and the 2016 paper reports that equal weighting raised the watch-time-weighted loss by 4.1%.

IntermediateWhat is the logQ correction and why is it needed?

In-batch negatives are the other videos in the same training batch, so they are sampled in proportion to how often each video is watched. Popular videos therefore appear as negatives much more often than rare ones, and a plain softmax learns to penalise popularity, which is a real signal. Subtracting log of the video's sampling probability from its logit (s - log p) makes frequent negatives count for less, in proportion to their frequency, which undoes the sampling bias. After training, serving uses the uncorrected score. The probability has to be estimated, and Yi et al. (2019) do it from the stream using a hashed array that tracks the average gap between appearances of each item, which handles a corpus with no fixed vocabulary.

IntermediateHow does a shallow tower remove position bias?

A small side network takes only bias-related features such as slot and device, and its output is added to the main model's logit during training. It absorbs the click probability that position explains, so the main tower learns how good the video is. At serving the position is treated as missing and only the main tower's output counts. That 2019 YouTube paper drops the position feature 10% of the time in training and crosses it with device. In its live tests the shallow tower gave +0.24% engagement, against −0.07% for position as an input feature and +0.01% for an adversarial loss.

IntermediateWhy predict the next watch and not a random held-out watch?

At serving time you predict the future from the past. A random hold-out lets the model see events that happened after the label, so it learns shortcuts that won't exist live, and it ignores asymmetric patterns such as watching a series in order. In 2016 the paper rolls back each user's history to a random moment and feeds only earlier actions. It also removes ordering from search queries by using an unordered bag of tokens, so the model can't reproduce the last search results page.

DeepYour offline metrics improve but the live experiment is flat. What do you look at?

First, the offline data comes from the old system, so the new model is credited only for videos the old system showed and can look better by imitating it; the 2016 paper says live and offline results are not always correlated. Second, position bias in the logs may be inflating the metric for items that were ranked high. Third, a mismatch between the training label and what the live metric measures, for example optimising click prediction when the experiment measures watch time or satisfaction. Fourth, train-serve skew in features such as impression counts and example age. I'd probably check each, then consider whether the model needs exploration data to see what it hasn't been shown.

DeepDescribe the top-K off-policy correction. When would it matter and when not?

A standard off-policy policy gradient weights each logged action by pi/beta, the new policy's probability over the behaviour policy's. Chen et al. (2019) note the system shows K items, so the chance a video appears on the page is 1-(1-pi)^K, and its derivative adds a multiplier K(1-pi)^(K-1). When pi is small it is about K, and when pi approaches 1 it goes to zero, so already-certain items stop absorbing gradient and probability mass spreads to other good items. With K=1 it reduces to the standard correction. In their live tests with K=16 as production, K=1 lost 0.66% ViewTime and K=2 lost 0.35%. It matters when a page shows several items that can each be taken independently, and matters less for a single-slot decision. The importance weights also need capping, e^3 in the paper, to control variance.

DeepWhat are the risks of optimising for watch time, and what did YouTube do about them?

Watch time favours long videos, rewards whatever holds attention including content people regret, and measures behaviour, which is only correlated with satisfaction. YouTube's published responses are layered. It added survey responses and counts only four- and five-star ratings as valued watch time, according to the 2021 post. That 2019 multitask paper adds satisfaction objectives (likes, dismissals, survey ratings) combined by hand-tuned weights. It demoted tabloid-style content in 2015 and borderline content in 2019, and has human evaluators rate authoritativeness for news. These are system-level corrections, and their effects, such as the reported 70% drop in borderline watch time, are measured by YouTube with definitions outsiders can't inspect.

18Go deeper

📄
Paper
The YouTube Video Recommendation System
Davidson et al., RecSys 2010. The co-visitation system of section 02: seeds, the relatedness graph and expansion.
📄
Paper
Deep Neural Networks for YouTube Recommendations
Covington, Adams and Sargin, RecSys 2016. Candidate generation as extreme classification, example age, and the weighted logistic ranker of sections 03 and 06.
📄
Paper
Sampling-Bias-Corrected Neural Modeling for Large Corpus Item Recommendations
Yi et al., RecSys 2019. Two towers, in-batch negatives, the logQ correction and streaming frequency estimation.
📄
Paper
Recommending What Video to Watch Next: A Multitask Ranking System
Zhao et al., RecSys 2019. MMoE for engagement and satisfaction objectives, and the shallow tower for position bias.
📄
Paper
Top-K Off-Policy Correction for a REINFORCE Recommender System
Chen et al., WSDM 2019. Off-policy correction, the top-K multiplier, weight capping and exploration, with live YouTube results. arXiv:1812.02353
📄
Paper
Reinforcement Learning for Slate-based Recommender Systems (SlateQ)
Ie et al., 2019. A decomposition for long-term value over slates, validated in live YouTube experiments. arXiv:1905.12767
📄
Paper
Accelerating Large-Scale Inference with Anisotropic Vector Quantization (ScaNN)
Guo et al., ICML 2020. The score-aware quantization loss of section 05. arXiv:1908.10396
📄
Paper
Modeling Task Relationships in Multi-task Learning with Multi-gate Mixture-of-Experts
Ma et al., KDD 2018. The MMoE layer that the 2019 YouTube ranker adopts.
📝
Company blog
On YouTube's Recommendation System
Cristos Goodrow, September 15, 2021. Signals, surveys, watch time, borderline content and the history of the changes.
📝
Company blog
YouTube Now: Why We Focus on Watch Time
Eric Meyerson, August 10, 2012. The announcement of the move from views to watch time.
📝
Company blog
Continuing Our Work to Improve Recommendations on YouTube
The YouTube Team, January 25, 2019. The announcement about borderline content.