Multi-Agent Workflow with LangGraph

Introduction The emergence of large language models (LLMs) has reshaped how AI systems interact with and interpret the world. Traditionally, single-agent architectures have been used to handle inputs, make decisions, and produce outputs. However, as AI systems scale to manage more complex, multi-step tasks, researchers and developers are increasingly turning to multi-agent systems and advanced graph-based architectures. Supported by frameworks like LangChain and LangGraph, these innovations enable more flexible, scalable, and collaborative AI systems to perform complex tasks.

Agents and Multi-Agent Systems An agent in AI is an autonomous entity capable of perceiving its environment, making decisions, and taking actions to achieve goals.

Multi-agent systems consist of multiple autonomous agents interacting in a networked environment to achieve collaborative goals. Compared to single-agent architectures, these systems offer several advantages:

Collaboration: Multiple agents can pool their strengths to tackle complex tasks, resulting in more comprehensive outcomes. Scalability: As workloads grow, adding new agents can more effectively distribute tasks than scaling a single agent’s capabilities. Specialization: Different agents can specialize in specific tasks, optimizing performance. For example, one agent may handle language parsing while another performs calculations or data retrieval. These characteristics make multi-agent systems a natural choice for implementing complex, scalable, and robust AI solutions.

LangChain & LangGraph

LangChain: Building and managing complex LLM chains LangChain is a pioneering framework designed to facilitate the development of LLM applications, supporting the entire lifecycle from creation to deployment and monitoring. One notable advantage of LangChain is its support for linking multiple LLMs together to handle complex queries. This linking concept allows the output of one model to serve as the input for another, enabling a tiered, modular decision-making process among LLMs.

LangChain uses directed acyclic graphs (DAGs) to construct these chains. In a DAG architecture, LLMs operate within a directed graph, where each node represents a specific processing or decision-making process, and there are no cycles that could lead to infinite loops. This architecture is particularly suited for handling increasingly complex and multi-step tasks, as it provides a clear linear path from input to output.

LangGraph: State-aware agents enhancing adaptability Based on the foundational capabilities of LangChain, LangGraph introduces stateful and adaptive agents within a graph-based architecture. This innovative design enables agents to learn from iterative feedback, adapt to dynamic environments, and tackle increasingly complex challenges. By leveraging directed cycles in the graph, LangGraph allows agents to progressively improve their decision-making processes over time, effectively simulating how humans learn through experience.

Adaptive Learning: LangGraph incorporates feedback loops within its architecture, allowing agents to review and reassess their previous actions. By analyzing past outcomes, these agents refine their strategies, reduce repeated mistakes, and dynamically adjust based on new data or scenarios. This feedback mechanism enhances the long-term resilience and effectiveness of the system. Handling complex tasks: LangGraph adopts a “divide and conquer” approach, breaking down large, multifaceted problems into smaller, manageable components. Specialized agents handle each component, working in parallel to optimize processing time and isolate errors to prevent cascading failures. This modular approach enables the system to efficiently tackle complex, multi-layered objectives. State Management: A robust state management system underpins LangGraph, retaining memory that evolves with dynamic updates and allowing agents to access shared context. This ensures consistency across tasks, optimizes resource utilization by reducing redundancy, and enhances decision-making through context-aware behavior. The system’s statefulness supports long-term learning, with accumulated experience improving performance over time. LangGraph integrates adaptive learning, task decomposition, and comprehensive state management into a groundbreaking solution for building intelligent, scalable, and efficient systems to address real-world challenges.

LangGraph Architecture Components The LangGraph framework is built around three key components that enable the creation of adaptive, stateful, and modular workflows for complex tasks. Each component plays a unique role within the overall architecture:

Nodes: Task-Specialized Agents Nodes are the fundamental building blocks of LangGraph. Each node represents an autonomous agent designed to perform a specific function, such as processing data, interacting with APIs, or making decisions. These agents operate independently, meaning each node is dedicated to its task and can be reused in different configurations. For example, one node may be responsible for cleaning and standardizing input data, while another generates visual outputs like charts or graphs. This modularity ensures flexibility and scalability in complex workflows.

Edges: Defining Dynamic Interactions Edges form the connections between nodes, determining how data and tasks flow within the graph. These connections can be static or conditional, allowing the system to dynamically adjust based on its state. For instance, a conditional edge may direct the process to a tool node if a specific tool is invoked or terminate the workflow if a final answer is provided. This adaptability enables LangGraph to respond to contextual changes, optimize resource usage, and ensure logical progress of tasks.

