{"slug": "when-to-use-an-agent-and-when-a-chain-notes-from-my-research-pipeline", "title": "When to use an agent and when a chain: notes from my research pipeline", "summary": "A developer built a multi-agent AI research pipeline that searches the web, scrapes pages, writes a Markdown report and has a critic score it, deliberately using LangChain agents only for the two open-ended steps (search and read) and LCEL chains for the fixed steps (write and critique). The search agent, using a Tavily-backed web_search tool, picks queries and returns five sources, while the reader agent uses a BeautifulSoup scrape_url tool to pick two pages; tool outputs are capped at 300 characters per snippet, 1,500 per page and 2,500 overall to stay within token limits. The critic returns a Pydantic-structured score, and reports scoring below MIN_GOOD_SCORE = 7 get one rewrite, with a score of 0 reserved to signal a failed review rather than a bad report.", "body_md": "My [Multi-Agent AI Research System](https://www.sangamgupta.in/work/multi-agent-research) takes a topic, searches the web, reads the most useful pages, writes a Markdown report and then has a critic score it. If it scores below 7 out of 10, the report goes back for one rewrite. The agents, tools, pipeline and feedback loop are my work, with AI help in places; the Streamlit interface was built with AI help.\n\nIt's called multi-agent, but only two of its four steps are agents. That split was the main design decision, so this post is about how I made it.\n\nThe rule I followed: if the model needs to decide, use an agent; if the task is fixed, use a chain.\n\nAn agent, built with LangChain's `create_agent`, is given tools and chooses for itself whether to call them, which one, and how many times. A chain built with LCEL is a fixed sequence: prompt, then model, then parser, every time. This is how the README sums up the first three steps:\n\n| Step | Built with | Why | \n|---|---|---|\n| Search | `create_agent` +`web_search` tool | The LLM decides what and how many times to search | \n| Read | `create_agent` +`scrape_url` tool | The LLM decides which URLs are worth reading | \n| Write | LCEL chain (`prompt \\ | model \\ | \n\nThe critic is a chain too, with Pydantic structured output on the end. Writing a report from notes, or reviewing one, is the same job every time. Searching and reading are not: which queries to run, how many, and which pages are worth opening depend on the topic, so those two steps let the model decide.\n\nThe search agent has one tool, {% raw %}`web_search`, built on the Tavily API. The model decides what to search and how often, and returns the five best sources. The reader agent has `scrape_url`, which fetches a page with `requests` and pulls out clean text with BeautifulSoup. It picks the two most useful pages to read, and skips sites that can't be scraped.\n\nBoth steps feed text into a model with token limits, so the tools cut it down first. Each search snippet is capped at 300 characters, the text of each scraped page at 1,500, and the whole search output passed to the reader at 2,500. The comment next to the page limit in `tools.py` says why: so the LLM doesn't exceed token limits.\n\nThe writer is a chain that turns the research notes into a Markdown report with an introduction, key findings, analysis, a conclusion and sources.\n\nThe critic uses the same model as the writer, with a smaller output limit. Instead of asking it for a free-text review, I give it a Pydantic schema through `with_structured_output`:\n\n```\nclass Critique(BaseModel):\n    \"\"\"Review of a research report.\"\"\"\n    score: int = Field(description=\"Overall quality score from 1 (poor) to 10 (excellent)\")\n    strengths: List[str] = Field(description=\"What the report does well (at most 4 items)\")\n    improvements: List[str] = Field(description=\"Specific suggestions to improve the report (at most 4 items)\")\n    verdict: str = Field(description=\"One-line overall verdict\")\n```\n\nBecause the score comes back as an integer field, the pipeline can compare it with a number directly instead of searching the critic's text for one. The improvements list is what the writer gets if a rewrite is needed.\n\nThe threshold is one constant in `pipeline.py`: `MIN_GOOD_SCORE = 7`. If the critic scores a report below 7, the writer rewrites it once using the critic's feedback, and the critic reviews the new version.\n\nThere is one case where the loop deliberately does nothing. If the review itself fails, the critique comes back with a score of 0, the improvement \"Automatic review failed. Please review the report manually.\" and the verdict \"Review unavailable\". A score of 0 means the review failed, not that the report is bad, so the pipeline doesn't rewrite in that case.\n\nThe app shows the result as three numbers: the critic score, whether the report was rewritten, and a verdict of Good for 7 or more, otherwise Needs work. In the sample run on \"Claude Fable 5.2\", the critic scored the report 8/10, so no rewrite was needed. The [sample report](https://github.com/TheSangamX/Multi-Agent-AI-Research-System/blob/main/assets/sample-report-claude-fable-5-2.pdf) is in the repo.\n\nThe models are Groq's `openai/gpt-oss-20b` and `openai/gpt-oss-120b`. On Groq each of them has its own daily token limit, so the model names are set in one place in `agents.py` and can be switched if one model's quota runs out.\n\nThe free tier also limits tokens per minute. The pipeline handles the two kinds of limit differently:\n\nThe README lists what I want to add: entering your own API key in the interface, exporting the report as a PDF, running search and reading for several sub-questions in parallel, scoring sources for credibility, and telling apart people with the same name when the topic is a person.\n\nThe app is [live on Streamlit Community Cloud](https://thesangamx-multi-agent-ai-research-system.streamlit.app), the code is on [GitHub](https://github.com/TheSangamX/Multi-Agent-AI-Research-System), and the [case study](https://www.sangamgupta.in/work/multi-agent-research) has the full flow.\n\n*First published on [sangamgupta.in](https://www.sangamgupta.in/writing/when-to-use-an-agent-and-when-a-chain).*", "url": "https://wpnews.pro/news/when-to-use-an-agent-and-when-a-chain-notes-from-my-research-pipeline", "canonical_source": "https://dev.to/thesangamx/when-to-use-an-agent-and-when-a-chain-notes-from-my-research-pipeline-5b6n", "published_at": "2026-11-30 15:10:30+00:00", "updated_at": "2026-11-30 15:16:11.008949+00:00", "lang": "en", "topics": ["ai-agents", "large-language-models", "ai-tools", "developer-tools"], "entities": ["LangChain", "LCEL", "Tavily", "BeautifulSoup", "Streamlit", "Pydantic"], "also_reported_by": [], "takeaway": "Use an agent only where the model must decide (which queries to run, which pages to read) and a fixed LCEL chain where the task is identical every time, capping tool output at 300 characters per snippet, 1,500 per page and 2,500 overall to stay inside token limits.", "recommendation": {"advice": "Split pipelines by decision-making: give agents tools only for open-ended steps like search and page selection, and use fixed chains for writing and critique, capping tool output to avoid token limits.", "playbook": "agent-architecture", "playbook_url": "https://wpnews.pro/playbooks/agent-architecture"}, "alternates": {"html": "https://wpnews.pro/news/when-to-use-an-agent-and-when-a-chain-notes-from-my-research-pipeline", "markdown": "https://wpnews.pro/news/when-to-use-an-agent-and-when-a-chain-notes-from-my-research-pipeline.md", "text": "https://wpnews.pro/news/when-to-use-an-agent-and-when-a-chain-notes-from-my-research-pipeline.txt", "jsonld": "https://wpnews.pro/news/when-to-use-an-agent-and-when-a-chain-notes-from-my-research-pipeline.jsonld"}}