An implementation of iterative deep research using the OpenAI Agents SDK
An implementation of iterative deep research using the OpenAI Agents SDK
A powerful deep research assistant built using the OpenAI Agents SDK, designed to perform in-depth research on any given topic. Compatible with Azure OpenAI, OpenAI, Anthropic, Gemini, DeepSeek, Perplexity, OpenRouter, Hugging Face and local models such as Ollama.
It uses a multi-agent architecture that works iteratively, continually refining its understanding of a topic and producing increasingly detailed insights that feed the final report.
Designed to be extendable to use custom tools and any other 3rd party LLMs compatible with the OpenAI API spec. LLM and tool calls can be optionally traced using OpenAI's tracing feature.
Some background reading here.
This package has two modes of research:
IterativeResearcher which runs a continuous loop of research on a topic or sub-topic and drafts a reportDeepResearcher which runs a more thorough and structured process, first forming a report outline, and then running concurrent IterativeResearcher instances for each section of the reportThe flow of the DeepResearcher is as follows:
IterativeResearcher, which:It is worth noting that the deep research agent does not ask clarifying questions at the start, so can be used in an automated fashion.
Deep Research Examples (using DeepResearcher):
Simple Research Examples (using IterativeResearcher):
flowchart LR
A["User Input
- query
- max_iterations
- max_time
- output_instructions"] --> B
subgraph "Deep Research Loop"
B["Knowledge
Gap Agent"] -->|"Current gaps
& objective"| C["Tool Selector
Agent"]
C -->|"Tool queries
(run in parallel)"| D["Tool Agents
- Web Search
- Crawler
- Custom tools"]
D -->|"New findings"| E["Observations
Agent"]
E --> |"Thoughts on findings
and research strategy"| B
end
E --> F["Writer Agent
(final output
with references)"]flowchart LR
A["User Input
- query
- max_iterations
- max_time"] --> B["Planner Agent"]
B -->|"Report plan
(sections & background context)"| D2
subgraph Parallel["Parallel Section Research"]
D1["IterativeResearcher
(Section 1)"]
D2["IterativeResearcher
(Section 2)"]
D3["IterativeResearcher
(Section 3)"]
end
D1 -->|"Section 1
Draft"| E["Proofreader
Agent"]
D2 -->|"Section 2
Draft"| E
D3 -->|"Section 3
Draft"| E
E --> F["Final
Research
Report"]Install using pip:
pip install deep-researcherOr clone the GitHub repo:
git clone https://github.com/qx-labs/agents-deep-research.git
cd agents-deep-research
pip install -r requirements.txtThen create a .env file with your API keys:
cp .env.example .envEdit the .env file to add your OpenAI, Serper and other settings as needed, e.g.:
OPENAI_API_KEY=
SEARCH_PROVIDER=serper # or set to openai
SERPER_API_KEY=# See the /examples folder for working examples
import asyncio
from deep_researcher import IterativeResearcher, DeepResearcher
# Run the IterativeResearcher for simple queries
researcher = IterativeResearcher(max_iterations=5, max_time_minutes=5)
query = "Provide a comprehensive overview of quantum computing"
report = asyncio.run(
researcher.run(query, output_length="5 pages")
)
# Run the DeepResearcher for more lengthy and structured reports
researcher = DeepResearcher(max_iterations=3, max_time_minutes=5)
report = asyncio.run(
researcher.run(query)
)
print(report)When running the deep researcher in Python, you have the option to set custom LLM configuration variables at runtime. This gives you flexibility to dynamically change the model choice within your code.
import asyncio
from deep_researcher import DeepResearcher, LLMConfig
# These configuration options will take precedence over the environment variables
llm_config = LLMConfig(
search_provider="serper",
reasoning_model_provider="openai",
reasoning_model="o3-mini",
main_model_provider="openai",
main_model="gpt-4o",
fast_model_provider="openai",
fast_model="gpt-4o-mini"
)
researcher = DeepResearcher(max_iterations=3, max_time_minutes=5, config=llm_config)
report = asyncio.run(
researcher.run(query)
)Run the research assistant from the command line.
If you've installed via pip:
deep-researcher --mode deep --query "Provide a comprehensive overview of quantum computing" --max-iterations 3 --max-time 10 --verboseOr if you've cloned the GitHub repo:
python -m deep_researcher.main --mode deep --query "Provide a comprehensive overview of quantum computing" --max-iterations 3 --max-time 10 --verboseParameters:
--query: The research topic or question (if not provided, you'll be prompted)--mode: If deep uses the DeepResearcher, if simple uses the IterativeResearcher (default: deep)--max-iterations: Maximum number of research iterations (default: 5)--max-time: Maximum time in minutes before the research loop auto-exits to produce a final output (default: 10)--output-length: Desired output length for the report (default: "5 pages")--output-instructions: Additional formatting instructions for the final reportBoolean Flags:
--verbose: Prints the research progress to console--tracing: Traces the workflow on the OpenAI platform (only works for OpenAI models)The deep researcher is designed to run any model compatible with the OpenAI API spec, and does so by adjusting the base_url parameter to the relevant model provider. Compatible providers include Azure OpenAI, OpenAI, Anthropic, Gemini, DeepSeek, Hugging Face and OpenRouter as well as locally hosted models via Ollama and LM Studio.
However, in order for the deep researcher to be run without errors it relies on models that are highly performant at tool calling.
gpt-4o-mini is as good if not better at tool selection than o3-mini (which is consistent with this leaderboard). Given the speed and cost benefits we therefore advise using gpt-4o-mini as the model for the majority of agents in our workflow, with o3-mini for planning tasks and gpt-4o for final writing.gemini-2.5-pro-preview-03-25) works well. Gemini 2.0 Flash (gemini-2.0-flash), despite being listed as compatible with tool calling, very frequently fails to call any tools.The Deep Research Assistant is built with the following components:
IterativeResearch instances, and final proofreading stepSERPER_API_KEY env variable.SEARCH_PROVIDER to openaiTool agents are agents specialized in carrying out specific tasks using one or more tools (e.g. web searches, fetching and interpreting data from an API, etc). To implement a custom tool agent:
deep_researcher/tools folderdeep_researcher/agents/tool_agents folderinit_tool_agents function in deep_researcher/agents/tool_agents/__init__.pydeep_researcher/agents/tool_selector_agent.py to include the name and description of the new agent, so that the ToolSelectorAgent knows of its existenceThis repository is in theory compatible with any LLMs that follow the OpenAI API specs. This includes the likes of DeepSeek as well as models served through OpenRouter. However, the models need to be compatible with Structured Outputs in the OpenAI API spec (i.e. being able to set response_format: {type: "json_schema", ...}).
LLMs are configured and managed in the deep_researcher/llm_config.py file.
The Deep Research assistant integrates with OpenAI's trace monitoring system. Each research session generates a trace ID that can be used to monitor the execution flow and agent interactions in real-time through the OpenAI platform.
DeepResearcher runs a lot of searches and API calls in parallel (at any given point in time it could be ingesting 50-60 different web pages). As a result you may find that yourself hitting rate limits for OpenAI, Gemini, Anthropic and other model providers particularly if you are on lower or free tiers. IterativeResearcher instead which is less consumptive of API calls.LLMs are not good at following guidelines on output length. You typically run into two issues:
No open issues yet, or sync has not completed.