Listen to this Post

Introduction:
The landscape of artificial intelligence is shifting from monolithic models to orchestrated systems of specialized agents. While a single Large Language Model (LLM) can generate impressive text, it often falls short when faced with complex, multi-step tasks that require real-time data retrieval, critical analysis, and iterative refinement. The future of AI lies in Agentic AI—where autonomous agents collaborate, each wielding specific tools and responsibilities, to solve problems too intricate for a single prompt. This article dissects a production-style Multi-Agent AI Research System built with LangChain, demonstrating how to decompose the research process into a pipeline of searching, reading, writing, and critiquing, ultimately producing higher-quality, verifiable reports.
Learning Objectives:
- Understand the architectural principles of multi-agent systems and how task decomposition enhances output quality.
- Learn to implement and orchestrate specialized AI agents for web search, content scraping, report writing, and critique using LangChain.
- Gain practical skills in integrating external tools (Tavily, BeautifulSoup) and LLMs (Mistral/Gemini) into a cohesive, interactive application.
1. Deconstructing the Research Pipeline: Agent by Agent
Real-world research isn’t a linear, one-step process; it’s a cyclical workflow of discovery, analysis, and synthesis. This system mirrors that by assigning distinct roles to four AI agents, each operating with a specific prompt and set of tools.
- Search Agent: This agent is the system’s scout. Equipped with the Tavily Search API, it performs live web research based on the user’s query. Unlike a generic search, Tavily is optimized for AI, returning structured results including titles, URLs, and snippets, which reduces hallucination by grounding the subsequent agents in real-world data.
- Reader Agent: Raw search results are often noisy. The Reader agent takes the top URLs from the search results, fetches the full HTML, and uses BeautifulSoup to extract clean, readable text. It strips out navigation menus, scripts, and styles, leaving only the core content for analysis.
- Writer Agent: This agent is the system’s synthesizer. Using a carefully engineered prompt, it consumes the aggregated search results and the detailed scraped content to generate a structured research report. The report follows a professional template, including an introduction, key findings, a conclusion, and a list of sources—transforming scattered data into a coherent narrative.
- Critic Agent: The final piece of the puzzle is the Critic agent. It functions as a quality assurance specialist, reviewing the draft report and providing a score, a list of strengths, and specific areas for improvement. This feedback loop ensures the final output is not only informative but also rigorously vetted.
- The Orchestration Engine: Connecting the Dots with LangChain
The true power of this system isn’t just in the individual agents but in how they are orchestrated. The `pipeline.py` file serves as the conductor, managing the state and the flow of information between each agent. This is where the “systems engineering” mindset becomes critical.
The workflow is a sequential, stateful pipeline:
- State Initialization: The pipeline begins with an empty dictionary `state = {}` that will hold all data generated throughout the process.
- Search Execution: The `build_search_agent()` is invoked, and the user’s topic is passed as a message. The agent uses the `web_search` tool and returns a structured list of results, which are then stored in
state["search_results"]. - Reading and Scraping: The `build_reader_agent()` is then called. Its prompt instructs it to pick the most relevant URL from the previous search results and scrape its content. The extracted text is saved to
state["scraped_content"]. - Report Writing: The
writer_chain, which is not an agent but a LangChain `chain` (prompt + LLM + output parser), is invoked. It combines the search results and scraped content into a single context and generates the final report, stored instate["report"]. - Critique and Feedback: Finally, the `critic_chain` reviews the report and generates constructive feedback, which is stored in
state["feedback"]. The pipeline returns the complete `state` dictionary.
This modular design allows for easy debugging, swapping of components (e.g., using Gemini instead of Mistral), and future enhancements like adding a fact-checking agent.
3. Setting Up the Development Environment
To build and run this system, you need to establish a robust Python environment. The `requirements.txt` file provides a comprehensive list of all necessary libraries. Here’s how to set it up on both Linux and Windows.
Linux/macOS Setup:
1. Create a virtual environment python3 -m venv research-env source research-env/bin/activate <ol> <li>Install core dependencies pip install langchain langchain-core langchain-community pip install langchain-mistralai mistralai pip install tavily-python beautifulsoup4 lxml requests pip install python-dotenv streamlit rich
Windows Setup (PowerShell):
1. Create a virtual environment python -m venv research-env .\research-env\Scripts\activate <ol> <li>Install dependencies (same packages) pip install langchain langchain-core langchain-community pip install langchain-mistralai mistralai pip install tavily-python beautifulsoup4 lxml requests pip install python-dotenv streamlit rich
Environment Variables:
Create a `.env` file in your project root to store your API keys securely:
TAVELY_API_KEY="your_tavily_api_key" MISTRAL_API_KEY="your_mistral_api_key" Or if using Gemini: GOOGLE_API_KEY="your_google_api_key"
The system uses `python-dotenv` to load these variables, keeping sensitive credentials out of the source code.
4. Building the Tools: The Agent’s Hands
Tools are the interface through which agents interact with the external world. In this system, two core tools are defined: `web_search` and scrape_url. They are decorated with @tool, making them LangChain-compatible.
The `web_search` Tool:
This function initializes a `TavilyClient` using the API key from the environment. It accepts a query string, executes a search with max_results=5, and formats the output into a readable string containing titles, URLs, and snippets. This structured output is crucial for the LLM to parse and understand the search results.
The `scrape_url` Tool:
This tool handles the extraction of text from a given URL. It uses the `requests` library to fetch the page and `BeautifulSoup` to parse the HTML. A critical step is the removal of non-content elements like <script>, <style>, <nav>, and `


