Skip to main content

Workflow Steps

Workflows are built from four step types. Each step receives the current state and can update it for the next step.

Step Types Overview

StepPurpose
AgentStepRun an agent; optionally map state to input via inputFrom.
FunctionStepCustom logic; run(state, ctx) returns partial state update.
ConditionStepBranch: if condition(state) then run nested steps.
ParallelStepRun multiple steps concurrently; merge results.

AgentStep

Runs an agent. Use inputFrom to derive the agent’s input from state.
interface AgentStep<TState> {
  name: string;
  agent: Agent;
  inputFrom?: (state: TState) => string;
}
name
string
required
Step identifier. Used in stepResults and logs.
agent
Agent
required
The agent to run.
inputFrom
(state) => string
Maps state to the agent’s input string. If omitted, the full state is JSON-stringified. Agent output is stored as state[stepName + “_output”].

Example

{
  name: "research",
  agent: researcher,
  inputFrom: (state) => `Research: ${state.topic}`,
}

FunctionStep

Runs custom logic. Return a partial state object to merge into the workflow state.
interface FunctionStep<TState> {
  name: string;
  run: (state: TState, ctx: RunContext) => Promise<Partial<TState>>;
}
name
string
required
Step identifier.
run
(state, ctx) => Promise<Partial<TState>>
required
Async function. Receives current state and RunContext. Return partial state to merge.

Example

{
  name: "validate",
  run: async (state, ctx) => {
    if (!state.topic?.trim()) {
      throw new Error("Topic is required");
    }
    return { validated: true };
  },
}

ConditionStep

Branches based on a condition. Runs nested steps only when condition(state) is true.
interface ConditionStep<TState> {
  name: string;
  condition: (state: TState) => boolean;
  steps: StepDef<TState>[];
}
name
string
required
Step identifier.
condition
(state) => boolean
required
Predicate. If true, nested steps run; otherwise they are skipped.
steps
StepDef<TState>[]
required
Nested steps to run when condition is true.

Example

{
  name: "maybe-review",
  condition: (state) => state.needsReview === true,
  steps: [
    {
      name: "review",
      agent: reviewer,
      inputFrom: (state) => `Review: ${state.draft}`,
    },
  ],
}

ParallelStep

Runs multiple steps concurrently. Results are merged into state.
interface ParallelStep<TState> {
  name: string;
  parallel: StepDef<TState>[];
}
name
string
required
Step identifier.
parallel
StepDef<TState>[]
required
Steps to run in parallel. All must complete before the workflow continues.

Example

{
  name: "parallel-analysis",
  parallel: [
    { name: "tech", agent: techAnalyst, inputFrom: (s) => s.proposal },
    { name: "legal", agent: legalAnalyst, inputFrom: (s) => s.proposal },
    { name: "finance", agent: financeAnalyst, inputFrom: (s) => s.proposal },
  ],
}

Full Example