State: Shared Memory Framework State serves as a shared data structure that captures the progress of the entire workflow. It acts as a “global memory,” retaining the results computed by each node and providing continuity within the graph. This structure is crucial for collaboration between nodes, as it allows agents to access and build upon previous results. State employs patterns to determine what information should be retained and uses update mechanisms to manage updates after each computation. This design ensures that the system remains consistent even in scenarios involving multi-agent interactions and iterative processes.

By combining these components, LangGraph creates a robust, modular, and adaptive high-level workflow coordination framework. It supports dynamic routing, stateful memory, and task-specific specialization.

Use Cases LangGraph is a powerful framework that enables complex automation and AI workflows by creating interconnected agent (or node) graphs. Here are some use cases where LangGraph can be effectively applied:

Customer Support Automation: LangGraph can be used to build advanced customer support systems that route requests between specialized agents. These agents can handle tasks such as querying flight information, updating bookings, and processing refunds. For example, one agent could interact with a flight database to find available seats based on customer queries, while another agent handles ticket updates or cancellations. Knowledge Retrieval and Q&A: LangGraph can automate information retrieval and evaluation by creating agents that process, rank, and synthesize documents. For instance, a retrieval agent searches for relevant documents, while a ranking agent assesses whether the retrieved information effectively answers the user’s question. This can be applied in scenarios such as research assistants or content-based decision-making systems. Building Autonomous Agents with Memory: LangGraph allows for the creation of self-improving agents that retain memory during interactions. In autonomous systems, agents can use memory modules to store previous experiences, allowing them to synthesize information and adjust their behavior over time. This use case is valuable in personal assistant applications where agents can learn and adapt to user preferences. Business Process Automation: LangGraph can automate multi-step business processes by connecting various agents focused on different tasks. These tasks can range from processing user requests to interacting with external APIs and updating central databases. A practical example is using LangGraph to automate internal workflows within enterprises, such as managing customer orders, inventory systems, or resource allocation. Some of these use cases are enabled by the support of multi-agent systems. Let’s briefly examine three of the most important types.

Multi-Agent System Architectures Implementing multi-agent systems (MAS) can take various forms, depending on the complexity and requirements of the tasks:

Collaborative Architecture: In this architecture, two or more agents collaborate to achieve a single goal. They can access shared tools, such as APIs or databases, to retrieve relevant information and generate coordinated responses. This is suitable for tasks that are too complex for a single agent but do not require extensive coordination. Supervisory Architecture: In a more structured arrangement, supervisory agents are responsible for overseeing and coordinating the behavior of subordinate agents, ensuring that each component’s tasks are executed correctly. Supervisory agents evaluate outputs and may adjust workflows based on the progress of subordinate agents. This approach is valuable in scenarios requiring high reliability and complex decision-making. Hierarchical Architecture: This advanced architecture introduces multiple layers of agents, each focusing on specific domains. At the top of the hierarchy, supervisory teams coordinate groups of agents. This approach is scalable, flexible, and reflects the trend of AI moving towards more autonomous decision-making systems.

Multi-Agent Workflow with LangGraph

LangGraph Studio A common challenge in agent frameworks is understanding what occurs during agent iterations. This is where LangGraph Studio comes into play. It provides a dedicated agent integrated development environment (IDE) that allows developers to visualize, interact with, and debug complex agent workflows.

With its visual graphs and state editing features, LangGraph Studio helps users better understand agent processes and accelerates iteration. It also integrates with LangSmith, enabling team members to collaboratively debug fault patterns.

Here is an example of a practical application of LangGraph Studio.

Multi-Agent Workflow with LangGraph

Hands-on In this hands-on tutorial, we will create a multi-agent system capable of scraping data online and generating charts based on that data. We will define agents, tools, and the logic to integrate everything into a functional workflow step by step. Let’s get started!

Step 1: Import Required Libraries First, we import the modules required to build agents and manage workflows.

from langchain_core.messages import (    BaseMessage,    HumanMessage,    ToolMessage,)from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholderfrom langgraph.graph import END, StateGraph

Step 2: Create AgentsAgents are assistants capable of performing tasks using tools. Here is a reusable function to create agents:

