Building a Hybrid Recommendation Engine with Collaborative Filtering, Semantic Search, LLM Reasoning, and LangGraph

Introduction

Modern recommendation systems rarely succeed with a single algorithm. Users expect recommendations that understand their interests, goals, constraints, and context. Traditional recommendation techniques often excel in one area but fail in others.

A hybrid recommendation engine combines the strengths of multiple approaches:

  • Collaborative Filtering (CF) for behavioral patterns.

  • Semantic Vector Search for content understanding.

  • LLM Contextual Reasoning for personalized decision-making.

  • LangGraph for orchestration, memory, and state management.

In this article, we'll build an enterprise-grade recommendation system that combines all four capabilities into a single intelligent platform.

1. Why Hybrid? The Limits of Any Single Signal

Different recommendation techniques capture different aspects of user intent.

ParadigmCapturesBlind Spot
Collaborative Filtering (CF)Taste similarity, emergent communitiesCold-start users/items, lacks semantic understanding
Semantic Vector SearchContent meaning, conceptual similarityIgnores social proof and popularity patterns
LLM Contextual ReasoningGoals, constraints, explanations, personalizationCan hallucinate without grounding, higher latency

A hybrid system solves these weaknesses by combining them.

  • CF identifies what similar users enjoyed.

  • Semantic Search identifies what is conceptually relevant.

  • LLM Reasoning ranks and explains recommendations based on user context.

  • LangGraph orchestrates the entire workflow with memory and state.

2. Real-World Use Case: Enterprise Learning & Career Development

Scenario

Lumina Corp, a consulting organization with 12,000 employees, operates an internal Learning & Development platform.

The platform recommends:

  • Training courses

  • Internal mentors

  • Stretch projects

  • Certification paths

Each employee has:

  • Current role

  • Skill profile

  • Career goals

  • Learning history

  • Project experience

Example User

A junior consultant wants to become a Cloud Architect.

Constraints:

  • Only 2 hours available per week.

  • Needs AWS and Terraform skills.

  • Prefers practical learning.

A recommendation engine must consider:

  • Similar employees who successfully became Cloud Architects.

  • Relevant learning content.

  • Time constraints.

  • Previous feedback.

No single algorithm can handle all these factors effectively.

3. High-Level Architecture

High-Level Architecture

The recommendation pipeline consists of:

  1. Collaborative Filtering Engine

  2. Semantic Search Engine

  3. LLM Contextual Reasoner

  4. LangGraph Multi-Agent Orchestrator

  5. Persistent Memory Layer

The workflow:

Code
User Request
      ↓
Router Agent
      ↓
 ┌───────────────┬───────────────┐
 │ Collaborative │   Semantic   │
 │   Filtering   │    Search    │
 └───────────────┴───────────────┘
            ↓
      Context Agent
            ↓
      LLM Reasoner
            ↓
       Final Ranking
            ↓
        User Response

4. The Three Pillars - Implementation

4.1 Collaborative Filtering (Item-Item Cosine Similarity)

We begin with a lightweight item-based collaborative filtering model.

The model uses a user-course interaction matrix and computes similarity between courses.

Python
import numpy as np
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity

class CollaborativeFilter:
    def __init__(self, interaction_df: pd.DataFrame):
        # interaction_df: user_id, course_id, rating
        self.pivot = interaction_df.pivot_table(
            index="user_id",
            columns="course_id",
            values="rating",
            fill_value=0
        )

        self.item_sim = cosine_similarity(self.pivot.T)
        self.course_ids = list(self.pivot.columns)

    def recommend(self, user_id: str, top_k: int = 10):
        if user_id not in self.pivot.index:
            return []

        user_vec = self.pivot.loc[user_id].values

        scores = self.item_sim.T @ user_vec

        consumed = set(np.where(user_vec > 0)[0])

        ranked = np.argsort(scores)[::-1]

        recs = []

        for idx in ranked:
            if idx not in consumed:
                recs.append({
                    "course_id": self.course_ids[idx],
                    "score": float(scores[idx]),
                    "source": "collaborative"
                })

                if len(recs) == top_k:
                    break

        return recs

Benefits

  • Fast inference

  • Easy retraining

  • Highly interpretable

Weakness

Cold-start users receive no recommendations.

4.2 Semantic Vector Search (RAG over Course Catalog)

Semantic retrieval solves the cold-start problem.

Instead of user behavior, recommendations are based on meaning and intent.

Each course is embedded and stored in a vector database.

Python
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from langchain.docstore.document import Document

