βConnect Planner, Researcher, Critic, and Writer agents using LangGraph StateGraph, implement graph execution with astream(), and prepare real-time agent status updates for the frontend.β
In this chapter of our Autonomous Deep Research Agent project, we move from building individual AI agents to connecting them into a complete multi-agent workflow using LangGraph.
Until now, we have developed four specialized agents: Planner, Researcher, Critic, and Writer. However, these agents are still independent components. We need an orchestration layer that defines their execution order, manages shared state, and coordinates the entire research process.
This is where LangGraph comes in.
We create a graph.py file to define the workflow using LangGraph's StateGraph.
Our graph contains four nodes:
Planner: Breaks the research query into actionable subqueries.
Researcher: Searches for relevant information and gathers sources.
Critic: Evaluates research findings, identifies contradictions, and cross-references claims.
Writer: Generates the final structured research report.
Each node operates on a shared AgentState, allowing information to flow between agents throughout execution.
The execution follows a sequential pipeline:
User submits a research query.
Planner generates the research plan.
Researcher gathers relevant information.
Critic evaluates the collected evidence.
Writer generates the final research report.
We initialize StateGraph with our AgentState schema and register all four agent nodes.
Next, we define the entry point and execution edges:
Planner β Researcher
Researcher β Critic
Critic β Writer
Writer β END
Finally, we compile the graph and store the compiled instance as research_graph, which can be reused whenever a research request arrives.
Before executing the graph, we prepare an initial state dictionary.
It contains the user's query, maximum source limit, report ID, callback function, and empty fields for intermediate research data.
These fields include:
Subqueries
Sources
Citations
Conflict flags
Research sections
Summary
Execution status
Error information
This shared state acts as a common data container that travels through every agent node.
Generating Architecture Diagramβ¦
Instead of waiting for the entire workflow to finish before updating the frontend, we use LangGraph's asynchronous astream() method.
This allows us to capture intermediate execution updates as individual nodes complete their work.
Our implementation uses an asynchronous iteration over the graph output to:
Identify which node has completed execution.
Extract the updated agent state.
Capture the current execution status.
Pass status updates through a callback function.
Prepare the updates for frontend delivery through WebSockets.
This approach makes the research workflow more transparent because the user can eventually see which agent is currently working.
We also implement a try-except block around graph execution.
If an API call or agent execution fails, we:
Update the report status to FAILED.
Capture and log the exception details.
Notify the frontend through the update callback.
Return the updated initial state.
This ensures that failures are communicated instead of leaving the frontend waiting indefinitely.
By the end of Part 11, all four individual agents are connected through a compiled LangGraph StateGraph.
We have also implemented the graph execution function, initialized shared state, captured intermediate node updates using astream(), and added basic error handling.
What's Next?
In the next stage, we will connect this graph execution pipeline to our main FastAPI application and expose it through API endpoints, bringing the backend orchestration closer to the frontend application.
The goal is to turn our individual AI agents into a fully coordinated Autonomous Deep Research Agent.
Continue following "Deep Research Agent" with Building a Real-Time WebSocket Streaming Engine with FastAPI | Deep Research Agent β Part 12.
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