Multi Agent Collaboration Network

Open in Colab Open in GitHub

Overview

In this tutorial, we'll explore how to implement a multi-agent network using LangGraph. A multi-agent network is an architecture that leverages a "divide-and-conquer" approach by breaking down complex tasks into smaller, specialized agents.

This helps resolve the inefficiencies of a single agent managing numerous tools, allowing each agent to focus on its area of expertise and solve problems effectively.

Inspired by the AutoGen paper, this tutorial walks you through the process of building such multi-agent networks with LangGraph step-by-step.

network

Key Topics Covered

  • Defining States : How to define graph states and manage the state information required for each agent's actions

  • Defining Tools : How to specify tools that agents can use and add them as nodes

  • Creating Agents : How to define agents and set them as nodes in a LangGraph network

  • Defining Agent Nodes : How to define specialized agents as nodes

  • Defining Tool Nodes : How to define tools as nodes and enable agents to utilize them

  • Defining Edge Logic : How to set branching logic that directs agents or tools based on the results of an agent’s task

  • Graph Definition : How to combine agents, tools, states, and edge logic into a cohesive graph

  • Running the Graph : How to execute the graph and perform real-world tasks with the defined setup

Let's get started!

Table of Contents

References


Environment Setup

Setting up your environment is the first step. See the Environment Setup guide for more details.

[Note]

The langchain-opentutorial is a package of easy-to-use environment setup guidance, useful functions and utilities for tutorials. Check out the langchain-opentutorial for more details.

You can set API keys in a .env file or set them manually.

[Note] If you’re not using the .env file, no worries! Just enter the keys directly in the cell below, and you’re good to go.

Why Do We Use Multi-Agent Networks?

A single agent can be efficient when working within a specific domain and using a limited number of tools. However, as the number of tools increases, several challenges arise:

  1. The logic for managing tools becomes increasingly complex.

  2. The amount of information the agent needs to process at once grows, leading to inefficiencies.

By adopting a "divide-and-conquer" approach, each agent focuses on a specific task or area of expertise, while the overall workload is distributed across a network. Each agent handles what it does best and delegates tasks to other specialized agents or utilizes tools effectively when needed.

This structure enhances efficiency and simplifies the management of complex workflows.

Defining States

messages is a list of messages shared between agents, and sender refers to the sender of the most recent message.

Defining Tools

Here, we define a few tools that agents will use in the network:

  • TavilySearch : A tool for searching information on the internet. It is used by the research_agent to find the necessary data.

  • PythonREPL : A tool for executing Python code. It is used by the chart_agent to create charts.

Creating Agents

Here’s an example of how to create agents. In this tutorial, we’ll use the Research Agent and the Chart Generator Agent to build a multi-agent network.

First, define the LLM model and a common system prompt to be used for the agents.

Research Agent

Create an agent that performs research using the TavilySearch tool. This agent is used to gather the required information.

Chart Generator Agent

Create an agent that generates charts using the PythonREPL tool. This agent is used to create charts.

Building a Multi-Agent Graph

This workflow uses LangGraph to build a multi-agent system where agents collaborate dynamically.

The router decides the next step by analyzing messagesβ€”either continuing to the next node or ending the workflow.

Each node, like researcher and chart_generator, is connected using conditional edges that determine the workflow’s flow based on the router's logic. The system starts with the researcher and alternates between agents until a "FINAL ANSWER" is reached.

Let's take a look at the graph we’ve created by visualizing it!

png

Running the Agents

Now it’s time to execute the multi-agent workflow we’ve built!

Using the RunnableConfig, we set up necessary configurations like recursion limits and unique thread IDs.

Then, we input a query and invoke the graph, allowing the agents to collaborate and generate results based on the workflow.

Here’s an example of code that visualizes CO2 emissions from 2015 to 2024. If you’re curious about something else, try changing the query and see how well the agents handle it!

[NOTE] As of January 22 2025, GPT-4o's maximum context length is 128,000 tokens. Depending on the query, you might exceed this limit and encounter an error. If that happens, consider refining the agent graph or modifying your query.

png

Last updated