Skip to main content

Command Palette

Search for a command to run...

Beyond Vector Search: Building an Economically-Aware Cognitive Agent for Recommendations

Updated
6 min read
  Beyond Vector Search: Building an Economically-Aware Cognitive Agent for Recommendations
M
I write about anything in the tech space

If you’ve ever shopped online, you’ve experienced the frustration of generic recommendations. You buy a refrigerator, and suddenly the algorithm thinks you want to start a collection of them. Or worse, you search for a budget smartphone, and the system recommends the latest $1,200 flagship because it has a high "relevance score."

Traditional recommendation systems are powerful, but they suffer from context blindness. They rely on static vector embeddings that match users to items, but they don't understand the reality of the user's situation. An embedding knows you like "Premium Tech," but it doesn't know that local inflation just spiked to 30%, making a flagship purchase a terrible financial decision this month.

For the DSN x BCT Hack Challenge, I wanted to see if I could fix this. The result is the DSN Rec Agent: a cognitive-neuroscience-inspired recommendation framework that moves beyond simple vector matching and acts like a rational, localized consumer.

Here is a deep dive into how I built it, the architecture behind it, and why cognitive agents are the future of e-commerce.


The Core Concept: From Search Engine to Simulated Consumer

The goal was to build an "active agent" that deliberates on the wisdom of a retrieval, rather than just returning the top 5 closest vectors. To do this, the agent needed three things:

  1. A Brain: To reason, reflect, and self-correct.

  2. A Memory: To remember past interactions and abstract insights without just repeating itself.

  3. Economic Grounding: To understand real-world financial contexts like inflation and purchasing power.

To achieve this, I built a containerized FastAPI application utilizing LangGraph for orchestration, ChromaDB for vector storage, and Google Gemini as the core reasoning engine.


  1. The Brain: A Stateful Reasoning DAG with LangGraph

At the heart of the DSN Rec Agent is a Stateful Directed Acyclic Graph (DAG) built with LangGraph. Instead of a linear prompt chain, the agent moves through seven distinct cognitive nodes, maintaining a "Global State" that evolves at each step.

  1. Extraction Node: The user inputs a raw prompt (e.g., "I'm a broke college student looking for headphones"). The agent extracts this into a structured Persona model, inferring traits like "Saver" or "Price-Conscious," and detects the user's geographical location and tone.

  2. Economic Context Node: This is where the magic starts. The agent fetches real-time macroeconomic data (Inflation, PPI, Exchange Rates) for the user's target country using a cache-first DuckDuckGo search fallback.

  3. Memory Retrieval & Reflection: The agent checks ChromaDB for similar past personas. A "Behavioral Analyst" node reviews these memories to generate high-level abstract insights.

  4. Product Discovery: The agent fetches relevant products via semantic search using local HuggingFace embeddings (all-MiniLM-L6-v2).

  5. Reasoning Engine: The LLM deliberates on the products, weighing the persona's traits against the live economic data.

  6. The Critic (Self-Correction Loop): Before showing the user anything, a Verification Node audits the reasoning. If it detects a hallucination (e.g., suggesting an item that isn't in the catalog), it generates a critique and loops back to fix its own mistake.

  7. Task Output: Finally, the agent generates a review or recommendation in the user's regional slang and verbosity.

Takeaway: The Critic Loop was critical. In ablation studies, removing the self-reflection node caused hallucination rates to jump by 40%.


  1. The Memory: Modeling Human Retention

If an agent remembers everything perfectly forever, it becomes rigid. If it forgets everything, it’s useless. To solve this, I implemented a Tri-Layer Memory System inspired by cognitive neuroscience:

  • Sensory Memory: Rapidly processes raw environmental data and product specs (highly volatile).

  • Short-Term Memory: Manages session context and tracks the agent's internal reasoning logs.

  • Long-Term Memory (ChromaDB): Persists abstract insights.

To prevent the agent from getting stuck on old trends, I implemented a mathematical Forgetting Mechanism. Using a power-law decay function, the system prunes low-value memories based on their initial strength (\(s_i\)) and exponential time-decay (\(r_i\)).

Furthermore, to prevent "Model Inbreeding" (where the model starts repeating its own generated reviews), the agent only saves third-person objective behavioral summaries to long-term memory, rather than the raw AI-generated text.


  1. Grounding in Economic Reality & Local Culture

A recommendation agent that doesn't understand what it's like to shop locally is essentially useless.

Economic Rationality: By injecting live macroeconomic data into the prompt state, the agent scales its recommendations. If a persona is marked as a "Saver" and the target country has high inflation, the agent will aggressively filter out luxury items and actively advise the user on finding the best utility-to-cost ratio.

Universal Linguistic Mirroring: The agent captures the user's specific regional dialect during the extraction phase. The final output nodes are hard-constrained to "embody" that location. If you prompt it as a 28-year-old tech enthusiast from Lagos, the review will sound like a local shopping assistant not a generic corporate blog. It even dynamically scales its verbosity; keeping reviews punchy for utilitarian items like cables, and narrative for experiential items like books.


Results and What I Learned

I evaluated the agent against a dataset of 21,000 Amazon reviews and a simulated local catalog. The results were incredibly promising:

  • Sentiment Alignment: 0.70 (High accuracy in mimicking human emotional states).

  • Rating Error (RAE): 1.2 Stars (It actually utilized the full 1-5 scale, avoiding the classic AI bias of rating everything 4 or 5 stars).

  • Economic Rationality Index: 4.2 / 5 (Strong alignment with budget and inflation context).

  • Top-3 Retrieval Relevance: 88%


Conclusion

Building the DSN Rec Agent proved to me that the next generation of AI is about building better cognitive architectures.

By orchestrating LLMs inside a directed graph with robust memory hygiene, self-reflection loops, and real-world economic grounding, we can move from simple retrieval systems to culturally wise and deeply personalized consumer agents.

This hackathon was a fantastic sandbox for testing agentic patterns. If you're building LLM applications, I highly recommend exploring LangGraph for state management and implementing explicit "Critic" nodes. Your users (and your logs) will thank you.

You can check out the architecture diagrams and the code for the project on my GitHub.

https://github.com/matt-wisdom/BCTHack


Have you experimented with Stateful DAGs or Agentic Memory? Let's discuss in the comments!