βImplement a real-time WebSocket streaming engine with FastAPI, managing report-specific connections and broadcasting live LangGraph agent execution updates to the frontendβ
In this part of our Autonomous Deep Research Agent series, we implement a real-time WebSocket streaming engine using FastAPI.
Our LangGraph-based multi-agent workflow is already capable of executing the Planner, Researcher, Critic, and Writer agents. However, research tasks can take 30β60 seconds or even longer to complete. Without real-time updates, users would have no indication of what is happening in the background.
To solve this, we introduce WebSockets to stream agent execution statuses directly from the backend to the frontend.
One approach to tracking background execution is HTTP polling, where the frontend repeatedly sends requests to the backend asking whether the task has completed.
For example, the frontend might send a request every few seconds:
Is the research complete?
Is the researcher still running?
Has the report been generated?
This creates unnecessary requests and increases server overhead.
Instead, we implement WebSockets, which establish a persistent, two-way communication channel between the frontend and backend.
Whenever an agent completes a step, the backend can immediately push an update to the frontend without waiting for another client request.
For our application, the initial updates will include statuses such as:
Planning
Researching
Reviewing
Writing
This gives users visibility into the agent workflow while the research is running.
We create a new router file:
router/websocket.py
The core component is a ConnectionManager class responsible for managing active WebSocket connections.
A critical requirement is supporting multiple users researching different topics simultaneously.
For example, if 100 users are running 100 different research tasks, each user should receive updates only for their own report.
To achieve this, we maintain a dictionary that maps each report ID to its active WebSocket connections.
The Connection Manager provides three important methods:
Connect: Accepts an incoming WebSocket connection and registers it against the corresponding report ID.
Disconnect: Removes a disconnected WebSocket from the active connection list. If no connections remain for a report, its entry is removed from the dictionary. This helps prevent stale connections from accumulating.
Broadcast: Sends JSON messages to all active connections associated with a particular report ID. If sending a message fails, the connection is handled through the disconnect mechanism.
This report-specific mapping ensures that updates from one research task are not accidentally delivered to another user's session.
In the previous part, we implemented an update callback inside our LangGraph execution workflow.
That callback receives status changes from the agents.
Now, we connect this mechanism to our WebSocket Connection Manager.
The flow is straightforward:
The LangGraph agent updates its execution status.
The update callback receives the new status.
The callback invokes the broadcast method.
The Connection Manager identifies active connections for that report ID.
The status is sent to the connected frontend as a JSON message.
This connects our existing multi-agent execution workflow with the real-time communication layer.
Next, we expose a WebSocket endpoint using FastAPI:
/reports/{report_id}/stream
The endpoint accepts two important parameters:
websocket: The incoming WebSocket connection.
report_id: The identifier used to associate the connection with a specific research report.
When a client connects, the endpoint registers the connection through the Connection Manager.
We then keep the connection alive using an asynchronous loop. In our current implementation, the client does not need to send application data; we use receive_text() to keep the connection open and handle incoming messages if needed.
When a WebSocket disconnection occurs, the endpoint catches WebSocketDisconnect and removes the connection from the manager.
With this, our real-time WebSocket streaming engine is ready.
Our communication flow now looks like this:
Generating Architecture Diagramβ¦
In this part, we completed the WebSocket communication layer that will allow our frontend to receive live updates from the research workflow.
At this stage, we are streaming execution statuses rather than the internal reasoning of the agents. We will progressively enhance this experience in upcoming parts.
In Part 13, we will move forward with building the remaining API endpoints, connecting the routers to our main FastAPI application, and testing the APIs using Postman.
We are gradually transforming our LangGraph workflow into a complete, interactive Deep Research Agent application.
Keep Building. Keep Learning. π
#AIEngineering #AgenticAI #LangGraph #FastAPI #WebSockets #Python #DeepResearchAgent #SNDev
Continue following "Deep Research Agent" with Building and Deploying a Full-Stack AI Deep Research Agent | Part 13 β Final Chapter.
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