LingxiGraph
开发指南

多智能体模式

选择 supervisor、handoff、swarm、group chat 或并行评审

多智能体不是单一架构。先根据控制权、并发和终止条件选择模式,再把每个 Agent 作为子图或节点组合。

模式控制权适合
Supervisor中央路由器选择下一个 Agent角色清晰、需要统一策略
Manager as toolsManager 把专家当工具调用输出边界清晰的专家能力
Handoff当前 Agent 显式交接客服转接、分层升级
Swarmactive_agent 在状态中持久化对等协作、动态所有权
Group chatselector 在有限轮次内选 speaker讨论、辩论、共识
Plan-executePlanner 生成任务,executor 执行长任务与可观察计划
Parallel reviewfan-out 后 reducer 汇总代码/文档/风险多维评审

Handoff

create_handoff_tool() 产生 Command(scope=PARENT),让子 Agent 把控制权交回直接父图。跨图传递的状态必须明确写入 Command.update,不会隐式复制子图全部状态。

from lingxigraph.patterns import create_handoff_tool

handoff_to_billing = create_handoff_tool(
    "billing",
    description="Transfer billing and refund questions",
)

Map-reduce fan-out

条件边可返回多个 Send(node, arg),为同一节点创建私有输入的并行任务。结果通过 Annotated reducer 合并回共享状态。不要让并行节点写同一个普通 LastValue 字段。

生产护栏

  • 为 group chat 设置有限 max_turns 或确定性 termination;
  • 父子图共享 max_model_callsmax_tool_callsmax_tokensmax_cost
  • 为远端 Agent 设置 timeout、retry 和取消传播;
  • handoff 状态只包含下一个角色需要的数据;
  • 所有带副作用的专家工具使用稳定幂等键;
  • 在 event metadata 中保留 agent、namespace 与 task path,便于追踪。

仓库的 examples/multi_agent_supervisor.pyexamples/map_reduce_send.pyexamples/subgraph_team_review.py 提供可运行示例。

On this page