class SemanticRetriever:
    def __init__(self, courses):
        self.embeddings = OpenAIEmbeddings(
            model="text-embedding-3-small"
        )

        docs = [
            Document(
                page_content=(
                    f"{c['title']}. "
                    f"{c['description']}. "
                    f"Skills: {', '.join(c['skills'])}"
                ),
                metadata={
                    "course_id": c["id"],
                    "level": c["level"],
                    "hours": c["hours"]
                }
            )
            for c in courses
        ]

        self.vectorstore = FAISS.from_documents(
            docs,
            self.embeddings
        )

    def search(self, query, k=10, filters=None):
        kwargs = {"k": k}

        if filters:
            kwargs["filter"] = filters

        results = (
            self.vectorstore
            .similarity_search_with_score(query, **kwargs)
        )

        return [
            {
                "course_id": doc.metadata["course_id"],
                "title": doc.metadata.get(
                    "title",
                    doc.page_content[:60]
                ),
                "score": float(score),
                "source": "semantic",
                "content": doc.page_content
            }
            for doc, score in results
        ]

Benefits

  • Handles cold-start scenarios

  • Understands meaning and intent

  • Works immediately for new content

Weakness

Does not incorporate user behavior or peer success patterns.

4.3 LLM Contextual Reasoner

The LLM acts as a decision-maker.

It evaluates:

  • User goals

  • Skill gaps

  • Time constraints

  • Collaborative signals

  • Semantic signals

and generates the final ranking.

Python
from pydantic import BaseModel, Field
from langchain_openai import ChatOpenAI

class Recommendation(BaseModel):
    course_id: str
    title: str
    final_score: float = Field(
        description="0-100 composite score"
    )
    rationale: str = Field(
        description="User-facing explanation"
    )

class RankedRecommendations(BaseModel):
    recommendations: list[Recommendation]
    summary: str

class ContextualReasoner:
    def __init__(self):
        self.llm = (
            ChatOpenAI(
                model="gpt-4o-mini",
                temperature=0
            )
            .with_structured_output(
                RankedRecommendations
            )
        )

    def rank(
        self,
        user_context,
        candidates
    ):
        prompt = f"""
You are an enterprise learning advisor.

USER CONTEXT:
{user_context}

CANDIDATES:
{candidates}

Instructions:
- Prefer dual-source recommendations.
- Respect weekly time limits.
- Prioritize skill gaps.
- Return top 5 recommendations.
"""

        return self.llm.invoke(prompt)

Benefits

  • Personalized recommendations

  • Human-readable explanations

  • Goal-aware decision making

5. Multi-Agent LangGraph Orchestration

LangGraph coordinates all recommendation components.

5.1 Shared State

Python
from typing import Annotated, Any
from langgraph.graph import StateGraph
from langgraph.graph.message import add_messages
from langchain_core.messages import BaseMessage

class RecState(dict):
    user_id: str
    messages: Annotated[
        list[BaseMessage],
        add_messages
    ]

    user_context: dict

    cf_candidates: list[dict]
    semantic_candidates: list[dict]

    final_recommendations: Any

    plan: list[str]

The state acts as a shared memory object across all agents.

5.2 Agent Nodes

Router Agent

Determines which recommendation specialists should run.

Python
from langchain_openai import ChatOpenAI

router_llm = ChatOpenAI(
    model="gpt-4o-mini",
    temperature=0
)

def router_node(state):
    plan = (
        router_llm.invoke(
            f"""
Request:
{state['messages'][-1].content}

User Context:
{state['user_context']}

Determine required agents:
cf, semantic, context
"""
        )
        .content
        .strip()
        .lower()
        .split(",")
    )

    return {
        "plan": [
            p.strip()
            for p in plan
        ]
    }

Collaborative Filtering Agent

Python
def cf_node(state):
    recs = cf_engine.recommend(
        state["user_id"],
        top_k=10
    )

    return {
        "cf_candidates": recs
    }

Semantic Search Agent

Python
def semantic_node(state):
    query = (
        f"{state['messages'][-1].content}. "
        f"Goal: {state['user_context']['goal']}"
    )

    recs = semantic.search(
        query,
        k=10,
        filters={
            "level":
            state["user_context"]["level"]
        }
    )

    return {
        "semantic_candidates": recs
    }

Context Agent

Merges results and invokes the LLM reasoner.

Python
def context_node(state):
    merged = {}

    for c in (
        state["cf_candidates"]
        + state["semantic_candidates"]
    ):
        cid = c["course_id"]

        if cid not in merged:
            merged[cid] = {
                **c,
                "sources": [c["source"]]
            }
        else:
            merged[cid]["sources"].append(
                c["source"]
            )

    candidates = list(merged.values())

    ranked = reasoner.rank(
        state["user_context"],
        candidates
    )

    return {
        "final_recommendations": ranked
    }

