LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
  • LangGraph Checkpoint
    LangGraph Store
    Checkpoint Postgres
    Store Postgres
    Checkpoint SQLite
    LangGraph Prebuilt
    LangGraph CLI
    LangGraph SDK
    LangGraph Supervisor
    LangGraph Swarm
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    LangGraph Checkpoint
    LangGraph Store
    Checkpoint Postgres
    Store Postgres
    Checkpoint SQLite
    LangGraph Prebuilt
    LangGraph CLI
    LangGraph SDK
    LangGraph Supervisor
    LangGraph Swarm
    Language
    Theme
    Pythonlanggraph-swarmswarmcreate_swarm
    Function●Since v0.0

    create_swarm

    Create a multi-agent swarm.

    Copy
    create_swarm(
      agents: list[Pregel],
      *,
      default_active_agent: str,
      state_schema: StateSchemaType = 
    View source on GitHub
    SwarmState
    ,
    context_schema
    :
    type
    [
    Any
    ]
    |
    None
    =
    None
    ,
    **
    deprecated_kwargs
    :
    Unpack
    [
    DeprecatedKwargs
    ]
    =
    {
    }
    )
    ->
    StateGraph

    Parameters

    NameTypeDescription
    agents*list[Pregel]

    List of agents to add to the swarm

    An agent can be a LangGraph CompiledStateGraph, a functional API workflow, or any other Pregel object.

    default_active_agent*str

    Name of the agent to route to by default (if no agents are currently active).

    state_schemaStateSchemaType
    Default:SwarmState

    State schema to use for the multi-agent graph.

    context_schematype[Any] | None
    Default:None

    Example:

    from langgraph.checkpoint.memory import InMemorySaver
    from langchain.agents import create_agent
    from langgraph_swarm import create_handoff_tool, create_swarm
    
    def add(a: int, b: int) -> int:
        '''Add two numbers'''
        return a + b
    
    alice = create_agent(
        "openai:gpt-4o",
        tools=[
            add,
            create_handoff_tool(
                agent_name="Bob",
                description="Transfer to Bob",
            ),
        ],
        system_prompt="You are Alice, an addition expert.",
        name="Alice",
    )
    
    bob = create_agent(
        "openai:gpt-4o",
        tools=[
            create_handoff_tool(
                agent_name="Alice",
                description="Transfer to Alice, she can help with math",
            ),
        ],
        system_prompt="You are Bob, you speak like a pirate.",
        name="Bob",
    )
    
    checkpointer = InMemorySaver()
    workflow = create_swarm(
        [alice, bob],
        default_active_agent="Alice"
    )
    app = workflow.compile(checkpointer=checkpointer)
    
    config = {"configurable": {"thread_id": "1"}}
    turn_1 = app.invoke(
        {"messages": [{"role": "user", "content": "i'd like to speak to Bob"}]},
        config,
    )
    turn_2 = app.invoke(
        {"messages": [{"role": "user", "content": "what's 5 + 7?"}]},
        config,
    )

    Specifies the schema for the context object that will be passed to the workflow.