Web Research Agent
March 8, 2026 ยท View on GitHub
A production-ready autonomous web research agent built with LangChain, LangGraph, and OpenAI. This agent accepts natural language research queries, intelligently breaks them into sub-questions, searches the web for current information, and synthesizes findings into professional, structured reports.
Features
- ๐ค Autonomous Research - Automatically plans, searches, and synthesizes information
- ๐ Multi-Provider Search - Supports Tavily, SerpAPI, and DuckDuckGo
- ๐ Structured Reports - Clean, professional output with sources and citations
- ๐ REST API - FastAPI backend with async support
- ๐ฅ๏ธ React Frontend - Modern chat interface with markdown rendering
- ๐ณ Docker Ready - Multi-stage containerized deployment
- โ๏ธ Cloud Deploy - Railway, Render, and Fly.io configs included
- ๐ Real-time Date Aware - Always searches with current date context
- โ๏ธ Type-Safe Configuration - Pydantic-based settings management
- ๐ก๏ธ Production-Ready - Proper error handling, logging, and security best practices
Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ User Query โ
โ (CLI / REST API / Chat Frontend) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ LangGraph ReAct Agent โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Planning Phase โ โ
โ โ โข Analyze research goal โ โ
โ โ โข Decompose into sub-questions โ โ
โ โ โข Prioritize information needs โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Research Phase โ โ
โ โ โข Tavily Search (AI-optimized, primary) โ โ
โ โ โข SerpAPI (Google results) โ โ
โ โ โข DuckDuckGo (free fallback) โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Synthesis Phase โ โ
โ โ โข Cross-reference sources โ โ
โ โ โข Resolve conflicts โ โ
โ โ โข Generate structured report โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Structured Report โ
โ โข Objective โข Pros & Cons โ
โ โข Key Findings โข Final Recommendation โ
โ โข Analysis โข Sources โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Project Structure
web-research-agent/
โโโ src/
โ โโโ __init__.py # Package initialization
โ โโโ config.py # Pydantic settings management
โ โโโ exceptions.py # Custom exception classes
โ โโโ logging_config.py # Structured logging setup
โ โโโ prompts.py # System prompts with real-time date
โ โโโ agent.py # ResearchAgent class (LangGraph)
โ โโโ agui_agent.py # AG-UI Protocol streaming agent
โ โโโ tools/
โ โโโ __init__.py # Tool exports
โ โโโ registry.py # Tool registry and discovery
โ โโโ tavily_search.py # Tavily search (primary)
โ โโโ serpapi_search.py # SerpAPI Google search
โ โโโ duckduckgo_search.py # DuckDuckGo fallback
โโโ frontend/ # Next.js React UI
โ โโโ app/
โ โ โโโ page.tsx # Chat interface component
โ โ โโโ layout.tsx # Root layout
โ โ โโโ globals.css # Tailwind styles
โ โ โโโ api/research/ # Research API proxy route
โ โโโ package.json
โ โโโ tailwind.config.ts
โโโ main.py # CLI entry point
โโโ api.py # FastAPI REST API
โโโ api_agui.py # AG-UI Protocol API server
โโโ Dockerfile # Multi-stage container build
โโโ docker-compose.yml # Local Docker setup
โโโ render.yaml # Render deployment
โโโ railway.json # Railway deployment
โโโ fly.toml # Fly.io deployment
โโโ Procfile # Process definition
โโโ DEPLOYMENT.md # Deployment guide
โโโ pyproject.toml # Python packaging & tooling config
โโโ requirements.txt # Python dependencies
โโโ requirements-api.txt # API-specific dependencies
Quick Start
Prerequisites
- Python 3.10+
- Node.js 18+ (for frontend)
- OpenAI API key
Option 1: CLI Usage
# Clone the repository
git clone https://github.com/senisinsane/Research-Agent.git
cd Research-Agent
# Create virtual environment
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Configure environment
cp .env.example .env
# Edit .env and add your API keys
# Run research
python main.py "What are the latest trends in AI?"
Option 2: REST API
# Start the API server
python api.py
# Or with AG-UI Protocol support
python api_agui.py
# Make a request
curl -X POST http://localhost:8000/research \
-H "Content-Type: application/json" \
-d '{"query": "What is quantum computing?"}'
Option 3: Full Stack (Frontend + Backend)
# Terminal 1: Start the backend
python api_agui.py
# Terminal 2: Start the frontend
cd frontend
npm install
npm run dev
# Open http://localhost:3000 in your browser
Option 4: Docker
# Build and run with Docker Compose
docker-compose up --build
# API available at http://localhost:8000
Configuration
Environment Variables
| Variable | Required | Default | Description |
|---|---|---|---|
OPENAI_API_KEY | โ Yes | - | OpenAI API key |
OPENAI_MODEL | No | gpt-4o-mini | Model to use |
OPENAI_TEMPERATURE | No | 0.0 | Response temperature |
TAVILY_API_KEY | โญ Recommended | - | Tavily API key (free tier) |
SERPAPI_API_KEY | No | - | SerpAPI key for Google search |
MAX_ITERATIONS | No | 25 | Max agent iterations |
SEARCH_MAX_RESULTS | No | 5 | Results per search query |
LOG_LEVEL | No | WARNING | Logging level |
Search Providers
| Provider | API Key | Best For |
|---|---|---|
| Tavily | Required | AI-optimized results (recommended) |
| SerpAPI | Required | Google search results |
| DuckDuckGo | None | Free fallback (always available) |
The agent automatically uses available providers. Configure Tavily for best results.
API Reference
REST Endpoints (api_agui.py)
| Endpoint | Method | Description |
|---|---|---|
/ | GET | API info and available endpoints |
/health | GET | Health check with configuration status |
/research | POST | Execute research query (JSON response) |
/research/stream | POST | Streaming research with SSE events |
/agui | POST | AG-UI Protocol streaming endpoint |
/langgraph | POST | LangGraph agent endpoint |
/docs | GET | Interactive Swagger documentation |
POST /research
Request:
{
"query": "Your research question"
}
Response:
{
"success": true,
"query": "Your research question",
"report": "**Objective**: This report investigates...\n\n**Key Findings**:..."
}
CLI Usage
Basic Usage
python main.py "your research query"
With Options
# Use a specific model
python main.py "Compare React vs Vue.js" --model gpt-4o
# Enable verbose logging
python main.py "Latest AI developments" --verbose
# Save to file
python main.py "Elon Musk net worth" --output report.txt
# Increase iteration limit
python main.py "Complex research topic" --max-iterations 40
CLI Arguments
| Argument | Short | Default | Description |
|---|---|---|---|
query | - | Required | Research query |
--model | - | From config | OpenAI model |
--max-iterations | - | 25 | Max iterations |
--verbose | -v | False | Debug logging |
--output | -o | stdout | Output file |
Output Format
Reports follow this structure:
- Objective - What was researched
- Key Findings - 3-5 critical discoveries
- Detailed Analysis - In-depth exploration with context
- Pros & Cons - Balanced assessment
- Final Recommendation - Evidence-based conclusion
- Sources - Cited references with URLs
Programmatic Usage
from src.agent import create_agent
# Create agent
agent = create_agent(model="gpt-4o-mini")
# Run research
result = agent.research("What is quantum computing?")
# Access result
print(result.content)
print(f"Success: {result.success}")
print(f"Query: {result.query}")
Async Usage
import asyncio
from src.agent import create_agent
async def main():
agent = create_agent()
result = await agent.research_async("Latest AI news")
print(result.content)
asyncio.run(main())
Error Handling
from src.exceptions import (
ConfigurationError,
AgentExecutionError,
EmptyResponseError,
SearchProviderUnavailableError,
)
try:
result = agent.research(query)
except ConfigurationError as e:
print(f"Config issue: {e}")
except AgentExecutionError as e:
print(f"Agent error: {e}")
except EmptyResponseError as e:
print(f"No response generated: {e}")
Deployment
Quick Deploy Options
| Platform | Configuration |
|---|---|
| Railway | railway.json |
| Render | render.yaml |
| Fly.io | fly.toml |
| Docker | Dockerfile + docker-compose.yml |
See DEPLOYMENT.md for detailed deployment instructions.
Docker Deployment
# Build image
docker build -t research-agent .
# Run container
docker run -p 8000:8000 \
-e OPENAI_API_KEY=your_key \
-e TAVILY_API_KEY=your_key \
research-agent
Development
Install Dev Dependencies
pip install -e ".[dev]"
Code Quality
# Lint code
ruff check .
# Format code
black src/ main.py api.py api_agui.py
# Type check
mypy src/
Running Tests
# Run all tests
pytest
# With coverage
pytest --cov=src --cov-report=html
Adding a New Search Provider
- Create
src/tools/your_provider.py:
from langchain_core.tools import tool
from src.config import get_settings
from src.logging_config import get_logger
logger = get_logger(__name__)
@tool
def your_search(query: str, max_results: int = 5) -> str:
"""Search description for the LLM."""
settings = get_settings()
# Your implementation
return formatted_results
- Register in
src/tools/registry.py:
from src.tools.your_provider import your_search
_TOOL_REGISTRY["your_search"] = your_search
Tech Stack
- Backend: Python 3.10+, FastAPI, LangChain, LangGraph
- Frontend: Next.js 14, React 18, Tailwind CSS
- AI: OpenAI GPT-4o-mini (default), GPT-4o
- Search: Tavily, SerpAPI, DuckDuckGo
- Deployment: Docker, Railway, Render, Fly.io
License
MIT License - See LICENSE for details.
Repository: github.com/senisinsane/Research-Agent