5.3 Graph Construction

Python
def build_graph():
    g = StateGraph(RecState)

    g.add_node("router", router_node)
    g.add_node("cf", cf_node)
    g.add_node("semantic", semantic_node)
    g.add_node("context", context_node)

    return g

LangGraph provides:

  • Workflow orchestration

  • State management

  • Checkpointing

  • Streaming

  • Human-in-the-loop workflows

5.4 Long-Term Memory (User Profile Store)

User profiles persist across sessions.

Python
class UserProfileStore:
    def __init__(self):
        self._store = {}

    def get(self, user_id):
        return self._store.get(
            user_id,
            {
                "role":
                    "Associate Consultant",
                "goal":
                    "Become a Cloud Architect",
                "hours_per_week": 2,
                "level":
                    "intermediate",
                "skill_gaps":
                    [
                        "AWS",
                        "Terraform",
                        "System Design"
                    ]
            }
        )

This long-term memory enables personalization over time.

6. End-to-End Execution

Example request:

Python
print(
    run(
        "emp-4271",
        "I want to move toward cloud architecture but only have 2h/week"
    )
)

Sample Output

Summary: Given your 2-hour weekly budget and goal of becoming a Cloud Architect, I've prioritized practical AWS and Terraform courses that peers in similar roles completed successfully.

  1. AWS Cloud Practitioner Essentials (Score 92)

    • Matches your AWS skill gap and is popular among aspiring architects.

  2. Terraform for AWS - Crash Course (Score 87)

    • Practical hands-on training that fits your available time.

  3. System Design Fundamentals (Score 78)

    • Provides foundational architecture knowledge through short learning modules.

7. Memory & State - What Persists Where

LayerTechnologyStoresLifetime
Working MemoryLangGraph StateCurrent execution stateSingle run
Conversation MemorySqliteSaverThread historyMultiple sessions
User MemoryPostgresGoals, preferences, feedbackLong-term
Knowledge MemoryFAISS / pgvectorCourse embeddingsUntil reindex
Behavioral MemoryRatings DBUser interactionsContinuous retraining

This layered memory architecture separates enterprise systems from simple demos.

8. Production Considerations

Observability

Use LangSmith tracing to monitor:

  • Agent execution

  • Candidate counts

  • Ranking decisions

  • Final recommendations

Guardrails

Add validation agents to:

  • Enforce mandatory training

  • Ensure compliance requirements

  • Prevent invalid recommendations

Fallback Strategies

If collaborative filtering returns no results:

Code
Cold Start User
       ↓
Semantic Search
       ↓
Context Agent
       ↓
Recommendation

Evaluation

Recommended metrics:

  • NDCG@5

  • Recall@K

  • Precision@K

  • LLM-as-Judge for rationale quality

Privacy

Never:

  • Store PII in embeddings

  • Mix tenant data

  • Expose user histories across organizations

9. Why This Architecture Wins

Modularity

Each component can be replaced independently.

Examples:

  • FAISS → pgvector

  • Cosine CF → Neural CF

  • GPT-4o Mini → Local LLM

Explainability

Recommendations include transparent rationales derived from actual signals.

Adaptability

The router dynamically chooses which specialists to use.

Memory-Rich Design

Supports:

  • Short-term memory

  • Long-term memory

  • Behavioral memory

  • Knowledge memory

Enterprise Readiness

LangGraph provides:

  • Checkpointing

  • Auditing

  • Human approvals

  • Stateful workflows

  • Production-grade orchestration

Conclusion

A modern recommendation engine requires more than collaborative filtering or vector search alone. By combining Collaborative Filtering, Semantic Retrieval, LLM-based Contextual Reasoning, and LangGraph orchestration, organizations can deliver highly personalized, explainable, and adaptive recommendations.

Each component addresses the weaknesses of the others:

  • Collaborative Filtering contributes behavioral intelligence.

  • Semantic Search contributes conceptual understanding.

  • LLM Reasoning contributes personalization and explanation.

  • LangGraph contributes orchestration, memory, and state.

Together, they form a scalable enterprise recommendation platform capable of supporting learning systems, e-commerce experiences, career development platforms, content discovery engines, and many other intelligent recommendation use cases.

Summary

Hybrid recommendation systems outperform single-model approaches by combining behavioral signals, semantic understanding, and contextual reasoning. Using Collaborative Filtering, Vector Search, LLM-based ranking, and LangGraph orchestration enables organizations to build recommendation platforms that are accurate, explainable, memory-aware, and enterprise-ready. This architecture provides better personalization, handles cold-start scenarios effectively, and creates a foundation for scalable AI-powered recommendation experiences.