workflow; only reach for stateGraph when you need cycles or branching that depends on accumulated state.
At a glance
The three patterns differ in topology:Workflow: linear pipeline
Workflow: linear pipeline
Each step runs in order. A
when predicate can skip a step, and transform can rewrite the input to the next step.Parallel: concurrent fan-out
Parallel: concurrent fan-out
All agents run at the same time on the same input. Fulfilled results are collected; rejected steps are isolated.
StateGraph: cycles and routing
StateGraph: cycles and routing
Nodes share a typed state object. Routing functions inspect the state after each node and pick the next node,
END, or loop back. maxIterations bounds cycles.Workflow
AWorkflow is a fixed list of steps. Each step receives the previous step’s response (or a transform of it) and returns a new response.
Step options
Each step can be a bareAgentHandle, a Parallel group (auto-wrapped), or a configuration object:
WorkflowResult
Parallel
Parallel runs multiple agents concurrently on the same input. Use it standalone or drop it into a workflow step list.
Per-step overrides
Pass per-step input or credentials when branches need different data:ParallelResult
Parallel swallows individual failures. Inspect rejected if you need fail-fast semantics. When used inside a Workflow, if all parallel steps fail, the workflow throws.StateGraph
When you need cycles or branching that depends on accumulated state, useStateGraph<TState>. Each node mutates a typed state object; edges route to the next node, including loops bounded by maxIterations.
Concepts
- State: a plain typed object that accumulates across every node execution. Each node returns a
Partial<S>that is shallow-merged in. - Nodes: async functions
(state: S) => Promise<Partial<S>>. UseagentNode()to wrap anAgentHandle. - Edges: a static node name,
END, or a routing function(state: S) => string | ENDthat runs after the node updates the state. END: sentinel that stops execution. A node with no registered edge also terminates the run.
Minimal example
agentNode
Wraps anAgentHandle as a node function. Provide two callbacks: one to extract the agent’s input from state, and one to merge the response back.