Skip to content
Shafin Zaman

AI agents

Research agent

Give it a topic and it plans sub-questions, searches the web, and returns a short report where every claim links to a source.

Intermediate 3 to 4 daysFree stack

Copy or download the full plan and paste it into your AI coding agent to build it.

Why build it

Asking a language model a broad question gets you a confident essay built from stale training data with no way to check it. A research agent flips that: it decomposes the topic into sub-questions, calls a real search tool for each, and writes only from what it actually found. This is the first project where you see an agent loop in action, meaning a model that decides what to do next instead of answering in one shot. It also teaches the hardest agent skill, which is knowing when to stop searching and start writing. Grounded, cited research is a pattern clients pay for because it is auditable.

Who it's for: Anyone who has built a basic chatbot and wants their first true agent. If you can call an LLM API and a fetch endpoint, you are ready for this.

What you'll build

Core (MVP)

  • Accept a topic or question as input
  • Plan step: model breaks the topic into 3 to 6 sub-questions
  • Web search tool the agent calls per sub-question
  • Fetch and summarize the top results into notes
  • A stop condition so the loop ends instead of running forever
  • Final report with inline citations back to source URLs
  • Stream the agent's steps so you can watch it think

Stretch

  • Let the agent add follow-up sub-questions when a gap appears
  • Deduplicate sources and rank them by relevance
  • Export the report as Markdown with a numbered reference list
  • Cache searches so repeated topics are near-instant
  • Add a token and step budget shown live in the UI

Step-by-step build

  1. 1

    Scaffold the app and wire one LLM call

    Create a Next.js app and add a single API route that sends a prompt to Groq and returns the text. Confirm the key works and you get a response before adding any agent logic. This is your baseline; everything else builds on a working model call.

  2. 2

    Add the planner

    Prompt the model to turn the topic into 3 to 6 focused sub-questions and return them as a JSON array validated by Zod. Keep the planner separate from the searcher so you can test it alone. A good plan is the difference between a focused report and a rambling one.

  3. 3

    Build the search tool

    Wrap a free search API in a function that takes a query and returns titles, snippets, and URLs. A tool is just a function the model is allowed to call with structured arguments. Test it directly with a hardcoded query before letting the model drive it.

  4. 4

    Register the tool with the model

    Using the Vercel AI SDK, describe the search tool to the model with a name, a description, and a Zod input schema. Now the model can choose to call it and you run the function on its behalf. Log every tool call so you can see exactly what the agent searched for.

  5. 5

    Run the agent loop

    Let the model alternate between calling the search tool and reading results until it decides it has enough. The SDK feeds each tool result back into the conversation automatically. Cap the loop at a maximum number of steps so a confused agent cannot spin forever.

  6. 6

    Add the stop condition

    Give the model an explicit instruction to stop searching once each sub-question is answered, then write the report. Combine that soft signal with the hard step cap from the previous step. Knowing when to stop is the skill that separates a real agent from an infinite loop.

  7. 7

    Write the cited report

    Prompt the model to synthesize the collected notes into a short report where each claim references the source URL it came from. Reject the answer if it cites a URL that was never fetched. This forces the report to stay grounded in what the agent actually read.

  8. 8

    Stream the steps to a UI

    Build a simple page that shows the plan, each search as it happens, and the final report as it streams in. Watching the agent work makes debugging far easier and demos far better. Add a loading state for each step so the UI never looks frozen.

Done when

  • Every claim in the final report links to a source the agent actually fetched
  • The loop always terminates, either by the stop signal or the step cap
  • Asking about an obscure topic yields a short honest report, not padding
  • You can read the log and see each sub-question and the search behind it
  • Removing the search tool makes the agent say it cannot research, not guess

Ship it

Deploy to Vercel free tier; the streaming API route runs on their edge or serverless functions with no config. Put your Groq and search keys in Vercel environment variables. Write a README that shows a real example topic, the report it produced, and the free-tier limits of each API.

What it proves: Shows you can build an agent loop with real tool calls and keep its output grounded and cited rather than hallucinated.

Hand it to your AI agent

Paste this into Cursor, Claude, or ChatGPT and build it step by step.

You are my senior AI engineer pair. Help me build "Research agent" step by step. An agent here means an LLM that loops, deciding to call tools until it has enough to answer. Tool-calling means the model asks to run a function and I run it and feed back the result.

Stack: Next.js, Vercel AI SDK 6 for the tool-calling loop and streaming, Groq API for inference, and a free web search API (Tavily or Brave) as the one tool.

Requirements:
1. Take a topic and plan 3 to 6 sub-questions as validated JSON.
2. Expose a web search tool the model can call per sub-question.
3. Run an agent loop with both a stop instruction and a hard step cap.
4. Produce a short report where every claim cites a URL that was actually fetched.
5. Stream the plan, searches, and report to a simple UI.

Work in this order: scaffold and one LLM call, then the planner, then the search tool, then registering it, then the loop and stop condition, then the cited report, then the UI. Give me commands and code for each step and STOP after each so I can test. Do not write the whole app at once.

More in AI agents

Building this? I post a new AI project plan on LinkedIn most weeks. Follow along and share what you ship.