Low-level orchestration framework for building stateful, multi-actor applications with LLMs
Orchestrating multiple specialized agents.
const State = Annotation.Root({
messages: Annotation<BaseMessage[]>({
reducer: messagesStateReducer,
default: () => []
}),
next: Annotation<string>()
});
const supervisorNode = async (state: State) => {
const lastMessage = state.messages[state.messages.length - 1];
// Decide which agent to route to
let next = "finish";
if (needsResearch(lastMessage)) next = "researcher";
else if (needsCoding(lastMessage)) next = "coder";
return { next };
};
const router = (state: State) => state.next;
graph
.addNode("supervisor", supervisorNode)
.addNode("researcher", researchAgent)
.addNode("coder", coderAgent)
.addEdge(START, "supervisor")
.addConditionalEdges("supervisor", router, {
researcher: "researcher",
coder: "coder",
finish: END
})
.addEdge("researcher", "supervisor")
.addEdge("coder", "supervisor");const State = Annotation.Root({
query: Annotation<string>,
results: Annotation<Record<string, any>>({
reducer: (a, b) => ({ ...a, ...b }),
default: () => ({})
})
});
graph
.addNode("agent1", (s) => ({ results: { agent1: process1(s.query) } }))
.addNode("agent2", (s) => ({ results: { agent2: process2(s.query) } }))
.addNode("aggregate", aggregateNode)
.addEdge(START, "agent1")
.addEdge(START, "agent2")
.addEdge("agent1", "aggregate")
.addEdge("agent2", "aggregate");See: Common Patterns for more agent patterns.