DEV Community

qing
qing

Posted on

Build a Multi-Agent AI System with LangChain

Build a Multi-Agent AI System with LangChain

Imagine you’ve built an AI agent that can search the web, another that can read documents, and a third that can write code. Now, what if you could make them talk to each other, debate ideas, and hand off tasks until they solve a problem no single agent could handle alone? That’s the power of a multi-agent AI system, and with LangChain, you can build one today without needing a PhD in distributed systems.

Multi-agent systems aren’t just a buzzword—they’re the next evolution of AI applications. Instead of relying on one monolithic model to do everything, you break the work into specialized roles: a researcher, a critic, a writer, and maybe even a code executor. Each agent focuses on its job, shares context with the others, and iterates until the output is solid. And the best part? LangChain makes this surprisingly accessible.

Let’s build a real, working multi-agent system from scratch that you can run on your laptop in under 10 minutes.

Why Multi-Agent Systems Matter

Single-agent AI often hits a wall when tasks get complex. It might hallucinate facts, miss context, or fail to validate its own output. Multi-agent systems solve this by introducing specialization, collaboration, and iteration.

According to production guides, the key success factors for multi-agent systems are:

  • Agent specialization: Each agent has a clear, focused purpose [2].
  • Communication patterns: Define how agents exchange information [2].
  • State management: Maintain consistent state across interactions [2].
  • Error handling: Build resilient systems that recover from failures [2].
  • Monitoring: Track performance using tools like LangSmith [2].

In practice, this means your “researcher” agent never tries to write prose, your “critic” never searches the web, and your “writer” only refines what the critic validates.

The Architecture: Supervisor + Sub-Agents

The most effective pattern for beginners is the supervisor model: one central agent (the supervisor) decides which sub-agent should handle the next step, routes the task, and aggregates the final result.

LangChain’s LangGraph library makes this easy. You define:

  • Sub-agents (e.g., search, read, write)
  • Tools each agent can use
  • A supervisor that orchestrates the flow [6][8]

This approach keeps your system simple, testable, and scalable.

Building Your First Multi-Agent System

Let’s create a system with three agents:

  1. Search Agent: Finds relevant information online.
  2. Reader Agent: Extracts key insights from URLs.
  3. Writer Agent: Synthesizes findings into a clear report.

We’ll use OpenAI’s gpt-4o (or any compatible model) and LangChain’s create_react_agent for simplicity.

Step 1: Install Dependencies

pip install langchain langchain-community langgraph openai
Enter fullscreen mode Exit fullscreen mode

Step 2: Set Up Your Environment

from langchain_openai import ChatOpenAI
from langchain_community.tools import DuckDuckGoSearchResults, WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper
from langgraph import StateGraph, Command
from typing import TypedDict, List

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

# Define tools
search_tool = DuckDuckGoSearchResults()
wiki_tool = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
tools = [search_tool, wiki_tool]
Enter fullscreen mode Exit fullscreen mode

Step 3: Define Agent States

class AgentState(TypedDict):
    task: str
    search_results: List[str]
    wiki_content: str
    draft: str
    final_report: str
Enter fullscreen mode Exit fullscreen mode

Step 4: Create Sub-Agents

def search_agent(state: AgentState):
    results = search_tool.invoke({"query": state["task"]})
    return {"search_results": [r["snippet"] for r in results]}

def reader_agent(state: AgentState):
    # Pick the first URL from search results (simplified)
    if not state["search_results"]:
        return {"wiki_content": ""}
    url = "https://en.wikipedia.org/wiki/" + state["task"].split()[0]  # Simplified
    content = wiki_tool.invoke({"query": state["task"].split()[0]})
    return {"wiki_content": content}

def writer_agent(state: AgentState):
    prompt = f"""
    Based on the following search results and Wikipedia content, write a concise report on: {state['task']}

    Search Results:
    {state['search_results']}

    Wikipedia Content:
    {state['wiki_content']}

    Write a clear, well-structured report.
    """
    response = llm.invoke(prompt)
    return {"draft": response.content}
Enter fullscreen mode Exit fullscreen mode

Step 5: Build the Supervisor Graph

def supervisor(state: AgentState):
    if not state["search_results"]:
        return Command(update=search_agent(state), goto="search_agent")
    elif not state["wiki_content"]:
        return Command(update=reader_agent(state), goto="reader_agent")
    elif not state["draft"]:
        return Command(update=writer_agent(state), goto="writer_agent")
    else:
        return Command(goto="__end__")

# Build the graph
workflow = StateGraph(AgentState)
workflow.add_node("search_agent", search_agent)
workflow.add_node("reader_agent", reader_agent)
workflow.add_node("writer_agent", writer_agent)
workflow.add_node("supervisor", supervisor)

workflow.set_entry_point("supervisor")
workflow.add_edge("supervisor", "supervisor")  # Loop until done

app = workflow.compile()
Enter fullscreen mode Exit fullscreen mode

Step 6: Run It!

initial_state = {"task": "climate change impacts on coastal cities", 
                 "search_results": [], 
                 "wiki_content": "", 
                 "draft": "", 
                 "final_report": ""}

result = app.invoke(initial_state)
print(result["draft"])
Enter fullscreen mode Exit fullscreen mode

This code will:

  1. Search for “climate change impacts on coastal cities”
  2. Fetch Wikipedia content about the top topic
  3. Write a concise report synthesizing both sources

You can run this today with your own API key. Just replace OPENAI_API_KEY in your environment.

Making It Production-Ready

The example above is a minimal prototype. To scale it:

  • Add more agents: e.g., a “critic” to validate the report [2][4].
  • Use LangSmith: Trace execution flows and monitor latency [2].
  • Implement error handling: Retry failed tool calls, fallback to alternative tools [2].
  • Persist state: Save intermediate results to a database for auditability [2].

LangGraph’s Command routing lets you dynamically switch agents based on the supervisor’s decision, making your system flexible and resilient [6].

Start Building Today

Multi-agent AI isn’t a distant future—it’s available now, and LangChain makes it accessible to developers at any level. You don’t need to rebuild the entire stack; just start with two or three specialized agents, test them individually, then connect them with a supervisor [2].

Your first system might be simple: a researcher that finds data, a writer that formats it, and a critic that checks accuracy. But once you see how they collaborate, you’ll realize this is how AI scales.

Ready to try it?

  1. Copy the code above into a multi_agent.py file.
  2. Set your OPENAI_API_KEY.
  3. Run python multi_agent.py and watch your agents collaborate.
  4. Share your results on Dev.to and tag me—I’d love to see what you build!

The future of AI isn’t one giant model. It’s a team of smart, specialized agents working together. And you’re now ready to build that team.


If you found this helpful, consider buying me a coffee ☕ — it keeps these articles coming!

Also check out my AI tools collection: AI 次元世界 — free AI tools for developers.


🛠️ Recommended Tool

If you found this useful, check out Content Creator Ultimate Bundle (Save 33%) — $29.99 and designed for developers like you.

Get instant access to our best-selling AI Dev Boost, HTML Landing Page Templates, AI Prompts for Developers, and Python Automation Scripts Pack, perfect for content creators and marketers looking to elevate their game. This bundle is a must-have for anyone looking to create stunning content, build high-converting landing pages, and drive real results. With these tools, you'll be able to create engaging content, build beautiful landing pages, and boost your online presence.

Top comments (0)