def create_agent(llm, tools, system_message: str):    """Create an agent."""    tool_names = ", ".join(tool.name for tool in tools)    system_msg = (        "You are a helpful AI assistant, collaborating with other assistants. Use the provided tools "        "to progress towards answering the question. If you are unable to fully answer, that's OK, another "        "assistant with different tools will help where you left off. Execute what you can to make progress. "        "If you or any of the other assistants have the final answer or deliverable, prefix your response with "        "FINAL ANSWER so the team knows to stop. You have access to the following tools, if you provide a tool "        "call, you must choose one of the following tools, you can't provide a name that isn't in this list: "        "{tool_names}.
{system_message}"    )    prompt = ChatPromptTemplate.from_messages(        [            ("system", system_msg),            MessagesPlaceholder(variable_name="messages"),        ]    ).partial(system_message=system_message, tool_names=tool_names)    return prompt | llm.bind_tools(tools)

Step 3: Define ToolsHere, we define two tools: one for online searching and another for executing Python code to generate charts.

from typing import Annotatedfrom langchain_community.tools.tavily_search import TavilySearchResultsfrom langchain_experimental.utilities import PythonREPL
# Search tooltavily_tool = TavilySearchResults(max_results=3)# Local Python code execution toolrepl = PythonREPL()@tooldef python_repl(code: Annotated[str, "The python code to execute to generate your chart."]):    """Execute python code. Print output to see the result, which will be visible to the user."""    try:        result = repl.run(code)        return f"Successfully executed:\n```python\n{code}\n```\nStdout: {result}\n\nIf you have completed all tasks, respond with FINAL ANSWER."    except BaseException as e:        return f"Failed to execute. Error: {repr(e)}"

Step 4: Create and Connect AgentsWe define two agents: one for research and another for chart generation. Each agent is equipped with the corresponding tools.

from langchain_openai import ChatOpenAIimport functools
llm = ChatOpenAI(model="gpt-3.5-turbo")# Research agentresearch_agent = create_agent(    llm,    [tavily_tool],    system_message="You should provide accurate data. You must invoke a tool name that is in the list of tools available; you can't use chart_generator as a tool name.",)research_node = functools.partial(agent_node, agent=research_agent, name="Researcher")# Chart generator agentchart_agent = create_agent(    llm,    [python_repl],    system_message="Any charts you display will be visible by the user.",)chart_node = functools.partial(agent_node, agent=chart_agent, name="chart_generator")

Step 5: Build the WorkflowUsing StateGraph, we define a dynamic workflow that allows agents to collaborate.

workflow = StateGraph(AgentState)
# Add nodes for agentsworkflow.add_node("Researcher", research_node)workflow.add_node("chart_generator", chart_node)workflow.add_node("call_tool", tool_node)# Add conditional edges between nodesworkflow.add_conditional_edges(    "Researcher",    router,    {"continue": "chart_generator", "call_tool": "call_tool", "__end__": END},)workflow.add_conditional_edges(    "chart_generator",    router,    {"continue": "Researcher", "call_tool": "call_tool", "__end__": END},)workflow.add_conditional_edges(    "call_tool",    lambda x: x["sender"],    {"Researcher": "Researcher", "chart_generator": "chart_generator"},)# Set entry pointworkflow.set_entry_point("Researcher")graph = workflow.compile()

Step 6: Invoke the SystemFinally, we invoke the system by providing a human query.

from langchain_core.messages import HumanMessage
human_message = HumanMessage(    content="Fetch the USA's GDP from 2015 to 2020, obtain data year by year, then draw a line graph of it. Once you code it up, finish.")# Stream events and visualize outputsevents = graph.stream(    {"messages": [human_message]},    {"recursion_limit": 20},)for event in events:    # Print messages from agents    for message in event.get("messages", []):        print(message)

This workflow dynamically coordinates tasks among agents to achieve the desired output. Try different queries and tools to explore the full potential of this system!

Interactive Example Now, let’s look at an example based on a user request to fetch GDP data for the USA from 2015 to 2020 and create a chart to visualize this data. This example demonstrates how a multi-agent system seamlessly automates data retrieval and visualization.

The research agent processes the information obtained using the Tavily tool after online data retrieval and forwards it to the chart generator agent. This ensures seamless collaboration among agents, efficiently meeting user needs.

Multi-Agent Workflow with LangGraph

The chart generator agent receives the data and writes the code needed to generate the chart. By dynamically creating visual content, it ensures that user requests are fulfilled efficiently and accurately.

Multi-Agent Workflow with LangGraph

Multi-Agent Workflow with LangGraph

Leave a Comment