Langchain & LangGraph Based Multi-Agent AI Workflow

With the advent of large language models (LLMs), MultiAgent workflows are gaining traction nowadays. Let's explore one such workflow to understand what MultiAgent workflows are, examine their main components, and see an example of how to use this type of workflow effectively.

Components of a Multi-Agent Workflow Using LangGraph

  1. Large Language Model (LLM): This serves as the brain of the Multi-Agent workflow, handling all the tasks assigned to an agent.
  2. Agents: These are personas assigned to an LLM to perform specific tasks or functions. Each agent operates semi-independently and communicates with other agents to achieve a common goal.
  3. State & Tools: This represents the shared context in which agents operate, including the data, resources, and any external systems or interfaces the agents interact with.
  4. Graph: The state graph acts as the binding glue for agents. Based on the current state, it invokes the next agent or tool. The state graph serves as the blueprint for how communication among agents will occur.

Example Workflow Using ChatGPT's 4.0 Model

In this example, we will create a workflow with two agents:

  1. Business Ideas Agent: Generates business ideas based on user input.
  2. Business Plan Agent: Develops an elaborate plan for each business idea generated by the Business Ideas Agent.
agent workflow

Workflow Process

  1. The user provides an input, which is processed by the Business Ideas Agent . This agent generates a few business ideas based on the user's prompt.
  2. The generated ideas are passed on to the Business Plan Agent , which develops detailed plans for each business idea. This agent processes the ideas one by one.
  3. Once all plans are generated, the workflow stops.

Structured Output

We will use a structured output format, ensuring that the output generated by each agent follows a fixed structure. This makes data processing easier, with everything stored in a central state that is updated at each step.

Implementing the Workflow

We will use Python with Langchain & LangGraph to define our workflow. Here's how you can get started:

  
pip install langchain langgraph pydantic dotenv
  

Define the state

  from typing import TypedDict, Optional, List

class GraphState(TypedDict):
    """ Represents the state of our graph """
    prompt: str
    business_ideas: List[str]
    business_ideas_plans: List[dict[str, str]]
    business_idea_number: int
  

Initialising the OpenAI model

  from dotenv import load_dotenv
# OpenAI api key is stored in .env file
load_dotenv()

llm = ChatOpenAI(model="gpt-4o", temperature=0)
  

setting up our Agent's persona is easy, we have to set the system prompt detailing what are the boundaries of this agent

  from langchain_core.prompts import ChatPromptTemplate

system_prompt_business_ideas = """
you are an assistant who generates business ideas. 
you will generate business ideas based on the input given by the human.
Make sure to answer in json format
Generate at most 2 business ideas.

here is one example:
example_user: can you generate some business ideas for me related to real estate?
example_assistant: {{"business_ideas": ["real estate app", "real estate investment platform", "real estate management software"]}}
"""

business_ideas = ChatPromptTemplate.from_messages([
    ("system", system_prompt_business_ideas),
    ("human", "{prompt}")

])

  

in the prompt we are providing instructions to the Agent on what it should do, also provided an exampe interaction, note that we have explicitly mentioned that the response should be in JSON format, for this to work properly, pydantic library comes in handy, we can create a schema and bind that to the llm

Here is the schema for Business Ideas Agent's response

  from pydantic import BaseModel, Field

class GeneratedIdeas(BaseModel):
    """generate business ideas in this format"""
    business_ideas: Optional[List[str]] = Field([], description="list of the business ideas generated by the assistant")
  

Binding the schema with our model and prompt

  business_ideas_agent = business_ideas | llm.with_structured_output(GeneratedIdeas, method="json_mode")
  

Similarly defining the business planning agent

  from typing import List
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langgraph.graph import END
from pydantic import BaseModel
from dotenv import load_dotenv

load_dotenv()

llm = ChatOpenAI(model="gpt-4o", temperature=0)


class BusinessPlan(BaseModel):
    """Business plan for a given business idea"""
    product_vision: str
    main_features: List[str]
    market_research: str
    competitor_analysis: str
    challenges: str


system_prompt = """
you are an assistant who writes elaborate and stunning business plans on a given business ideas.
your plans are unique and cover the following areas:
- Product Vision
- Main Features
- Market Research
- Competitor Analysis
- Challenges

all the above areas should be answered in json format
"""

