The Recommendation Engine Engineer builds systems that surface relevant content, products, or people to each user. The right algorithm depends on what you know: collaborative filtering requires interaction history (users who liked X also liked Y); content-based filtering requires item features (recommend items with similar attributes). New products need embeddings-based semantic similarity to bootstrap without interaction data.
Algorithm Selection
| Algorithm | When to use | Cold start? |
|---|---|---|
| Collaborative filtering | Large user-item interaction matrix; similar users have similar tastes | ❌ Needs history |
| Content-based | Rich item metadata; new items without interaction data | ✅ Works immediately |
| Embedding similarity | Text/image content; semantic similarity matters | ✅ Works immediately |
| Hybrid (most common) | Combine above; fallback chain for cold start | ✅ With fallback |
| BPR (Bayesian Personalized Ranking) | Implicit feedback (clicks, views); no explicit ratings | ❌ Needs history |
Embedding-Based Recommendations
// Generate item embeddings and find nearest neighbours
// Works for: articles, products, videos - any content with text description
import Anthropic from '@anthropic-ai/sdk';
import { PineconeClient } from '@pinecone-database/pinecone';
const client = new Anthropic();
const pinecone = new PineconeClient();
// 1. Index items at upload time
async function indexItem(item: { id: string; title: string; description: string }) {
const embeddingResponse = await client.messages.create({
model: 'claude-opus-5',
max_tokens: 1,
messages: [{ role: 'user', content: item.description }],
// In practice use a dedicated embedding API; this illustrates the concept
});
// Use a proper embedding model in production:
// const { embedding } = await cohere.embed({ texts: [item.description], model: 'embed-english-v3.0' });
const index = pinecone.Index('items');
await index.upsert({
vectors: [{
id: item.id,
values: embedding, // 1024-dim vector
metadata: { title: item.title, category: item.category },
}],
});
}
// 2. Get recommendations for an item
async function getSimilarItems(itemId: string, limit = 10) {
const index = pinecone.Index('items');
const targetVector = await index.fetch({ ids: [itemId] });
const vector = targetVector.vectors[itemId].values;
const results = await index.query({
vector,
topK: limit + 1, // +1 because the item itself is returned
includeMetadata: true,
filter: { /* optional: same category */ },
});
return results.matches
.filter(m => m.id !== itemId) // Exclude the input item
.slice(0, limit)
.map(m => ({ id: m.id, score: m.score, title: m.metadata!.title }));
}
Cold Start Handling
// Fallback chain: personalised → trending → editorial
async function getRecommendations(userId: string, context: string): Promise<Item[]> {
// 1. Try personalised (requires interaction history)
const history = await getUserHistory(userId);
if (history.length >= 5) {
const personalised = await collaborativeFilter(userId);
if (personalised.length > 0) return personalised;
}
// 2. Fall back to content-based on current context
if (context) {
const contextBased = await embeddingSearch(context, 10);
if (contextBased.length > 0) return contextBased;
}
// 3. Last resort: trending items (time-decayed popularity)
return getTrendingItems(10);
}