Haziq Ali · Full Stack Developer
73.06°E
LanguageEN
Retrieval · 2024

Intelligent Agent

A LangChain agent that classifies every incoming question and routes it to the right source — chat, your documents, or live web search — instead of retrieving blindly on every turn.

  • LangChain
  • MongoDB Atlas
  • OpenAI
  • Tavily

Overview

Most RAG implementations have the same flaw: they retrieve on every single turn. Ask “hello” and the system dutifully embeds it, searches a vector store, and stuffs five irrelevant document chunks into the prompt. Intelligent Agent was built around the opposite instinct — decide first what kind of question this is, then choose a strategy.

Key Features

  • Query classification with structured output: Every question is first passed to a classifier using a Zod-typed schema that sorts it into one of three categories — normal conversation, context-based response, or real-time information. The category decides what happens next.
  • Three routing paths: Conversational turns answer directly with no retrieval; document questions hit the vector store; questions needing current information go out to live web search via Tavily.
  • Per-user document isolation: The vector retriever applies a userId pre-filter at query time, so a user’s retrieval can only ever touch their own documents — multi-tenancy enforced at the retrieval layer rather than trusted to application code.
  • Multi-format ingestion: Uploads accepted as PDF, DOCX, CSV, and XLSX, each with its own loader before chunking and embedding.

Technologies Used

  • LangChain + @langchain/core: Chain composition, document combination, and output parsing.
  • MongoDB Atlas Vector Search via @langchain/mongodb: Vector store with metadata pre-filtering.
  • OpenAI: GPT-4 for classification and generation, text-embedding-ada-002 for embeddings.
  • Tavily Search API Retriever: Live web retrieval for time-sensitive questions.
  • Express + Multer: Upload handling and API surface.
  • pdf-parse, Mammoth, d3-dsv, xlsx: Document extraction across formats.

Challenges and Learnings

Using an LLM with structured output as a router — rather than a keyword heuristic — turned out to be the design that made everything else simple. Once the model reliably tells you “this needs real-time information,” the retrieval strategy stops being a compromise between competing use cases and becomes a straightforward dispatch. The other lesson was about isolation: pushing the userId filter down into the retriever’s pre-filter, instead of filtering results after they come back, is the difference between a multi-tenant system that’s secure by construction and one that’s secure until someone forgets a check.

Outcome

An agent that gets the boring cases right — it doesn’t search the web to say hello, and it doesn’t hallucinate from stale documents when the question is about today. The routing pattern here is the direct ancestor of the tool-selection work in Core Lens.

Contact

Let's talk.

Based in Düsseldorf. Open to full-stack and lead engineering work, on site in NRW, across Germany, or fully remote.