Skip to main content
Workflows let you chain multiple agents, functions, and connectors into a single execution pipeline with branching, parallel execution, iteration, and full observability.

Quick Start

The flow() API is the recommended way to create workflows. The legacy createWorkflow() still works but is deprecated.

Core Concepts

Steps

Each step receives the previous step’s output as input, plus a ctx object with access to all previous results:

Accessing Previous Step Results

Every step receives two arguments: input (output from the previous step) and ctx (the full workflow context). Use ctx to access any previous step’s result by name.
Use kebab-case IDs for steps (e.g., fetch-user, generate-report). Avoid spaces, accents, or special characters — the step ID becomes the key in ctx.results, so ctx.results['fetch-user'] is much cleaner than ctx.results['Buscar Usuário'].
What’s available in ctx:
You can only access results from steps that have already executed. Accessing a step that hasn’t run yet (or was skipped by a when guard) returns undefined.
Common patterns:

Schema Validation

Steps can declare an outputSchema for runtime validation. If the output doesn’t match, execution stops immediately with a ZodError:

Conditional Guards

Skip steps based on runtime conditions:

Step Types

.step() — Function Step

Transform data, call APIs, run business logic:

.agent() — Agent Step

Execute an AI agent. The output is always { text, metadata: { agent, model, stepId } }:

.connector() — Connector Step (simple)

Call an external service with template interpolation:

Connectors inside .step() (dynamic)

For dynamic connector calls with logic, use the connector() function inside a .step():
connector() is a function, not a client factory. Always pass all 3 arguments: connector(slug, resource, data). See Connectors for details.

Routing

.branch() — Binary Routing (if/else)

Route to one of two paths based on a condition:
For complex paths with multiple steps, pass arrays:

.switch() — Multi-way Routing

Route to one of N paths based on a value:
With agent steps per case:

Parallel & Iteration

.parallel() — Concurrent Execution

Run multiple steps at the same time:

.foreach() — Array Iteration

Process each item in an array, with optional concurrency:

.map() — Data Transformation

Transform output between steps when shapes don’t match:

Output Transform

Define how to build the final workflow output from all step results:

Retry Configuration

Add retry logic to any step:

Real-time Events

Workflows emit events during execution for monitoring:

Graph Serialization

Get the workflow structure as a serializable DAG for visualization:

Full Example: Multi-Agent Customer Service

Full Example: Lead Qualification Pipeline

Common Mistakes

.parallel() — Must receive an array of steps

.switch() — Use on, not key or evaluate

.foreach() — Input must be an array

The previous step must return an array. Use .map() to extract it if needed:

.map() — No ID, no ctx, just a transform function

connector() — Function call, not a client

Method Reference

Observability

Workflows share the same observability controls as Agents.

Trace Hierarchy

Every workflow execution automatically generates a hierarchical trace tree:

Controlling Verbosity

Sanitizing Traces

Use onTrace to remove sensitive data or cancel specific traces:
See Observability for the complete reference on tracing modes, interceptors, and custom logging.

Next Steps

Observability

Tracing, interceptors, and metrics

Complex Workflows

Advanced patterns and real-world examples

Connectors

Integrate external services