Skip to content
Sangam Gupta
All writing

When to use an agent and when a chain: notes from my research pipeline

30 November 2026, 4 min read

My Multi-Agent AI Research System 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.

It'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.

The research app after a run on Claude Fable 5.2: the critic scored the report 8/10 and it was not rewritten
The app after the sample run: score 8/10, no rewrite needed.

The rule

The rule I followed: if the model needs to decide, use an agent; if the task is fixed, use a chain.

An 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:

Step Built with Why
Search create_agent + web_search tool The LLM decides what and how many times to search
Read create_agent + scrape_url tool The LLM decides which URLs are worth reading
Write LCEL chain (prompt | model | parser) Fixed task: research in, report out

The 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.

Search and read

The search agent has one tool, 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.

Both 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.

Bar chart of the text limits in the pipeline: 300 characters per search snippet, 1,500 per scraped page and 2,500 for the search output sent to the reader
Text is cut down before it reaches the model.

Write and review

The writer is a chain that turns the research notes into a Markdown report with an introduction, key findings, analysis, a conclusion and sources.

The 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:

class Critique(BaseModel):
    """Review of a research report."""
    score: int = Field(description="Overall quality score from 1 (poor) to 10 (excellent)")
    strengths: List[str] = Field(description="What the report does well (at most 4 items)")
    improvements: List[str] = Field(description="Specific suggestions to improve the report (at most 4 items)")
    verdict: str = Field(description="One-line overall verdict")

Because 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.

The feedback loop

The 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.

There 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.

The 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 is in the repo.

Staying inside Groq's free tier

The 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.

The free tier also limits tokens per minute. The pipeline handles the two kinds of limit differently:

  • A "tokens per minute" error (HTTP 429) is worth waiting for. The pipeline reads the wait time from the error message, such as "Please try again in 2.6s" or "1m5.2s", defaulting to 15 seconds, then retries, up to five times.
  • A "tokens per day" error is not. Waiting a few seconds won't help, so it doesn't retry.

What's next

The 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.

The app is live on Streamlit Community Cloud, the code is on GitHub, and the case study has the full flow.

Get new posts by email

One email when I publish something. No spam, unsubscribe any time.