business_plan_prompt = ChatPromptTemplate.from_messages([
    ("system", system_prompt),
    ("human", "{business_idea}")
])

business_plan_agent = business_plan_prompt | llm.with_structured_output(BusinessPlan, method="json_mode")
  

Next, we need to define how these Agents interact with each other, this is represented using nodes on graph, for which Langgraph comes in, lets first define our Agents as nodes , each node takes in the current state and updates it and returns the updated state

  # Initial state
prompt = """can you generate some business ideas for me related to recruitment market focus on Tech recruiting?"""
initial_state = {"prompt": prompt, "business_ideas_plans": [], "business_idea_number": 0}

def business_ideas_node(state):
    result = business_ideas_agent.invoke(state)
    if "business_ideas" in result:
        state["business_ideas"] = result["business_ideas"]
    return state
 
def business_plan_node(state):

  	business_idea = state["business_ideas"][state["business_idea_number"]]
    if "business_idea" is None:
        return END

    result = business_plan_agent.invoke({"business_idea": business_idea})

    state["business_ideas_plans"].append(result)
    state["business_idea_number"] += 1
    return state
  

The logic here is simple, for Business Ideas node we pass the initial state to the agent and get the results back, store these results in the state and return it back for next node to process.

When the state is passed to the business plan node it reads in the business idea from the state, based on business_idea_number , invokes the agent and updates the business_idea_number, so next time when this node is invokes, we process the next business idea.

Now that our nodes are ready, we need to create logic for when to end the run for our agent graph, this is defined using conditional edges , here is our conditional edge

  # decides which node to call next
# END is a special node which signals the end of our agents run

def should_end(state: GraphState):
    pprint(f" in Should end state {state}")
    if state["business_idea_number"] >= len(state["business_ideas"]):
        return END
    return "business_plan" 

  

in should_end we check if all business ideas are processed then signal END else invoke our business_plan edge, last os to define the graph for our agents

  
# initialize the workflow
agents_workflow = StateGraph(GraphState)
# add nodes and edges
agents_workflow.add_node("business_idea", business_ideas_node)
agents_workflow.add_node("business_plan", business_plan_node)
agents_workflow.add_edge("business_idea", "business_plan")
# add conditional edges
agents_workflow.add_conditional_edges("business_plan", should_end)
# set entry point which node to start from
agents_workflow.set_entry_point("business_idea")
# compile the graph
app = agents_workflow.compile()

print(app.get_graph().print_ascii())
  
    #our Agent Graph
  +-----------+    
  | __start__ |    
  +-----------+    
        *          
        *          
        *          
+---------------+  
| business_idea |  
+---------------+  
        .          
        .          
        .          
+---------------+  
| business_plan |  
+---------------+  
        .          
        .          
        .          
   +---------+     
   | __end__ |     
   +---------+     

  

Lets now run our workflow and validate the graph

   for output in app.stream(initial_state, stream_mode="values"):
	    pprint(output)
  

If Everythings goes well, you should see the following output in the terminal

  # Initial Prompt
{'prompt': 'can you generate some business ideas for me related to recruitment market focus on Tech recruiting?', 'business_ideas_plans': [], 'business_idea_number': 0}

# Response from Business Ideas Agent
{'prompt': 'can you generate some business ideas for me related to recruitment market focus on Tech recruiting?', 'business_ideas': ['AI-Powered Tech Recruitment Platform', 'Tech Talent Assessment and Training Service'], 'business_ideas_plans': [], 'business_idea_number': 0}

