Building A Dynamic Multi-Agent Workflow - Harnessing AI Collaboration With LangChain & LangGraph - by Rohit Kumar - Oct, 2024 - Medium
Building A Dynamic Multi-Agent Workflow - Harnessing AI Collaboration With LangChain & LangGraph - by Rohit Kumar - Oct, 2024 - Medium
Open in app
44
Search
Get unlimited access to the best of Medium for less than $1/week. Become a member
https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 1/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…
Fig-1
Prerequisites
Before diving into the code, ensure you have the following installed:
Python 3.7+
LangChain and LangGraph libraries installed. You can install them via pip:
import os
os.environ["OPENAI_API_KEY"] = "your_openai_api_key"
Creating an AI Agent
https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 2/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…
The system message explains the role of the agent and how it should behave. For
example, one agent is instructed to generate random numbers, and another is asked
to multiply them.
class AgentState(TypedDict):
messages: Sequence[BaseMessage]
sender: str
Each agent sends and receives messages, and the state keeps track of the current
agent that is responsible for the next action.
The workflow is implemented using LangGraph’s StateGraph . Here, the agents are
added as nodes in the workflow, and the transitions between them are defined
based on the router logic.
The router function helps in controlling the flow of messages between agents:
def router(state):
messages = state["messages"]
last_message = messages[-1]
if "FINAL ANSWER" in last_message.content:
if state["sender"] == "Agent_1":
return "Agent_2"
return END
return "continue"
The workflow defines how the agents interact and the conditions under which the
control moves from one agent to another.
workflow.add_node("Agent_1", agent_1_node)
workflow.add_node("Agent_2", agent_2_node)
Conditional edges are added to move the process from one agent to another based
on the router logic.
Main Execution
The main part of the script is responsible for initializing the workflow and executing
it based on a user’s initial input. The input message instructs the system to generate
random numbers and multiply them by 10:
if __name__ == "__main__":
initial_state = {
"messages": [
https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 4/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…
Here, the workflow is executed with an initial message, and the system streams the
events through each agent. The recursion limit ensures that the workflow does not
run indefinitely.
Output
Fig-2
Fig-3
Conclusion
This Python script demonstrates how to build a simple, multi-agent workflow using
LangChain and LangGraph. The process involves defining agents, setting up their
states, and routing the messages between them to achieve a specific task. This
https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 5/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…
Follow
Rohit Kumar
https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 6/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…
Sometimes we need to run two Python scripts simultaneously to optimize Hybrid CPU-GPU
Applications, Parallelism in Data Preparation…
Rohit Kumar
Sep 17 5
https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 7/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…
Rohit Kumar
May 18 5
Rohit Kumar
Jun 23
https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 8/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…
Pankaj
6d ago 58
Mohammed Lubbad
https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 9/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…
Artificial intelligence (AI) is evolving rapidly, reshaping how we approach problem-solving and
system design. But what if we could empower…
Oct 11 67
Lists
Staff Picks
756 stories · 1421 saves
Self-Improvement 101
20 stories · 2969 saves
Productivity 101
20 stories · 2516 saves
https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 10/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…
Sep 17 55 2
https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 11/13
2024/11/6 晚上9:49 Building a Dynamic Multi-Agent Workflow: Harnessing AI Collaboration with LangChain & LangGraph | by Rohit Kumar | Oct,…
4d ago 4
Oct 27
Bella Belgarokova
In this tutorial, we will build a sophisticated tool for generating prompt templates tailored for AI
language models. This project is…
Jul 24
https://medium.com/@rohitobrai11/building-a-dynamic-multi-agent-workflow-harnessing-ai-collaboration-with-langchain-langgraph-01b8a24e9e3d 13/13