The four-stage recommender

Production recommenders look the same on every team that ships them. Four stages, in order: retrieval, filtering, ranking, reranking. Each stage takes the output of the previous one and narrows it. The whole pipeline turns a customer id into a short list of items in well under a second.

The four-stage recommender pipeline

terminal
bash
git clone https://github.com/learnwithparam/personalized-recommender-system
cd personalized-recommender-system
make dev-local

Clone the workshop repo. One make target boots the whole local stack so you can follow along.

recsys/inference/pipeline.py
python
class RecommenderPipeline:
    def recommend(self, customer_id, top_k=12, use_llm=False):
        # Stage 1 + 2: retrieve candidates + filter already-bought
        candidates = self._retrieval.retrieve_candidates(
            customer_id, top_k=settings.RETRIEVAL_TOP_K
        )
        candidate_ids = [c["id"] for c in candidates]

        # Stage 3: rerank with CatBoost
        ranked = self._ranking.rank(customer_id, candidate_ids)

        # Stage 3b (optional): rerank top set with an LLM
        if use_llm and self._llm.enabled:
            ranked = self._llm.rerank(features, ranked, max_items=top_k * 2)

        # Stage 4: enrich and return top K with article metadata
        return self._enrich(ranked[:top_k])

The orchestrator. Each stage is a service that consumes the previous output. Note how the LLM rerank stays opt-in.

Quiz: Quiz

Loading practice…

AI prompt: Try it: failure modes by stage

Loading practice…