# Response from Business Planning Agent
{'prompt': 'can you generate some business ideas for me related to recruitment market focus on Tech recruiting?', 'business_ideas': ['AI-Powered Tech Recruitment Platform', 'Tech Talent Assessment and Training Service'], 'business_ideas_plans': [{'Product Vision': {'description': 'To revolutionize the tech recruitment industry by leveraging artificial intelligence to match top tech talent with the right...'}, 'Main Features': {'1. AI-Driven Matching Algorithm': 'Utilizes machine learning to analyze job descriptions and candidate profiles, ensuring the best fit based on skills, experience, and cultural alignment.', '2. Automated Screening': 'Automatically screens resumes and applications, filtering out unqualified candidates and highlighting top talent.', '3. Predictive Analytics': 'Provides insights and predictions on candidate success and retention based on historical data and trends.', '4. Chatbot Assistance': 'Offers 24/7 support to candidates and recruiters through an AI-powered chatbot, answering queries and providing updates.', '5. Skill Assessment Tools': 'Includes coding challenges, technical quizzes, and other assessment tools to evaluate candidate skills accurately.', '6. Diversity and Inclusion Metrics': 'Tracks and reports on diversity metrics to help companies build more inclusive teams.', '7. Integration with ATS': 'Seamlessly integrates with existing Applicant Tracking Systems (ATS) to streamline the recruitment process.'}, 'Market Research': {'industry_trends': 'The global recruitment market is expected to grow significantly, driven by the increasing demand for skilled tech professionals and the adoption of AI in recruitment processes.', 'target_audience': 'Tech companies, startups, and enterprises looking to hire software developers, data scientists, IT professionals, and other tech roles.', 'market_size': 'The global recruitment market was valued at $215 billion in 2020 and is projected to reach $334 billion by 2025.', 'growth_opportunities': 'There is a growing need for efficient and effective recruitment solutions, especially in the tech industry where the demand for talent often outstrips supply.'}, 'Competitor Analysis': {'1. LinkedIn Talent Solutions': {'strengths': 'Large user base, strong brand recognition, extensive network.', 'weaknesses': 'High cost, less focus on AI-driven matching.'}, '2. Hired': {'strengths': 'Specialized in tech recruitment, transparent salary offers.', 'weaknesses': 'Limited to certain regions, smaller candidate pool.'}, '3. HackerRank': {'strengths': 'Strong focus on skill assessments, popular among developers.', 'weaknesses': 'Primarily assessment-focused, less comprehensive recruitment features.'}, '4. ZipRecruiter': {'strengths': 'Wide reach, user-friendly interface.', 'weaknesses': 'General recruitment platform, not specialized in tech.'}}, 'Challenges': {'1. Data Privacy': 'Ensuring the protection of candidate and employer data in compliance with global data protection regulations.', '2. Bias in AI': 'Mitigating any potential biases in the AI algorithms to ensure fair and unbiased candidate matching.', '3. Market Penetration': 'Establishing a strong presence in a competitive market with established players.', '4. Integration with Existing Systems': 'Ensuring seamless integration with various Applicant Tracking Systems (ATS) and other HR tools used by companies.', '5. Keeping Up with Tech Trends': 'Continuously updating the platform to keep pace with the rapidly evolving tech industry and recruitment practices.'}}], 'business_idea_number': 1}

