Overview
Workflows allow you to:- Create event-driven architectures
- Manage state across multiple steps
- Build iterative refinement loops
- Compose complex AI pipelines
Complete Example
joke-workflow.ts
Step-by-Step Explanation
1. Define Events
Events represent data flowing through your workflow:2. Create Stateful Workflow
Add state management to track workflow progress:3. Define Event Handlers
Handlers process events and emit new events:4. Conditional Routing
Route to different events based on conditions:5. Access State
Read and update workflow state:6. Execute Workflow
Workflow Patterns
Linear Pipeline
Sequential processing steps:Parallel Execution
Process multiple paths simultaneously:Iterative Refinement
Loop until condition met:Error Handling
Handle errors gracefully:Running the Example
- Install dependencies:
- Set your API key:
- Run the workflow:
Expected Output
The workflow will:- Generate an initial joke about pirates
- Critique the joke
- If needed, improve the joke up to 3 times
- Return the final joke and critique
Advanced Features
Timeout Handling
Workflow Composition
Combine multiple workflows:Next Steps
Advanced Workflows
Explore the workflows-ts repository
Agents
Combine workflows with agents
RAG Pipeline
Build RAG ingestion workflows
Event Streaming
Stream workflow events to clients
Related Examples
- Workflows Repository - Advanced workflow patterns
- Document Workflow - Document processing workflow
- Agent Workflows - Agent-based workflows