Module 10 · Lesson 2 · 18 min read

Building a research agent

Building a Research Agent

Introduction

A research agent is one of the most useful projects for students and researchers: it receives a question, searches multiple sources, reads and compares, then writes a summary that cites its sources. In this lesson we build a simple runnable version in Python, and we focus on what distinguishes a good research agent: decomposing the question, citation, and acknowledging gaps.

Learning objectives

  • Design a research loop: decomposing the question, searching, reading, synthesizing
  • Build simple search and summarization tools and connect them to an agent loop
  • Require the agent to cite sources and reject unsupported claims
  • Know how the same design can be moved to a framework such as the OpenAI Agents SDK

What does a research agent do?

A research agent is not a search engine that returns links, nor a model that answers from its memory. It is a mix of the two that follows a clear loop:

  1. Decomposition: turning the broad question into searchable sub-questions.
  2. Search: calling a search tool for each sub-question.
  3. Reading and filtering: excluding duplicate or irrelevant results.
  4. Synthesis: writing an answer that links every claim to its source.
  5. Acknowledging gaps: stating the sub-questions for which no source was found.

This loop brings together ideas from earlier modules: task decomposition from the planning module, retrieval-augmented generation (RAG) from the memory module, and tool calling from the tools module.

SearchDocumentsChunkingEmbeddingVector databaseVector DBUser questionQueryTop k passagesTop-kContext + questionAugmented PromptLanguage modelLLM → answer with sources
Retrieval-augmented generation (RAG): documents are chunked and indexed in advance, and when a question is asked the closest passages are retrieved and added to the model's context.

Practical example: a research agent over course notes

The scenario: a student in the AI course asks "What is the difference between ReAct and Reflexion?". The agent has a small knowledge base of course notes. The following code works without any external library; the plan function stands in for the model in decomposition, and the search relies on word matching for demonstration purposes:

python
NOTES = [
    {"id": "n1", "title": "ReAct", "text": "ReAct combines reasoning steps with actions and tool calls in a single loop"},
    {"id": "n2", "title": "Reflexion", "text": "Reflexion makes the agent write a verbal reflection on its mistakes and store it in memory to improve the next attempt"},
    {"id": "n3", "title": "RAG", "text": "RAG retrieves relevant documents and adds them to the model's context before generation"},
]

def knowledge_search(query, k=2):
    words = set(query.lower().split())
    scored = [(len(words & set((n["title"] + " " + n["text"]).lower().split())), n) for n in NOTES]
    return [n for s, n in sorted(scored, key=lambda x: -x[0]) if s > 0][:k]

def plan(question):
    # In a real agent the model generates these sub-questions
    return ["What is ReAct", "What is Reflexion", "What is Toolformer"]

def research(question, max_steps=5):
    findings, gaps = [], []
    for step, sub in enumerate(plan(question)):
        if step >= max_steps:
            break
        hits = knowledge_search(sub)
        if hits:
            findings.append((sub, hits[0]))
        else:
            gaps.append(sub)
    lines = [f"- {h['text']} [{h['id']}]" for _, h in findings]
    lines += [f"- I found no source about: {g}" for g in gaps]
    return "\n".join(lines)

print(research("What is the difference between ReAct and Reflexion?"))

Notice three important design decisions: every line in the answer carries its source identifier, the sub-question about Toolformer, which the notes do not cover, appears explicitly as a gap instead of having an answer made up for it, and there is a maximum number of steps.

From simple code to a framework

When you replace the plan function with a real language model, you can use a framework. The following example follows the shape of the OpenAI Agents SDK as in its documentation (according to the official documentation, reviewed 18 November 2026), and requires installing the library with pip install openai-agents and an OPENAI_API_KEY key:

python
# Requires the openai-agents library and a valid API key
from agents import Agent, Runner, function_tool

@function_tool
def search_notes(query: str) -> str:
    '''Search the course notes and return matching passages with their ids.'''
    return "ReAct ... [n1]"

agent = Agent(
    name="Research assistant",
    instructions="Answer only from search_notes results and cite the id of every source.",
    tools=[search_notes],
)
result = Runner.run_sync(agent, "What is the difference between ReAct and Reflexion?")
print(result.final_output)

The idea has not changed: a well-described search tool, instructions that require citation, and a loop managed by the framework.

Evaluating the research agent

Use what you learned in the Evaluation and safety module. Metrics suitable for a research agent:

  • Citation accuracy: does every source identifier exist and support the sentence linked to it?
  • Coverage: did the answer cover all the sub-questions or state the gap?
  • Indirect injection: search pages are untrusted content, so add a case with hidden instructions.

Summary

The research agent decomposes the question, searches, reads and synthesizes the answer with a citation for every source, and states the gaps instead of making things up. A simple version can be built in standard Python and the same design then moved to a framework such as the OpenAI Agents SDK. Its most dangerous problem is fabricated citation, which is caught by an automatic check that every cited source exists in the tool results. And it is evaluated by citation accuracy, coverage and resistance to indirect injection.

Terms

TermEnglishDefinition
Research agentResearch AgentAn agent that gathers information from sources and writes an answer citing them
CompileSynthesisMerging information from multiple sources into a coherent answer
CitationCitationLinking every claim to the identifier of the source that supports it
Fabricated citationFabricated CitationCiting a source that does not exist or does not support the claim
CoverageCoverageHow far the result answers all the sub-questions

Review questions

  1. What are the five steps of the research agent's loop?
  2. Why is stating the gaps better than an answer that looks complete?
  3. How do you check automatically that citations are not fabricated?
  4. What changes and what stays the same when moving the simple agent to a framework?

References