Skip to main content

Workflows

Workflows orchestrate multi-step pipelines with shared state. Define steps (agents, functions, conditions, parallel execution), pass state between them, and handle retries and errors—all in a declarative, state-machine style.

Stateful Step-Based Execution

Shared State

Each step reads from and writes to a typed TState object passed through the workflow.

Step Types

AgentStep, FunctionStep, ConditionStep, ParallelStep.

Retry & Error Handling

Configurable retry policy and step result status (done, error, skipped).

Event-Driven

Optional EventBus for workflow lifecycle events.

WorkflowConfig

interface WorkflowConfig<TState> {
  name: string;
  initialState: TState;
  steps: StepDef<TState>[];
  storage?: StorageDriver;
  retryPolicy?: { maxRetries: number; backoffMs: number };
  eventBus?: EventBus;
}
name
string
required
Display name for the workflow.
initialState
TState
required
Initial state object. Steps read from and merge updates into this state.
steps
StepDef<TState>[]
required
Ordered array of steps. See Workflow Steps.
storage
StorageDriver
Storage for workflow persistence (e.g., resuming, audit).
retryPolicy
object
{ maxRetries, backoffMs } for failed steps. See Retry & Error Handling.
eventBus
EventBus
Custom event bus for workflow events.

State Machine Concept

Workflows behave like state machines:
  1. Initial stateinitialState is the starting point.
  2. Steps — Each step receives the current state and can return a partial update.
  3. State merge — Updates are merged into the state before the next step.
  4. ResultWorkflowResult contains final state and stepResults.

Basic Example

import { Workflow, Agent, openai } from "@radaros/core";

interface ContentState {
  topic: string;
  research_output?: string;
  write_output?: string;
}

const researcher = new Agent({
  name: "Researcher",
  model: openai("gpt-4o-mini"),
  instructions: "Research topics and provide factual summaries.",
});

const writer = new Agent({
  name: "Writer",
  model: openai("gpt-4o-mini"),
  instructions: "Write clear content from research notes.",
});

const workflow = new Workflow<ContentState>({
  name: "Content Pipeline",
  initialState: { topic: "Quantum computing basics" },
  steps: [
    {
      name: "research",
      agent: researcher,
      inputFrom: (state) => `Research: ${state.topic}`,
    },
    {
      name: "write",
      agent: writer,
      inputFrom: (state) => `Write a draft using: ${state.research_output}`,
    },
  ],
});

const result = await workflow.run();
console.log(result.state.write_output);

WorkflowResult

interface WorkflowResult<TState> {
  state: TState;
  stepResults: StepResult[];
}
state
TState
Final state after all steps (including any updates from steps).
stepResults
StepResult[]
Per-step results: stepName, status (done | error | skipped), error?, durationMs.

Next Steps