# 2nd Response from business Planning agent
{'prompt': 'can you generate some business ideas for me related to recruitment market focus on Tech recruiting?', 'business_ideas': ['AI-Powered Tech Recruitment Platform', 'Tech Talent Assessment and Training Service'], 'business_ideas_plans': [{'Product Vision': {'description': 'To revolutionize the tech recruitment industry by leveraging artificial intelligence to match top tech talent with the right job opportunities, ensuring a seamless and efficient hiring process for both employers and candidates.'}, 'Main Features': {'1. AI-Driven Matching Algorithm': 'Utilizes machine learning to analyze job descriptions and candidate profiles, ensuring the best fit based on skills, experience, and cultural alignment.', '2. Automated Screening': 'Automatically screens resumes and applications, filtering out unqualified candidates and highlighting top talent.', '3. Predictive Analytics': 'Provides insights and predictions on candidate success and retention based on historical data and trends.', '4. Chatbot Assistance': 'Offers 24/7 support to candidates and recruiters through an AI-powered chatbot, answering queries and providing updates.', '5. Skill Assessment Tools': 'Includes coding challenges, technical quizzes, and other assessment tools to evaluate candidate skills accurately.', '6. Diversity and Inclusion Metrics': 'Tracks and reports on diversity metrics to help companies build more inclusive teams.', '7. Integration with ATS': 'Seamlessly integrates with existing Applicant Tracking Systems (ATS) to streamline the recruitment process.'}, 'Market Research': {'industry_trends': 'The global recruitment market is expected to grow significantly, driven by the increasing demand for skilled tech professionals and the adoption of AI in recruitment processes.', 'target_audience': 'Tech companies, startups, and enterprises looking to hire software developers, data scientists, IT professionals, and other tech roles.', 'market_size': 'The global recruitment market was valued at $215 billion in 2020 and is projected to reach $334 billion by 2025.', 'growth_opportunities': 'There is a growing need for efficient and effective recruitment solutions, especially in the tech industry where the demand for talent often outstrips supply.'}, 'Competitor Analysis': {'1. LinkedIn Talent Solutions': {'strengths': 'Large user base, strong brand recognition, extensive network.', 'weaknesses': 'High cost, less focus on AI-driven matching.'}, '2. Hired': {'strengths': 'Specialized in tech recruitment, transparent salary offers.', 'weaknesses': 'Limited to certain regions, smaller candidate pool.'}, '3. HackerRank': {'strengths': 'Strong focus on skill assessments, popular among developers.', 'weaknesses': 'Primarily assessment-focused, less comprehensive recruitment features.'}, '4. ZipRecruiter': {'strengths': 'Wide reach, user-friendly interface.', 'weaknesses': 'General recruitment platform, not specialized in tech.'}}, 'Challenges': {'1. Data Privacy': 'Ensuring the protection of candidate and employer data in compliance with global data protection regulations.', '2. Bias in AI': 'Mitigating any potential biases in the AI algorithms to ensure fair and unbiased candidate matching.', '3. Market Penetration': 'Establishing a strong presence in a competitive market with established players.', '4. Integration with Existing Systems': 'Ensuring seamless integration with various Applicant Tracking Systems (ATS) and other HR tools used by companies.', '5. Keeping Up with Tech Trends': 'Continuously updating the platform to keep pace with the rapidly evolving tech industry and recruitment practices.'}}, {'Product Vision': {'description': 'To revolutionize the tech industry by providing a comprehensive talent assessment and training service that identifies, nurtures, and enhances the skills of tech professionals, ensuring they meet the evolving demands of the industry.'}, 'Main Features': {'1. Skill Assessment': 'A robust platform that evaluates technical skills through coding challenges, project-based assessments, and theoretical tests.', '2. Personalized Training Programs': 'Customized learning paths based on assessment results, including courses, workshops, and hands-on projects.', '3. Real-time Feedback': 'Instant feedback on assessments and training progress to help users understand their strengths and areas for improvement.', '4. Industry-Standard Certifications': 'Accredited certifications upon completion of training programs to validate skills and enhance employability.', '5. Employer Dashboard': 'A dedicated portal for employers to track the progress of their employees, access assessment results, and identify top talent.', '6. Community and Networking': 'A platform for users to connect, share knowledge, and collaborate on projects, fostering a community of continuous learning.'}, 'Market Research': {'industry_trends': 'The tech industry is rapidly evolving, with a growing demand for skilled professionals in areas such as AI, cybersecurity, and software development. Companies are increasingly investing in upskilling their workforce to stay competitive.', 'target_audience': 'Tech professionals seeking to enhance their skills, recent graduates entering the tech industry, and companies looking to assess and train their employees.', 'market_size': 'The global market for tech training and assessment is projected to reach $20 billion by 2025, driven by the increasing need for continuous learning and skill development.'}, 'Competitor Analysis': {'1. Coursera': {'strengths': 'Wide range of courses, partnerships with top universities, recognized certifications.', 'weaknesses': 'Less focus on personalized training and real-time feedback.'}, '2. Pluralsight': {'strengths': 'Comprehensive tech-focused courses, skill assessments, and analytics.', 'weaknesses': 'Higher cost, less emphasis on community and networking.'}, '3. Udacity': {'strengths': 'Nanodegree programs, project-based learning, industry partnerships.', 'weaknesses': 'Limited course variety, higher price point.'}}, 'Challenges': {'1. Market Saturation': 'The market for tech training and assessment is becoming increasingly crowded, making it challenging to stand out.', '2. Keeping Content Updated': 'Ensuring that training materials and assessments remain current with the latest industry trends and technologies.', '3. User Engagement': 'Maintaining high levels of user engagement and motivation throughout the training programs.', '4. Scalability': 'Scaling the platform to accommodate a growing number of users while maintaining quality and personalized experiences.'}}], 'business_idea_number': 2}

  

Source code

Click here for the source code.