LangGraph Swarm AI agents collaborating seamlessly

Meet LangGraph Swarm Agents: A Collaborative AI Ecosystem

Imagine building powerful multi-agent systems with LangGraph Swarm, where agents collaborate autonomously for seamless AI workflows. That’s LangGraph Swarm: a lightweight, decentralized multi-agent system where agents dynamically hand off tasks and the system retains memory of the last active agent for seamless conversation flow

Unlike rigid supervisor architectures where a central agent dictates the flow, Swarm agents are autonomous. They self-monitor shared conversation history and decide when to hand off tasks, leading to flexible, emergent problem-solving with minimal orchestration

What Makes Swarm Special

Peer-to-peer task delegation

No single “manager” agent decides the flow. Instead, any agent can trigger a handoff when it encounters content outside its expertise, ensuring modular responsibility sharing  .

Contextual awareness

Swarm maintains an internal state including the active agent and conversation history. When a request crosses domains, say from a tech question to translation, control switches fluidly, and the conversation carries on in context.

Flexible and extensible

Need to scale? Just add new agents. Whether it’s finance, travel, or medical advice, agents can plug in with their own tools and prompts. Swarm handles handoff and context, keeping each domain neatly encapsulated.


Walkthrough: Build a 3-Agent Swarm (Q&A, Science, Translator)


Let’s walk through a multi-domain scenario with code:

from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
from langgraph_swarm import create_handoff_tool, create_swarm
from langgraph.checkpoint.memory import InMemorySaver

llm = ChatOpenAI(model="gpt-4o")

qa_agent = create_react_agent(
    llm,
    tools=[
        question_answering_tool,
        create_handoff_tool(agent_name="science_agent", description="Forward science questions"),
        create_handoff_tool(agent_name="translator_agent", description="Forward translation requests")
    ],
    prompt=(
        "You are a general Q&A agent. For scientific queries, hand off. "
        "For translation requests, hand off. Otherwise, answer directly."
    ),
    name="qa_agent",
)

science_agent = create_react_agent(
    llm,
    tools=[
        science_tool,
        create_handoff_tool(agent_name="qa_agent", description="Return to QA"),
        create_handoff_tool(agent_name="translator_agent", description="Forward translation")
    ],
    prompt="You are a science expert. If not a science question, hand off.",
    name="science_agent",
)

translator_agent = create_react_agent(
    llm,
    tools=[
        translate_tool,
        create_handoff_tool(agent_name="qa_agent", description="Return to QA"),
        create_handoff_tool(agent_name="science_agent", description="Return to Science")
    ],
    prompt="You are a translation specialist. If not translation, hand off.",
    name="translator_agent",
)

workflow = create_swarm(
    [qa_agent, science_agent, translator_agent],
    default_active_agent="qa_agent"
)
app = workflow.compile(checkpointer=InMemorySaver())

# Demo Interaction
^[config = {"configurable": {"thread_id": "42"}}]({"attribution":{"attributableIndex":"1718-1"}})
^[resp1 = app.invoke({"messages":[{"role":"user","content":"Explain photosynthesis."}]}, config)]({"attribution":{"attributableIndex":"1718-2"}})
^[resp2 = app.invoke({"messages":[{"role":"user","content":"Translate that to Spanish."}]}, config)]({"attribution":{"attributableIndex":"1718-3"}})

Flow Explained:

Throughout, the system preserves context and active_agent state.

User asks a science question → QA Agent hands off to Science Agent.

Science Agent answers in greater depth, then retains control.

Next, translation is requested → Translator Agent takes over.

Deep Dive: How Hand-offs Work

Under the hood, Swarm uses LangGraph’s Command object to direct agent transitions. Each create_handoff_tool encapsulates a command to switch active agents, optionally carrying metadata or summaries. This dirigible graph structure gives clarity and control over how agents collaborate and when they should step in or step aside.

Agent-Oriented System: A Real Case

In a travel booking assistant, you might have:

  • Flight Agent: searches and books flights.
  • Hotel Agent: handles accommodation.
  • Weather Agent: fetches forecasts.
  • Support Agent: answers general questions.

Each has its domain-specific tools and handoff capabilities. Start with Flight Agent, then add handoffs to Hotel Agent or Weather Agent based on user’s needs. Context flows naturally through the shared state, and the right expert is always active 

Why Swarm Works for Developers

  • Decoupled code: no monolithic prompt, each agent handles a clear scope.
  • Scalable architecture: new features are full-blown agents, not hacky prompt additions.
  • Robust context flow: shared state ensures conversations don’t break when handing off.
  • Lightweight but powerful: Swarm provides structure without enforcing centralized orchestration.

LangGraph Swarm transforms multi-agent collaboration: agents are self-aware collaborators, not tools called by a central brain. They route, respond, and remember, forming a brilliant AI ensemble.

Leave a Comment


Alpesh Kumar
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.