Back to all posts
Saket's Blog

Building Multi-Agent Systems with LangChain & CrewAI

2024-11-18
15 min read
AIMulti-AgentLangChainCrewAI

Building Multi-Agent Systems with LangChain & CrewAI

Multi-agent AI systems represent one of the most exciting developments in applied artificial intelligence. By coordinating multiple specialized AI agents, we can solve complex problems that would be difficult for a single model to handle effectively.

What are Multi-Agent Systems?

Multi-agent systems consist of multiple AI agents, each with:

  • Specialized roles and capabilities
  • The ability to communicate with other agents
  • A shared goal or task to accomplish

Together, these agents can tackle complex tasks through collaboration, specialization, and structured workflows.

Why Use Multi-Agent Architectures?

  • Specialization: Each agent can excel at specific subtasks
  • Scalability: Complex problems can be broken down into manageable components
  • Resilience: The system can continue functioning even if some agents fail
  • Emergent Intelligence: Collective capabilities often exceed the sum of individual agents

Building with LangChain

LangChain provides excellent primitives for creating multi-agent systems. Here's how to create a simple research team using LangChain:

from langchain.agents import initialize_agent, Tool
from langchain.chains import LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.tools import DuckDuckGoSearchRun

# Create the base language model
llm = ChatOpenAI(temperature=0)

# Create specialized tools
search_tool = DuckDuckGoSearchRun()

# Create researcher agent
researcher_tools = [
    Tool(
        name="SearchTool",
        func=search_tool.run,
        description="Useful for searching the internet for current information"
    )
]

researcher = initialize_agent(
    researcher_tools,
    llm,
    agent="zero-shot-react-description",
    verbose=True,
    handle_parsing_errors=True,
)

# Create writer agent
writer_prompt = PromptTemplate(
    input_variables=["research", "topic"],
    template="Based on this research: {research}\nWrite a comprehensive article about {topic}."
)

writer = LLMChain(llm=llm, prompt=writer_prompt)

# Coordinator function
def research_and_write(topic):
    # Step 1: Research phase
    research_task = f"Research the following topic thoroughly: {topic}"
    research_results = researcher.run(research_task)
    
    # Step 2: Writing phase
    article = writer.run(research=research_results, topic=topic)
    
    return article

# Execute the workflow
article = research_and_write("Latest advancements in quantum computing")
print(article)

Building with CrewAI

CrewAI is specifically designed for multi-agent orchestration with a focus on roles and coordination:

from crewai import Crew, Agent, Task
from crewai.tools import DuckDuckGoSearch

# Create the search tool
search_tool = DuckDuckGoSearch()

# Define agents with specific roles
researcher = Agent(
    role="Research Specialist",
    goal="Find accurate and comprehensive information on the given topic",
    tools=[search_tool],
    backstory="You are an expert researcher with decades of experience gathering information."
)

writer = Agent(
    role="Content Writer",
    goal="Create engaging and informative content based on research",
    backstory="You are a skilled writer who specializes in making complex topics accessible."
)

editor = Agent(
    role="Content Editor",
    goal="Ensure content is accurate, well-structured, and error-free",
    backstory="You have years of experience editing technical and educational content."
)

# Define tasks
research_task = Task(
    description="Research the latest advancements in quantum computing",
    agent=researcher,
    expected_output="Comprehensive research notes on quantum computing advancements"
)

writing_task = Task(
    description="Write an informative article about quantum computing advancements",
    agent=writer,
    expected_output="Draft article about quantum computing",
    context=[research_task]  # This task depends on the research task
)

editing_task = Task(
    description="Edit and improve the article for clarity and accuracy",
    agent=editor,
    expected_output="Final polished article ready for publication",
    context=[writing_task]  # This task depends on the writing task
)

# Create the crew
crew = Crew(
    agents=[researcher, writer, editor],
    tasks=[research_task, writing_task, editing_task],
    verbose=True
)

# Execute the workflow
result = crew.kickoff()
print(result)

Challenges in Multi-Agent Systems

While powerful, multi-agent systems come with challenges:

  1. Coordination Overhead: Agents must effectively communicate and coordinate
  2. Error Propagation: Mistakes by one agent can affect the entire system
  3. Cost Management: Running multiple LLM instances increases API costs
  4. Evaluation Complexity: Assessing performance becomes more difficult

Best Practices

  • Clear Role Definition: Each agent should have specific responsibilities
  • Structured Information Flow: Design explicit communication protocols
  • Fallback Mechanisms: Implement error handling for agent failures
  • Human-in-the-Loop: Add oversight for critical decision points

Conclusion

Multi-agent systems represent a powerful paradigm for solving complex problems by combining specialized AI capabilities. Both LangChain and CrewAI provide excellent frameworks for implementing these systems, with CrewAI offering more role-focused abstractions.

In future posts, we'll explore more advanced multi-agent patterns and real-world applications.