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.
| Paradigm | Captures | Blind Spot |
|---|---|---|
| Collaborative Filtering (CF) | Taste similarity, emergent communities | Cold-start users/items, lacks semantic understanding |
| Semantic Vector Search | Content meaning, conceptual similarity | Ignores social proof and popularity patterns |
| LLM Contextual Reasoning | Goals, constraints, explanations, personalization | Can 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

The recommendation pipeline consists of:
Collaborative Filtering Engine
Semantic Search Engine
LLM Contextual Reasoner
LangGraph Multi-Agent Orchestrator
Persistent Memory Layer
The workflow:
User Request
↓
Router Agent
↓
┌───────────────┬───────────────┐
│ Collaborative │ Semantic │
│ Filtering │ Search │
└───────────────┴───────────────┘
↓
Context Agent
↓
LLM Reasoner
↓
Final Ranking
↓
User Response4. 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.
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 recsBenefits
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.
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.
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
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.
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
def cf_node(state):
recs = cf_engine.recommend(
state["user_id"],
top_k=10
)
return {
"cf_candidates": recs
}Semantic Search Agent
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.
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
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 gLangGraph 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.
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:
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.
AWS Cloud Practitioner Essentials (Score 92)
Matches your AWS skill gap and is popular among aspiring architects.
Terraform for AWS - Crash Course (Score 87)
Practical hands-on training that fits your available time.
System Design Fundamentals (Score 78)
Provides foundational architecture knowledge through short learning modules.
7. Memory & State - What Persists Where
| Layer | Technology | Stores | Lifetime |
|---|---|---|---|
| Working Memory | LangGraph State | Current execution state | Single run |
| Conversation Memory | SqliteSaver | Thread history | Multiple sessions |
| User Memory | Postgres | Goals, preferences, feedback | Long-term |
| Knowledge Memory | FAISS / pgvector | Course embeddings | Until reindex |
| Behavioral Memory | Ratings DB | User interactions | Continuous 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:
Cold Start User
↓
Semantic Search
↓
Context Agent
↓
RecommendationEvaluation
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.


Join the conversation! Your thoughts help the community grow.