Back to Insights
PersonalPythonGroqTavilyNext.jsWebSocketAI AgentLLMFastAPITypeScript

How to Build an Autonomous AI Research Agent with Groq, Tavily & WebSocket Streaming

2026-07-189 min read

The Project

AutoAnalyst Engine is an autonomous AI research agent that takes a topic, searches the web, synthesizes findings, and generates professional Markdown reports — all streamed live to a dashboard via WebSockets.

Instead of manually Googling, reading 20 tabs, and writing up findings, you give AutoAnalyst a topic and watch it work in real-time. It thinks, searches, observes, and writes — showing every step as it happens.

System Architecture

┌─────────────────────────────────────────────────┐
│                  Frontend (Next.js)              │
│  ┌──────────┐  ┌──────────────┐  ┌───────────┐ │
│  │ Artifacts │  │ Thought      │  │ Report    │ │
│  │ Sidebar   │  │ Stream (logs)│  │ Viewer    │ │
│  └──────────┘  └──────────────┘  └───────────┘ │
│                    │ WebSocket                    │
└────────────────────┼─────────────────────────────┘
                     │ ws://localhost:8000/ws/research
┌────────────────────┼─────────────────────────────┐
│            Backend (FastAPI)                      │
│  ┌──────────────┐  ┌────────────┐  ┌──────────┐ │
│  │ main.py      │→ │engine.py   │→ │tools.py  │ │
│  │ (WebSocket)  │  │(Research   │  │(Tavily   │ │
│  │ + REST API)  │  │Engine)     │  │search +  │ │
│  │              │  │            │  │save file)│ │
│  └──────────────┘  └────────────┘  └──────────┘ │
│                           │                      │
│                    ┌──────┴──────┐               │
│                    │  Groq API   │               │
│                    │  (LLaMA 3.1)│               │
│                    └─────────────┘               │
└──────────────────────────────────────────────────┘

How It Works

The Research Loop

AutoAnalyst uses an iterative Plan-Act-Observe loop:

  1. Plan — The LLM receives a topic and creates a research plan with search queries
  2. Act — The agent calls Tavily Search API for each query, fetching real web results
  3. Observe — Results are fed back to the LLM, which extracts key findings
  4. Repeat — Based on findings, the agent generates deeper queries (3-5 per cycle)
  5. Synthesize — After enough iterations, the agent writes a structured report
# Simplified research loop
class ResearchEngine:
    async def run(self, topic: str, websocket: WebSocket):
        await self.stream_log(websocket, f"Starting research on: {topic}")
        
        queries = self.generate_initial_queries(topic)
        findings = []
        
        for query in queries:
            await self.stream_log(websocket, f"Searching: {query}")
            results = await self.tavily_search(query)
            await self.stream_log(websocket, f"Found {len(results)} results")
            
            analysis = await self.analyze_with_llm(results, topic)
            findings.extend(analysis)
            
            # Generate follow-up queries based on findings
            if len(findings) < 10:
                follow_ups = await self.generate_followup_queries(findings)
                queries.extend(follow_ups)
        
        report = await self.generate_report(topic, findings)
        await self.save_report(topic, report)
        await self.stream_artifact(websocket, topic, report)

Real-Time Thought Stream

Every step is pushed to the frontend via WebSocket. Users see the agent's reasoning in real-time:

Starting research on: "Impact of AI on Healthcare"
→ Searching: "AI diagnostics accuracy 2024 studies"
→ Found 8 results
→ Analyzing: Johns Hopkins study shows 95% accuracy in radiology
→ Searching: "AI drug discovery breakthrough 2024"
→ Found 12 results
→ Analyzing: DeepMind's AlphaFold predicted 200M+ protein structures
→ Generating report...

Professional Reports

The final output is a structured Markdown report with:

  • Executive Summary — One-paragraph overview
  • Key Findings — Bullet points with citations
  • Detailed Analysis — Section-by-section breakdown
  • Sources — Links to all referenced articles
  • Methodology — How the research was conducted

Tech Stack

LayerTechnology
FrontendNext.js 16, React 19, TypeScript, Tailwind CSS v4
BackendPython 3.14, FastAPI, Uvicorn
AI ModelLLaMA 3.1 8B (via Groq API)
Web SearchTavily Search API
CommunicationWebSocket (real-time), REST API

Features

  • Autonomous Research — Give it a topic, it searches the web and writes a full report
  • Live Thought Stream — Watch the agent's reasoning and search steps in real-time
  • Professional Reports — Executive Summary, Findings, Analysis, and Sources
  • Artifact Management — Download or delete past reports from the sidebar
  • Multi-search Strategy — The agent performs 3-5 searches from different angles

Setup

# Backend
cd backend
python -m venv .venv
.venv\Scripts\activate    # Windows
pip install -r requirements.txt

# Environment variables (.env)
groq_api="gsk_your_groq_api_key_here"
tavily_api="tvly-your_tavily_api_key_here"

# Start backend
uv run uvicorn main:app --reload

# Frontend (separate terminal)
cd frontend
npm install
npm run dev

Open http://localhost:3000 and start researching.

API Endpoints

MethodPathDescription
WebSocket/ws/researchReal-time research session
GET/api/reports/{filename}Download a saved report
DELETE/api/artifacts/{filename}Delete a saved report

What I Learned

  1. WebSocket streaming transforms UX — Watching the agent think in real-time is far more engaging than waiting for a spinner. Users trust the output more when they see how it was derived.

  2. LLM function calling is perfect for agent loops — Groq's structured output made the Plan-Act-Observe loop clean and predictable. The LLM outputs tool calls, the backend executes them, and feeds results back.

  3. Multi-search strategies beat single queries — One search query often misses context. Having the agent generate 3-5 different angles for each topic produced consistently better reports.

  4. Tavily is purpose-built for LLM agents — Unlike general search APIs, Tavily returns clean, structured results that are easy to feed into an LLM without heavy parsing.

Frequently Asked Questions

Quick answers to common questions about this project.

The agent uses a ReAct (Reasoning + Acting) loop: it plans the next research step, calls Tavily search or a reasoning tool, observes the result, and iterates. Groq's LLaMA 3.1 powers the planning and summarization at each step.

Tavily is a search API optimized for AI agents. Unlike Google Search, it returns clean, structured results with summaries instead of raw HTML, making it easy for LLMs to parse and reason about search results without extra processing.