“Complete the Deep Research Agent by building REST APIs, adding rate limiting, integrating WebSockets and Next.js, and deploying the application with Render and Vercel.”
In the previous chapters, we built the core multi-agent research workflow using LangGraph, implemented real-time progress updates with FastAPI WebSockets, and connected our research agents.
In this final chapter, we bring everything together and turn the project into a working full-stack AI application.
We will build the remaining REST API endpoints, execute research tasks asynchronously, protect expensive AI operations with rate limiting, connect the Next.js frontend, test the complete workflow, and deploy the application.
We start by creating a dedicated reports.py file inside the router directory.
Instead of putting every endpoint inside main.py, we use FastAPI's APIRouter to keep the backend modular and organized.
We implement four main operations:
POST /reports: Creates a new research task and starts the agent workflow.
GET /reports/{report_id}: Retrieves a specific research report.
GET /reports: Retrieves previously generated reports with pagination.
DELETE /reports/{report_id}: Deletes a stored research report.
We also implement helper functions to convert MongoDB ObjectIds into regular strings for JSON responses and validate report IDs before querying the database.
A deep research workflow involves multiple agents, external APIs, web searches, and LLM calls. Executing the entire workflow inside a normal HTTP request could cause long response times or gateway timeouts.
To solve this, we use FastAPI's BackgroundTasks.
When a user submits a research query, the API immediately creates a report record in MongoDB and returns a report ID with an HTTP 202 Accepted response.
The background worker then executes the LangGraph workflow independently.
Once the research is complete, it extracts the summary, report sections, citations, sources, and word count, and saves the final result in MongoDB.
If an exception occurs, the worker records the failure status and notifies the frontend through WebSockets.
Every research request can trigger multiple LLM calls, Tavily searches, and ArXiv queries. Uncontrolled requests can quickly increase infrastructure and API costs.
We integrate SlowAPI to introduce IP-based rate limiting.
This helps protect the application from excessive requests caused by bots, misuse, or accidental frontend request loops.
We configure request limits on the API endpoints and handle rate-limit exceptions centrally in FastAPI.
Think of rate limiting as a financial safeguard for an AI application: it helps control how frequently users can trigger expensive agent workflows.
After implementing the endpoints, we register the reports router and WebSocket router inside main.py under the /api/v1 prefix.
We also configure:
SlowAPI limiter initialization and exception handling.
CORS middleware to allow requests from the Next.js frontend.
MongoDB connection and application startup configuration.
The REST API handles report creation and retrieval, while WebSockets deliver live execution updates.
This separation allows the frontend to receive an immediate response while the research workflow continues in the background.
Before integrating the frontend, we test the backend independently using Postman.
The workflow is straightforward:
Send a POST request with the research query and maximum number of sources.
Receive the generated report ID.
Connect to the WebSocket endpoint using that report ID.
Observe live execution updates.
Retrieve the completed report using the GET endpoint.
During execution, we can observe updates such as archive search, web search, researching, fact-checking, synthesizing, and completion.
The final API response contains the research query, status, executive summary, report sections, and citations with their source details.
With the backend working, we connect it to the Next.js frontend.
The interface allows users to submit research queries, select the maximum number of sources, and start the research process.
WebSocket messages power the live execution logs, while the REST API retrieves the completed report.
The final interface displays the generated report, its sections, sources, citations, and other research information.
The final step is deploying both parts of the application.
We use:
Backend: Render
Frontend: Vercel
Database: MongoDB Atlas
We connect the backend and frontend GitHub repositories to their respective deployment platforms and configure the required environment variables.
After deploying the backend, we update the frontend's public API URL. We then update the backend's CORS configuration to allow requests from the deployed Vercel domain.
Finally, we verify the deployed API, retrieve and delete reports, and run a complete research query through the live application.
The following diagram illustrates how the frontend, API layer, background research workflow, database, and WebSocket connection work together.
Generating Architecture Diagram…
With this chapter, we have completed the initial version of our Full-Stack AI Deep Research Agent.
We started with a multi-agent architecture and progressively built the research workflow, connected external search tools, implemented real-time updates, developed REST APIs, integrated a frontend, and deployed the application.
More importantly, this project demonstrates how different AI engineering components work together in a practical application—not just how to build an individual AI agent.
This is the foundation. In future enhancements, we can explore better retrieval mechanisms, persistent memory, evaluation, observability, and more advanced research capabilities.
The initial Deep Research Agent project is now complete!
Thanks for following the entire series. Keep building and keep experimenting.
— SN Dev
You have completed all available parts of "Deep Research Agent". Stay tuned for more updates!
Ask questions, discuss architecture, and share insights with other developers.
Sign in to join the discussion and share your thoughts with other developers.
Sign In to Comment